Programming Fundamental Instructor Name: Muhammad Safyan Lecture-12
Lecture outline Function: Call By Value Function Call By Reference Scope of Identifiers
Functions
Function Steps involve in functions: Declare a function Define a function Call a function
Calling function Called function
Area of the Ring Inner Circle Outer Circle Area of Outer Circle ____ Area of Inner Circle Area of Outer Circle = Area of the Ring
Calculating ringArea without using Function main ( ) { ringArea = ( 3.1415926 * rad1 * rad1 ) – ( 3.1415926 * rad2 * rad2 ) ; }
Example: Function to calculate the area of a ring (with Return Value)
Example: Function to calculate the area of a ring (Without Return a value)
Call By Value
Formal and Actual Parameters formal parameter — the identifier used in a method to stand for the value that is passed into the method by a caller. For example, amount is a formal parameter of process Deposit. actual parameter — the actual value that is passed into the method by a caller. For example, the 200 used when process Deposit is called is an actual parameter.
Call By Value #include <iostream.h > Void f ( int ) ; main ( ) { int i = 10 ; cout << “In main i = " << i ; f ( i ) ; cout << " Back in main, i = " << i ; } void f( int i ) { cout << "In function f , i = " << i ; i *= 2 ; cout << "In function f , i is now = “ << i ; }
int square( int ); main() { int a = 10; cout << a << " squared: " << square( a ) << endl; Getch(); } int square( int x ) return x * x;
Call Value
Call Value
Call By Value
Use of Function in IF Condition We can also use a function in the conditional statements like: if ( isEven ( number ) )
#include <iostream> #include <conio.h> using namespace std; int isEven(int); main ( ) { int number; cout << " Please enter the number: " ; cin>>number ; if ( isEven ( number ) ) cout << " The number entered is even " << endl; } else cout << " The number entered is odd " << endl; getch(); int isEven ( int number ) if ( 2 * ( number / 2 ) == number ) return 1; return 0;
Call By Reference
Reference &x Int j=2; Int &x=j‘; int &y; // Error: y must be initialized
Reference int main() { int x = 3; int &y = x; cout << "x = " << x << endl << "y = " << y << endl; y = 7; Getch(); }
CallByReference In ‘Call By Reference’, we are not passing the value itself but some form of reference or address. To understand this, you can think in terms of variables which are names of memory locations. We always access a variable by its name (which in fact is accessing a memory location), a variable name acts as an address of the memory location of the variable.
CallByReference we can’t pass the value. We have to pass the memory address of the value. This introduces a new mechanism which is achieved by using ‘&’ (ampersand) operator in C language. This ‘&’ operator is used to get the address of a variable.
Call by Reference Example void squareByReference( int & ); int main() { int z = 4; cout << "z = " << z << " before squareByReference" << endl; squareByReference( z ); cout << "z = " << z << " after squareByReference" << endl; getch(); } void squareByReference( int &numberRef ) numberRef *= numberRef;
Example
Scope of Identifiers Identifier is any name user creates in his/her program Functions are also identifiers Labels are also identifiers
Scope of Identifiers Scope means visibility A variable declared inside a block has visibility within that block only Variables defined within the function has a scope that is function wide
Example void functionName ( ) { int i ; } …..
Identifiers Important Points Do not create variables with same name inside blocks, inside functions or inside bigger blocks Try to use separate variable names to avoid confusion Reuse of variables is valid
File Scope # include < iostream.h > int i ; Global variable
Global Variable Can be used anywhere in program Can cause logical problems if same variable name is used in local variable declarations For good programming Try to minimize the use of global variables Try to use local variables as far as possible
Visibility of Identifiers Global Scope Anything identified or declared outside of any function is visible to all functions in that file Function level scope Declaring variables inside a function can be used in the whole function Block level scope Variables or integers declared inside block are used inside block
for ( int i = 0 ; i < 10 ; i++ ) Example: Block Scope for ( int i = 0 ; i < 10 ; i++ ) It is block level scope declared in for loop When for is finished “ i ” no longer exists
Example: Global Scope using namespace std; int i ; void f () ; main() { i=10; cout<<"within main i ="<<i<<endl ; f() ; cout<<"after calling f() i ="<<i<<endl ; getch(); } void f () cout<< "Inside function f , i =" << i<<endl ; i=20 ;