# Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 5. Functions.

## Presentation on theme: "Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 5. Functions."— Presentation transcript:

Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 5. Functions

Prof. amr Goneid, AUC2 Functions

3 Functions Predefined (Library) Functions Modular Programming with Functions Types of Functions Function Prototype Declaration Function Definition Formal & Actual Parameters Who Sees Who: Scope of an Identifier Parameter Passing

Prof. amr Goneid, AUC4 1. Predefined Functions Example: #include void main() { cout << ”Square Root. Ex: sqrt(9.0) = ” << sqrt(9.0) << endl; cout << ”Powers. Ex: pow(3.0, 4.0) = ” << pow(3.0,4.0) << endl; cout << ”Absolute Value for int. Ex: ” << ”abs(-9) = ” << abs(-9) << endl; cout << ”Absolute Value for long. Ex: ” << ”labs(-900) = ” << labs(-900) << endl;

Prof. amr Goneid, AUC5 cout << ”Absolute Value for double. Ex: ” << ”fabs(-9.5) = ” << fabs(-9.5) << ”\n”; cout << ”Ceiling (round up). Ex: ceil(4.1)” << ” = ” << ceil(4.1) << endl; cout << ”Floor (round down). ” << ”Ex: floor(4.7) = ” << floor(4.7) << endl; } Square Root. Ex: sqrt(9.0) = 3.0 Powers. Ex: pow(3.0,4.0) = 81 Absolute Value for int. Ex: abs(-9) = 9 Absolute Value for long. Ex: labs(-9000) = 9000 Absolute Value for double. Ex: fabs(-9.5) = 9.5 Ceiling (round up). Ex: ceil(4.1) = 5 Floor (round down). Ex: floor(4.7) = 4 Output:

Prof. amr Goneid, AUC6 2. Modular Programming with Functions Level Main Function Function1 Function2 Function3 Function4Function5 0 1 2.... Functions are natural building blocks for modular programming

Prof. amr Goneid, AUC7 C++ Program Structure Compiler Directives Function Prototypes int main ( ) { Main Data Declarations Main Actions } Used Functions Defined Here C++ program contains function prototype declarations and function definitions. The main is just another function.

Prof. amr Goneid, AUC8 Functions & The Main Function A function is invoked by another function (e.g main) int main( ) main Data Area Main Body Invoke function Next action Function Header Local Data Area Function Body

Prof. amr Goneid, AUC9 3. Types of Functions Input Params Typed Function void Function Action Output Params Returns a Single Scalar Value

Prof. amr Goneid, AUC10 4. Function Prototype Declaration Syntax: (formal parameter list) ; Examples: int cube ( int n ) ; // A function receiving an int parameter (n) and returning an int value. float minxy ( float x, float y ) ; // A function receiving two float parameters ( x, y ) and returning a float value.

Prof. amr Goneid, AUC11 Prototype Declaration (Examples) void printchar ( char c, int n ) ; // a function receiving two parameters ( c, n ) and returns nothing. It is supposed to do an action, e.g. print n of char c on one line. void errormessage ( ) ; // a function receiving nothing and returning nothing. It is supposed to do an action, e.g. print a fixed error message.

Prof. amr Goneid, AUC12 5. Function Definition ( List of Formal Parameters) { Local Data Declarations Function Actions (Executable Statements) }

Prof. amr Goneid, AUC13 Building Typed Functions Syntax: (formal params) { Local Data Area Function Body contains a statement: return ; }

Prof. amr Goneid, AUC14 Example of an Integer Function Function to return the larger of two integer numbers a and b. int maxab ( int a, int b ) { //Does not need Local Data return ( (a >= b) ? a : b ); }

Prof. amr Goneid, AUC15 Example of a Real Function Function to return the area of a circle of radius r. float area ( float r ) { // Local Data const float pi = 3.14159 ; // Action return ( pi * r * r ) ; }

Prof. amr Goneid, AUC16 Example of a Boolean Function Function to return true if an integer n is even and false otherwise. bool iseven ( int n ) { //Does not need Local Data return ( n % 2 == 0 ) ; }

Prof. amr Goneid, AUC17 Example of Using a Typed Function // Prints if an entered integer is even or odd # include using namespace std; // Function used…. bool iseven ( int n ); int main ( ) { int num; cout << “ Enter an integer number: “; cin >> num;

Prof. amr Goneid, AUC18 Example of Using a Typed Function if ( iseven ( num ) ) cout << “ Number is even ! “ ; else cout << “ Number is odd ! “; return 0 ; } // Returns true if an integer is even, false otherwise bool iseven ( int n ) { return ( n % 2 == 0 ); }

Prof. amr Goneid, AUC19 Type of Returned Value Type of a value returned by a called function must be consistent with the type expected by the caller as identified in the function prototype declaration.

Prof. amr Goneid, AUC20 Building void Functions Syntax: void (formal params) { Local Data Area Function Body does not contain a return statement }

Prof. amr Goneid, AUC21 Example of a void Function Action: Fill screen with blanks. void blankscreen( ) { const char blank = ‘ ’ ; int row, col ; for (row = 1; row <= 25; row++) { for (col = 1; col <= 80; col++) cout << blank ; cout << endl; }

Prof. amr Goneid, AUC22 Example of a void Function Action: Write n dashes on a line. void dashes( int n ) { const char dash = ‘-’ ; int i ; for (i = 1; i <= n; i++) cout << dash ; }

Prof. amr Goneid, AUC23 Example of Using a void Function // Prints numbers and dashes # include using namespace std; // Function used…. void dashes ( int n ); int main ( ) { float salary, bonus; cout > salary; bonus = 0.1 * salary ;

Prof. amr Goneid, AUC24 Example of Using a void Function cout << “Bonus ” ; dashes(3); cout << bonus; dashes(5); cout << endl; return 0 ; } // Writes n dashes on one line void dashes ( int n ) { const char dash = ‘-’ ;int i ; for (i = 1; i <= n; i++) cout << dash ; }

Prof. amr Goneid, AUC25 6. Formal & Actual Parameters In Function Declarations: bool iseven(int n);int maxab( int a, int b ) void dashes(int n); a,b,n are FORMAL parameters(Dummies or Gates). They are LOCAL to their modules. When invoked in a main function: maxab(x,y) or maxab(1+z,2.3) dashes(7); dashes(k);iseven ( num ) x, y, 1+z, 2.3, 7, k, num are ACTUAL parameters passed from main to modules through their respective gates.

Prof. amr Goneid, AUC26 Key Points Ê The substitution of the value of an actual parameter in a function call for its corresponding formal parameter is strictly positional. That is, the value of the first actual parameter is substituted for the first formal parameter; the second and so on

Prof. amr Goneid, AUC27 Key Points Ë The names of these corresponding pairs of parameters are no consequence in the substitution process. The names may be different, or they may be the same. Ì The substituted value is used in place of the formal parameter at each point where that parameter appears in the called function.

Prof. amr Goneid, AUC28 Passing values of Actual Parameters main maxab main iseven a b n num x y

Prof. amr Goneid, AUC29 Formal & Actual Parameters Correspondence between actual and formal parameters is determined by position in their respective lists. These lists must be the same size. The names of corresponding actual and formal parameters may be different. Formal parameters and corresponding actual parameters should agree with respect to type.

Prof. amr Goneid, AUC30 Overloaded Functions: #include float average(float x, float y); // Returns the average of x and y float average(float x, float y, float z); // Returns the average of x, y, and z void main() { cout << ”The average of 3.0 and 7.0” << ” is ” << average(3.0, 7.0) << endl; cout << ”The average of 3.0, 4.0, and 8.0” << ” is ” << average(3.0, 4.0,8.0) << endl; }

Prof. amr Goneid, AUC31 float average(float x, float y) { return ((x + y)/2.0); } float average(float x, float y, float z) { return ((x + y + z)/3.0); } The average of 3.0 and 7.0 is 5.0000 The average of 3.0, 4.0, and 8.0 is 5.0000 Output:

Prof. amr Goneid, AUC32 7. Who Sees Who: Scope of an Identifier To see = to recognize = to be able to use, invoke, change, etc. Scope = the domain in which an identifier is recognizable. The scope of an identifier extends only from the point where it is defined to the end of the module in which it is defined. A module can see itself (Recursion)

Prof. amr Goneid, AUC33 Scope(continued) Global : can be seen by all modules. Local: can be seen only by its module but not by other modules. Names declared inside a function/main are local to that function/main. Anything declared before the main function is global. It can be called anywhere in the program. Hence, all functions are global. For two things having the same id, local overrides global.

Prof. amr Goneid, AUC34 Scope(example) Module A Data P, Q Module B Data x, w Module C Data m, n Main Data x, y Prototypes of A, B, C int x, m; // Global Variables

Prof. amr Goneid, AUC35 A function to swap two characters. // x and y are passed by value void swap (char x, char y) { char temp; temp = x; x = y; y = temp; } 8. Parameter Passing: Example of a Paradox

Prof. amr Goneid, AUC36 Paradox (continued) A program uses the function to swap two characters: void swap (char x, char y); int main ( ) { char a,b ; a = ‘M’ ; b = ‘N’ ; cout << a << ‘ ‘ << b << endl; swap(a,b); cout << a << ‘ ‘ << b << endl; } No Change! Why ? M N

Prof. amr Goneid, AUC37 Where in Memory? The DOS Memory Map: one segment = 64 kbyte DS = Data Segment (Data) CS = Code Segment ( Main & Modules code) SS = Stack Segment (System Stack) Heap = Rest of DOS memory DOSCSDSSSHEAP LM HM

Prof. amr Goneid, AUC38 Parameter Passing: What Really Happened Memory BeforeMemory After ‘N’ ‘M’ ‘N’ ‘M’a b a b Swap addr ‘M’ -> x ‘N’-> y Swap addr ‘N’ <- x ‘M’ <- y DSSSDSSS

Prof. amr Goneid, AUC39 To see the change, pass the address, not the value ! Memory BeforeMemory After ‘N’ ‘M’ ‘N’a b a b Swap addr Addr of a Addr of b Swap addr Addr of a Addr of b DSSSDSSS

Prof. amr Goneid, AUC40 How to pass the Address (pass by Reference) The correct function to swap two characters. // x and y are passed by reference void swap (char& x, char& y) { char temp; temp = x; x = y; y = temp; } // symbol & means address of

Prof. amr Goneid, AUC41 Passing by Reference (continued) A program uses the function to swap two characters: void swap (char& x, char& y); int main ( ) { char a,b ; a = ‘M’ ; b = ‘N’ ; cout << a << ‘ ‘ << b << endl; swap(a,b); cout << a << ‘ ‘ << b << endl; } Now there is Change! M N N M

Prof. amr Goneid, AUC42 Parameter Passing: Summary Input Only Parameters: those you do not want to change- pass by value Output Only Parameters: those you want to see what happened to them – pass by reference (address) using &. Input/Output Parameters: pass by reference using &.

Download ppt "Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 5. Functions."

Similar presentations