Upload
bilal-ahmed
View
19
Download
0
Embed Size (px)
DESCRIPTION
Virtual University Course CS201- Introduction to Programming Lecture No 10 Instructor's Name: Dr. Naveed A. Malik Course Email: [email protected]
Citation preview
Introduction to Introduction to ProgrammingProgramming
Lecture 10Lecture 10
Header FilesHeader Files
Scope of Scope of VariablesVariables
FunctionsFunctions– Call by valueCall by value– Call by referenceCall by reference
Today's Lecture Today's Lecture IncludesIncludes
#include <iostream.h>#include <iostream.h>
Header FilesHeader Files
int functionName ( int , int functionName ( int , int );int );
PrototypePrototype
Return valueAssignment List with data type
double pi = 3.1415926;double pi = 3.1415926;
Using Header FilesUsing Header Files
It is better to define this value in a It is better to define this value in a header fileheader file
Then simply by including the header file Then simply by including the header file in the program this value is defined and in the program this value is defined and it has a meaningful nameit has a meaningful name
#define pi 3.1415926#define pi 3.1415926 Name can be used inside a program Name can be used inside a program
exactly like a variableexactly like a variable It cannot be used as a variable It cannot be used as a variable
CircleArea = pi * radius * radiusCircleArea = pi * radius * radius
Circumference = 2 * pi * radiusCircumference = 2 * pi * radius
#define
Identifier is any name user creates in Identifier is any name user creates in his/her programhis/her program
Functions are also identifiersFunctions are also identifiers
Labels are also identifiersLabels are also identifiers
Scope of Scope of IdentifiersIdentifiers
Scope means visibilityScope means visibility
A variable declared inside a block has A variable declared inside a block has visibility within that block onlyvisibility within that block only
Variables defined within the function Variables defined within the function has a scope that is function widehas a scope that is function wide
Scope of Scope of IdentifiersIdentifiers
ExampleExamplevoid functionName ( ) void functionName ( )
{{
{{
int i ; int i ;
}}
……....
}}
Do not create variables with Do not create variables with same name inside blocks, inside same name inside blocks, inside functions or inside bigger blocksfunctions or inside bigger blocks
Try to use separate variable Try to use separate variable names to avoid confusionnames to avoid confusion
Reuse of variables is validReuse of variables is valid
Identifiers Important Identifiers Important PointsPoints
# include < iostream.h ># include < iostream.h > int i ;int i ;
File ScopeFile Scope
Global variable
Can be used anywhere in programCan be used anywhere in program Can cause logical problems if same variable Can cause logical problems if same variable
name is used in local variable declarationsname is used in local variable declarations
For good programmingFor good programming
Try to minimize the use of global variablesTry to minimize the use of global variables Try to use local variables as far as possibleTry to use local variables as far as possible
Global VariableGlobal Variable
Global ScopeGlobal Scope
Anything identified or declared outside of Anything identified or declared outside of any function is visible to all functions in that any function is visible to all functions in that file file
Function level scopeFunction level scope
Declaring variables inside a function can be Declaring variables inside a function can be used in the whole functionused in the whole function
Block level scopeBlock level scope
Variables or integers declared inside block Variables or integers declared inside block are used inside blockare used inside block
Visibility of Visibility of IdentifiersIdentifiers
for ( for ( int iint i = 0 ; i < 10 ; i++ ) = 0 ; i < 10 ; i++ )
It is block level scope declared in It is block level scope declared in forfor loop loop
When When forfor is finished “ i ” no is finished “ i ” no longer existslonger exists
Example: Block Example: Block ScopeScope
#include < iostream.h >#include < iostream.h >
int i ; int i ;
void f ( void ) ;void f ( void ) ;
main ( )main ( )
{{
i = 10 ;i = 10 ;
cout<< “ within main i = “ cout<< “ within main i = “ << i ;<< i ;
f ( ) ;f ( ) ;
}}
Example: Global ScopeExample: Global Scope
void f ( void )void f ( void )
{{
cout<< “ Inside function f , i =“ cout<< “ Inside function f , i =“ << i ;<< i ;
i = 20 ;i = 20 ;
}}
Example: Global ScopeExample: Global Scope
#include <iostream.h >#include <iostream.h >int f ( int ) ;int f ( int ) ;main ( )main ( ){{
int i = 10 ;int i = 10 ;cout << “In main i = " << i ;cout << “In main i = " << i ;f ( i ) ;f ( i ) ;cout << " Back in main, i = " cout << " Back in main, i = "
<< i ;<< i ;}}
Example: Call by ValueExample: Call by Value
s
int f ( int i )int f ( int i )
{ {
cout << "In function f , i = " << i ;cout << "In function f , i = " << i ;
i *= 2 ;i *= 2 ;
cout << "In function f , i is now = “ cout << "In function f , i is now = “ << i ;<< i ;
return i ;return i ;
}}
Example: Call by Example: Call by ValueValue
double square ( double x ) double square ( double x ) {{
return x * x ;return x * x ;}}main ( )main ( ){{
double number = 123.456 ;double number = 123.456 ;cout << “ The square of “ << number << “ is “<< square cout << “ The square of “ << number << “ is “<< square
( number ) ; ( number ) ; cout << “ The current value of “ << number << “is “ << cout << “ The current value of “ << number << “is “ <<
number ;number ;} }
Example : Square of a Example : Square of a NumberNumber
#include < math.h >#include < math.h >
double sqrt ( double );double sqrt ( double );
loglog10 10 , pow ( x, pow ( xy y ) , sin , cos , tan ) , sin , cos , tan ……
Math.hMath.h
A function in which original value of A function in which original value of the variable is changed the variable is changed
To call by reference we cannot pass To call by reference we cannot pass value, we have to pass memory value, we have to pass memory address of variableaddress of variable
““&” is used to take the address of a &” is used to take the address of a variable variable
Call by ReferenceCall by Reference
main ( ) main ( ) {{
double x = 123.456 ;double x = 123.456 ;square ( &x ) ;square ( &x ) ;
}}Value of ‘x’ is not passed , but the Value of ‘x’ is not passed , but the memory address of ‘x’ is passedmemory address of ‘x’ is passed
Example: Call by Example: Call by ReferenceReference
square ( double *x )square ( double *x )
{{
*x = *x * *x ;*x = *x * *x ;
}}
Example: Call by Example: Call by ReferenceReference
x is a pointer to a variable double
Pointers are used to pass address Pointers are used to pass address of variable for reference of variable for reference
We use “ &x ” to send the address We use “ &x ” to send the address of “ x “ of “ x “
To receive the address we use “ *x To receive the address we use “ *x ” ” (whatever “ x ” points to)(whatever “ x ” points to)
PointersPointers
Special function which can call itselfSpecial function which can call itself
xx1010 = x * x = x * x99
xx99 = x * x = x * x88
xx88 = x * x = x * x77
… … … …
xxnn = x * x = x * xn-1n-1
Recursive Recursive FunctionsFunctions
n! = n * (n-1) * (n-2) …….. 3 * 2 * n! = n * (n-1) * (n-2) …….. 3 * 2 * 11
5! = 5 * 4 * 3 * 2 * 15! = 5 * 4 * 3 * 2 * 1
4! = 4 * 3 * 2 * 14! = 4 * 3 * 2 * 1
5! = 5 * 4!5! = 5 * 4!
0! = 10! = 1
Recursive Functions: Recursive Functions: FactorialFactorial
long factorial ( long n )long factorial ( long n ){{
if (n == 1 )if (n == 1 )return ( n ) ;return ( n ) ;
else else return ( n * factorial (n-1) ) ;return ( n * factorial (n-1) ) ;
}}
Recursive Functions: Recursive Functions: FactorialFactorial
Try to write program for Try to write program for
Fibonacci seriesFibonacci series
Find ‘power of number’ using Find ‘power of number’ using recursive techniquerecursive technique
ExerciseExercise
Example Example The Fibonacci SeriesThe Fibonacci Series
Set of recursive calls to function Set of recursive calls to function fibonaccifibonacci
f( 3 )
f( 1 )f( 2 )
f( 1 ) f( 0 ) return 1
return 1 return 0
return +
+return
There are two issues inside a There are two issues inside a computercomputer
Memory overheadMemory overhead
Stack overheadStack overhead
Management Issues of Management Issues of ComputerComputer
Elegant codeElegant codewhere price is not too where price is not too highhigh
Efficient codeEfficient codewhere price is too highwhere price is too high
Programming Programming OptionsOptions
Header FilesHeader Files– Nice mechanism of putting all Nice mechanism of putting all
prototypes and definitions of global prototypes and definitions of global constants etc.constants etc.
Scope of variablesScope of variables FunctionsFunctions
– Call by valueCall by value– Call by referenceCall by reference
RecursionRecursion
What have we Done Today What have we Done Today ……