32
Introduction to Introduction to Programming Programming Lecture 10 Lecture 10

CS201- Introduction to Programming- Lecture 10

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

Page 1: CS201- Introduction to Programming- Lecture 10

Introduction to Introduction to ProgrammingProgramming

Lecture 10Lecture 10

Page 2: CS201- Introduction to Programming- Lecture 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

Page 3: CS201- Introduction to Programming- Lecture 10

#include <iostream.h>#include <iostream.h>

Header FilesHeader Files

Page 4: CS201- Introduction to Programming- Lecture 10

int functionName ( int , int functionName ( int , int );int );

PrototypePrototype

Return valueAssignment List with data type

Page 5: CS201- Introduction to Programming- Lecture 10

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

Page 6: CS201- Introduction to Programming- Lecture 10

#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

Page 7: CS201- Introduction to Programming- Lecture 10

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

Page 8: CS201- Introduction to Programming- Lecture 10

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

Page 9: CS201- Introduction to Programming- Lecture 10

ExampleExamplevoid functionName ( ) void functionName ( )

{{

{{

int i ; int i ;

}}

……....

}}

Page 10: CS201- Introduction to Programming- Lecture 10

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

Page 11: CS201- Introduction to Programming- Lecture 10

# include < iostream.h ># include < iostream.h > int i ;int i ;

File ScopeFile Scope

Global variable

Page 12: CS201- Introduction to Programming- Lecture 10

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

Page 13: CS201- Introduction to Programming- Lecture 10

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

Page 14: CS201- Introduction to Programming- Lecture 10

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

Page 15: CS201- Introduction to Programming- Lecture 10

#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

Page 16: CS201- Introduction to Programming- Lecture 10

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

Page 17: CS201- Introduction to Programming- Lecture 10

#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

Page 18: CS201- Introduction to Programming- Lecture 10

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

Page 19: CS201- Introduction to Programming- Lecture 10

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

Page 20: CS201- Introduction to Programming- Lecture 10

#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

Page 21: CS201- Introduction to Programming- Lecture 10

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

Page 22: CS201- Introduction to Programming- Lecture 10

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

Page 23: CS201- Introduction to Programming- Lecture 10

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

Page 24: CS201- Introduction to Programming- Lecture 10

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

Page 25: CS201- Introduction to Programming- Lecture 10

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

Page 26: CS201- Introduction to Programming- Lecture 10

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

Page 27: CS201- Introduction to Programming- Lecture 10

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

Page 28: CS201- Introduction to Programming- Lecture 10

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

Page 29: CS201- Introduction to Programming- Lecture 10

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

Page 30: CS201- Introduction to Programming- Lecture 10

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

Page 31: CS201- Introduction to Programming- Lecture 10

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

Page 32: CS201- Introduction to Programming- Lecture 10

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 ……