Download presentation
Presentation is loading. Please wait.
1
The Function Prototype
Like other identifiers in C++, function must be declared before it can be referenced. To declare a function, we can insert a function prototype before the main function. The function prototype provides all information that the C++ compiler needs to know to translate calls to the function correctly. A function prototype tells the compiler the - data type of the function - the function name - information about the arguments that the function expects. Examples: void draw_circle ( ); int m ( ) ; void print_box (int) ; int Rect_area (int , int);
2
Function Prototype #include <iostream.h> int Mul(int, int);
int Add(int, int); void Show(); void main() { Show(); } int Mul(int X, int Y) { return X*Y; } int Add(int X, int Y) { return X+Y; } void Show() { int A=10, B=20; cout<<Add(A,B)<<'\t'<<Mul(A,B)<<endl; } Function Prototype contains only data types But may contain identifiers.
3
Scope of Variables (1) Global variables:
- Those variables that are declared before the main function. - These are visible from any point of the code, inside and outside any function. (2) Local variables: - These are declared inside a block or a function. - The scope of local variables is limited to the same nesting level in which they are declared.
4
Example of Local and Global Variables
// File: global.cpp #include <iostream.h> int x = 7 ; // global variables int fun1 (int ); // function prototype void main ( ) { int z ; // local variable in main cout << “The global variable: “ << x ; z = fun1 ( 5 ) ; // calling add function cout << “ The result is “ << z << endl ; } int fun1 ( int a ) { int r ; // local variable in fun1 r = a * a * a ; return r ; }
5
Functions and Passing Parameters
The mechanisms of passing parameters: (1) Call by value: - During the function call, the value of the argument is found and passed to the function. - Any modification inside the function does not affect the argument. (2) Call by reference: - During the function call, the address of the variable is passed to the function. - Any modification made on the parameter inside the function will have effect in the passed variable outside it.
6
Difference between Function Definitions for Call by Value and Call by Reference
For call by value, we declare the arguments of the function as usual. Example: int func1 ( int , int ); // function prototype For call by reference, the type of the argument is followed by the symbol (&) to specify that the variable has to be passed by reference. void func2 ( int & , int & ); // function prototype
7
Call by value When calling, the value of actual parameter will be copied to the formal parameter. #include <iostream.h> void Increment(int); void main() { int A = 10; Increment(A); cout<<A<<endl; } void Increment(int X) { ++X; }
8
Call By reference When calling, the reference formal parameter will be an alternative name to the actual parameter. #include <iostream.h> void Increment(int&); void main() { int A = 10; Increment(A); cout<<A<<endl; } void Increment(int &X) { ++X; }
9
Call By reference When calling, the pointer formal parameter will points to the actual parameter. #include <iostream.h> void Increment(int*); void main() { int A = 10; Increment(&A); cout<<A<<endl; } void Increment(int *X) { ++*X; }
10
Example 5: Call by Value // File: calls1.cpp
#include <iostream.h> // function prototype: the arguments to be passed by value int add (int , int ); void main ( ) { int x, y , z ; // local variables in main cout << “ Enter two integers: “ ; cin >> x >> y ; cout << “ x = “ << x << “ y = “ << y << endl; z = add ( x , y ) ; // calling add function cout << “ The result is “ << z ; cout <<“After call “<< “ x = “ << x << “ y = “ << y << endl; } int add ( int a , int b ) { return a + b ; }
11
Execution of Example 5 The user will be prompted to enter two integers. The user enters, for example, 10 and 20 that are saved in x and y respectively. When function add is called, the value of x and y are passed to the function. The function add takes the values 10 and 20 and links them to a and b respectively. The function will return the result which is 30. The output: Enter two integers: x = y = 20 The result is 30 After call x = 10 y = 20
12
Example 6: Call by Reference
// File: calls2.cpp #include <iostream.h> // function prototype: the arguments to be passed by reference void duplicate (int & , int & , int & ); void main ( ) { int x, y , z ; // local variables in main cout << “ Enter three integers: “ ; cin >> x >> y >> z ; cout << “ Before call: “ << “ x = “ << x << “ y = “ << y << “ z = “ << z <<endl; duplicate ( x , y , z ) ; // calling duplicate function cout <<“ After call: << “ x = “ << x << “ y = “ << y << “ z = “ << z <<endl; } void duplicate ( int & a , int & b , int & c ) { a *= 2 ; b *= 2 ; c *= 2 ; }
13
Execution of Example 6 The user will be prompted to enter three integers. The user enters, for example, 10, 20, and 30 that are saved in x, y, and z respectively. When function duplicate is called, the addresses of x, y, and z are passed to the function. The addresses of x, y, and z are linked to the parameters of the function a, b, and c. The function will duplicate each parameter and save the result in the same parameter. Thus, a becomes 20 hence x becomes 20 also b becomes 40 hence x becomes 40 also c becomes 60 hence x becomes 60 also After the call we see that the values of x, y, and z are changed. The output: Enter three integers: Before call: x = y = z = 30 After call: x = y = z = 60
14
Recursion Function call itself #include <iostream.h>
int Fact (int N) { if (N<=1) return 1; else return N * Fact(N-1); } void main() { cout<<Fact(5)<<endl;
15
Example #include <iostream.h> int Zap (int N) { int Z;
if (N<=1) Z=1; else Z= Zap(N-1) + Zap(N-3); return Z; } void main() { cout<<Zap(5)<<endl;
16
Array as parameter -Array name is pointer (call by reference)
#include <iostream.h> void Increment (int a[]) { for (int i=0; i<5; i++) a[i] += 10; } void main() { int b[5] = {10,20,30,40,50}; Increment(b); for(int i=0; i<5; i++) cout<<b[i]<<'\t'; cout<<endl; }
17
Array as parameter -Array element (call by value)
#include <iostream.h> void Increment (int a) { ++a; } void main() { int b[5] = {10,20,30,40,50}; Increment(b[1]); cout<<b[1]<<endl; }
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.