Download presentation
Presentation is loading. Please wait.
1
Programming Fundamental
Instructor Name: Muhammad Safyan Lecture-12
2
Lecture outline Function: Call By Value Function Call By Reference
Scope of Identifiers
3
Functions
4
Function Steps involve in functions: Declare a function
Define a function Call a function
5
Calling function Called function
6
Area of the Ring Inner Circle Outer Circle Area of Outer Circle
____ Area of Inner Circle Area of Outer Circle = Area of the Ring
7
Calculating ringArea without using Function
main ( ) { ringArea = ( * rad1 * rad1 ) – ( * rad2 * rad2 ) ; }
8
Example: Function to calculate the area of a ring
(with Return Value)
9
Example: Function to calculate the area of a ring
(Without Return a value)
10
Call By Value
11
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.
12
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 ; }
13
int square( int ); main() { int a = 10; cout << a << " squared: " << square( a ) << endl; Getch(); } int square( int x ) return x * x;
14
Call Value
15
Call Value
16
Call By Value
17
Use of Function in IF Condition
We can also use a function in the conditional statements like: if ( isEven ( number ) )
18
#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;
19
Call By Reference
20
Reference &x Int j=2; Int &x=j‘;
int &y; // Error: y must be initialized
21
Reference int main() { int x = 3; int &y = x;
cout << "x = " << x << endl << "y = " << y << endl; y = 7; Getch(); }
22
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.
23
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.
24
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;
25
Example
28
Scope of Identifiers Identifier is any name user creates in his/her program Functions are also identifiers Labels are also identifiers
29
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
30
Example void functionName ( ) { int i ; } …..
31
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
32
File Scope # include < iostream.h > int i ; Global variable
33
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
34
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
35
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
36
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 ;
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.