Introduction to Programming

Similar documents
Chapter 3 - Functions

C++ PROGRAMMING SKILLS Part 3 User-Defined Functions

Functions and Recursion

C++, How to Program. Spring 2016 CISC1600 Yanjun Li 1

C++ Programming Lecture 11 Functions Part I

Functions and an Introduction to Recursion Pearson Education, Inc. All rights reserved.

Chapter 3 - Functions. Chapter 3 - Functions. 3.1 Introduction. 3.2 Program Components in C++

C Functions. 5.2 Program Modules in C

CHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad

Chapter 3 - Functions

Functions. Systems Programming Concepts

C Programming for Engineers Functions

Function Example. Function Definition. C Programming. Syntax. A small program(subroutine) that performs a particular task. Modular programming design

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 4

Dr M Kasim A Jalil. Faculty of Mechanical Engineering UTM (source: Deitel Associates & Pearson)

CHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad

엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University COPYRIGHTS 2017 EOM, HYEONSANG ALL RIGHTS RESERVED

C++ Programming: From Problem Analysis to Program Design, Fifth Edition. Chapter 6: User-Defined Functions I

Using Free Functions

Methods (Deitel chapter 6)

CSE123. Program Design and Modular Programming Functions 1-1

Methods (Deitel chapter 6)

Lecture 04 FUNCTIONS AND ARRAYS

Introduction to Programming

Programming Fundamentals for Engineers Functions. Muntaser Abulafi Yacoub Sabatin Omar Qaraeen. Modular programming.

Methods: A Deeper Look

GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004

Fundamentals of Programming Session 25

Functions. Angela Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan.

Programming Language. Functions. Eng. Anis Nazer First Semester

6-1 (Function). (Function) !*+!"#!, Function Description Example. natural logarithm of x (base e) rounds x to smallest integer not less than x

INTRODUCTION TO C++ FUNCTIONS. Dept. of Electronic Engineering, NCHU. Original slides are from

CT 229 Java Syntax Continued

Chapter 6 - Notes User-Defined Functions I

Week 2: Console I/O and Operators Arithmetic Operators. Integer Division. Arithmetic Operators. Gaddis: Chapter 3 (2.14,3.1-6,3.9-10,5.

Functions. Prof. Indranil Sen Gupta. Dept. of Computer Science & Engg. Indian Institute t of Technology Kharagpur. Introduction

(created by professor Marina Tanasyuk) FUNCTIONS

C Programs: Simple Statements and Expressions

Computing and Statistical Data Analysis Lecture 3

Functions. Functions are everywhere in C. Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR

Fundamentals of Programming Session 4

CSE101-lec#12. Designing Structured Programs Introduction to Functions. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU

Programming for Engineers Functions

Fundamentals of Programming Session 24

Fundamentals of Programming Session 20

Function. specific, well-defined task. whenever it is called or invoked. A function to add two numbers A function to find the largest of n numbers

Fundamentals of Programming Session 8

Lab Instructor : Jean Lai

Chapter 4. Procedural Abstraction and Functions That Return a Value. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Introduction to Programming

Lecture 3. Review. CS 141 Lecture 3 By Ziad Kobti -Control Structures Examples -Built-in functions. Conditions: Loops: if( ) / else switch

Fundamentals of Programming Session 12

Fundamentals of Programming Session 23

Fundamentals of Programming & Procedural Programming

Fundamentals of Programming Session 15

Chapter 3 - Functions

Outline. Why do we write functions? Introduction to Functions. How do we write functions? Using Functions. Introduction to Functions March 21, 2006

Functions. A function is a subprogram that performs a specific task. Functions you know: cout << Hi ; cin >> number;

1001ICT Introduction To Programming Lecture Notes

4. C++ functions. 1. Library Function 2. User-defined Function

Functions. Lecture 6 COP 3014 Spring February 11, 2018

Introduction to Programming session 24

Engineering Problem Solving with C++, 3e Chapter 2 Test Bank

Fall Semester (081) Dr. El-Sayed El-Alfy Computer Science Department King Fahd University of Petroleum and Minerals

CSCE 110 PROGRAMMING FUNDAMENTALS

C Functions Pearson Education, Inc. All rights reserved.

Functions. Computer System and programming in C Prentice Hall, Inc. All rights reserved.

CS 106 Introduction to Computer Science I

Downloaded from Chapter 2. Functions

Function. Mathematical function and C+ + function. Input: arguments. Output: return value

1. Variables 2. Arithmetic 3. Input and output 4. Problem solving: first do it by hand 5. Strings 6. Chapter summary

Built-in Types of Data

1. Match each of the following data types with literal constants of that data type. A data type can be used more than once. A.

Top-Down Design Predefined Functions Programmer-Defined Functions Procedural Abstraction Local Variables Overloading Function Names

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces

CISC 1110 (CIS 1.5) Introduc2on to Programming Using C++

Chapter 5 C Functions

Fundamentals of Programming Session 13

Review. Modules. CS 151 Review #6. Sample Program 6.1a:

Lesson #3. Variables, Operators, and Expressions. 3. Variables, Operators and Expressions - Copyright Denis Hamelin - Ryerson University

C++ Programming: From Problem Analysis to Program Design, Third Edition

Introduction to C Language

Functions and Recursion

Intrinsic Functions Outline

Functions in C++ Problem-Solving Procedure With Modular Design C ++ Function Definition: a single

3. Functions. Modular programming is the dividing of the entire problem into small sub problems that can be solved by writing separate programs.

Fundamentals of Programming Session 14

Computer Programming C++ Classes and Objects 6 th Lecture

Introduction to Scientific Programming with C++

More Flow Control Functions in C++ CS 16: Solving Problems with Computers I Lecture #4

Functions. Lab 4. Introduction: A function : is a collection of statements that are grouped together to perform an operation.

Lecture 04 FUNCTIONS AND ARRAYS

Fundamentals of Programming Session 19

Methods CSC 121 Fall 2014 Howard Rosenthal

Fundamentals of Programming Session 24

3.1. Chapter 3: The cin Object. Expressions and Interactivity

CS110: PROGRAMMING LANGUAGE I

CS 151 Review #3. // More than one variable can be defined // in a statement. Multiple variables are // separated by a comma.

The Math Class. Using various math class methods. Formatting the values.

Chapter 2. Outline. Simple C++ Programs

Transcription:

Introduction to Programming session 9 Instructor: Reza Entezari-Maleki Email: entezari@ce.sharif.edu 1 Fall 2010 These slides are created using Deitel s slides Sahrif University of Technology

Outlines Program Components in C++ Math Library Functions Functions Functions Definitions 2

Program Components in C++ Divide and conquer Construct a program from smaller pieces or components Each piece more manageable than the original program Modules: functions and classes Programs use new and prepackaged modules New: programmer-defined functions, classes Prepackaged: from the standard library Functions invoked by function call Function name and information (arguments) it needs 3

Program Components in C++ Function definitions Only written once Hidden from other functions Boss to worker analogy A boss (the calling function or caller) asks a worker (the called function) to perform a task and return (i.e., report back) the results when the task is done. 4

Math Library Functions Perform common mathematical calculations Include the header file <cmath> Functions called by writing functionname (argument); or functionname(argument1, argument2, ); Example cout << sqrt( 900.0 ); sqrt (square root) function The preceding statement would print 30 All functions in math library return a double 5

Math Library Functions Function arguments can be Constants sqrt( 4 ); Variables sqrt( x ); Expressions sqrt( sqrt( x ) ) ; sqrt( 3-6x ); 6

7 M e t h o d D e sc rip t io n Exa m p le ceil( x ) rounds x to the sm allest integer not less than x ceil( 9.2 ) is 10.0 ceil( -9.8 ) is -9.0 cos( x ) trigonom etric cosine of x cos( 0.0 ) is 1.0 (x in radians) exp( x ) ex ponential function ex exp( 1.0 ) is 2.71828 exp( 2.0 ) is 7.38906 fabs( x ) absolute value of x fabs( 5.1 ) is 5.1 fabs( 0.0 ) is 0.0 fabs( -8.76 ) is 8.76 floor( x ) rounds x to the largest integer not greater th an x floor( 9.2 ) is 9.0 floor( -9.8 ) is -10.0 fmod( x, y ) rem ainder o f x/y as a floating- fmod( 13.657, 2.333 ) is 1.992 point num ber log( x ) natural logarithm of x (base e) log( 2.718282 ) is 1.0 log( 7.389056 ) is 2.0 log10( x ) logarithm of x (base 10) log10( 10.0 ) is 1.0 log10( 100.0 ) is 2.0 pow( x, y ) x raised to pow er y (xy) pow( 2, 7 ) is 128 pow( 9,.5 ) is 3 sin( x ) trigonom etric sine o f x sin( 0.0 ) is 0 (x in radians) sqrt( x ) square root of x sqrt( 900.0 ) is 30.0 sqrt( 9.0 ) is 3.0 tan( x ) trigonom etric tan gent of x tan( 0.0 ) is 0 (x in radians) Fig. 3.2 M a t h lib ra ry fu n c t io n s.

Functions Functions Modularize a program Software reusability Call function multiple times Local variables Known only in the function in which they are defined All variables declared in function definitions are local variables Parameters Local variables passed to function when called Provide outside information 8

Functions Definitions Function prototype Tells compiler argument type and return type of function int square( int ); Function takes an int and returns an int Explained in more detail later Calling/invoking a function square(x); Parentheses an operator used to call function Pass argument x Function gets its own copy of arguments After finished, passes back result 9

Functions Definitions Format for function definition return-value-type function-name( parameter-list ) { declarations and statements } Parameter list Comma separated list of arguments Data type needed for each argument If no arguments, use void or leave blank Return-value-type Data type of result returned (use void if nothing returned) 10

Functions Definitions Example function int square( int y ) { return y * y; } return keyword Returns data, and control goes to function s caller If no data to return, use return; Function ends when reaches right brace Control goes to caller Functions cannot be defined inside other functions Next: program examples 11

1 // Creating and using a programmer-defined function. 2 #include <iostream> 3 using std::cout; 4 using std::endl; Function prototype: specifies data types of arguments and return values. square expects and int, and returns an int. 5 int square( int ); // function prototype 6 int main() 7 { 8 // loop 10 times and calculate and output 9 // square of x each time 10 for ( int x = 1; x <= 10; x++ ) Parentheses () cause function to be called. When done, it returns the result. 11 cout << square( x ) << " "; // function call 12 cout << endl; 13 return 0; // indicates successful termination 14 } // end main 12

15 // square function definition returns square of an integer 16 int square( int y ) // y is a copy of argument to function 17 { 18 return y * y; // returns square of y as an int 19 20 } // end function square Definition of square. y is a copy of the argument passed. Returns y * y, or y squared. 1 4 9 16 25 36 49 64 81 100 13