cs201- introduction to programming- lecture 09
DESCRIPTION
Virtual University Course CS201- Introduction to Programming Lecture No 09 Instructor's Name: Dr. Naveed A. Malik Course Email: [email protected]TRANSCRIPT
![Page 1: CS201- Introduction to Programming- Lecture 09](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e783b479596e4f8b461a/html5/thumbnails/1.jpg)
Introduction to ProgrammingIntroduction to Programming
Lecture 9Lecture 9
![Page 2: CS201- Introduction to Programming- Lecture 09](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e783b479596e4f8b461a/html5/thumbnails/2.jpg)
DecisionsDecisions LoopsLoops SequencesSequences
Programming Programming ToolkitToolkit
![Page 3: CS201- Introduction to Programming- Lecture 09](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e783b479596e4f8b461a/html5/thumbnails/3.jpg)
Laboratory StoolLaboratory Stool
![Page 4: CS201- Introduction to Programming- Lecture 09](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e783b479596e4f8b461a/html5/thumbnails/4.jpg)
Constructing a laboratory Constructing a laboratory StoolStool
![Page 5: CS201- Introduction to Programming- Lecture 09](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e783b479596e4f8b461a/html5/thumbnails/5.jpg)
Constructing a Constructing a laboratory Stoollaboratory Stool
Task: Making a Task: Making a stoolstool– Subtask:Subtask:
Make a seatMake a seat Make legs for the Make legs for the stoolstool
Assemble themAssemble them
![Page 6: CS201- Introduction to Programming- Lecture 09](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e783b479596e4f8b461a/html5/thumbnails/6.jpg)
What are functions?What are functions? How are they defined ?How are they defined ? How are they declared ?How are they declared ? What values are passed to What values are passed to
functions ?functions ? What values do functions return ?What values do functions return ?
What we will study today What we will study today ……
![Page 7: CS201- Introduction to Programming- Lecture 09](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e783b479596e4f8b461a/html5/thumbnails/7.jpg)
FunctionFunctionFunction nameFunction name
{{
Body of the Body of the functionfunction
}}
![Page 8: CS201- Introduction to Programming- Lecture 09](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e783b479596e4f8b461a/html5/thumbnails/8.jpg)
FunctionFunction
Two types of functions: Two types of functions:
1.1. Functions that return a valueFunctions that return a value
2.2. Functions that do not return a Functions that do not return a valuevalue
![Page 9: CS201- Introduction to Programming- Lecture 09](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e783b479596e4f8b461a/html5/thumbnails/9.jpg)
return-value-type function-name( argument-return-value-type function-name( argument-list )list ){{ declarations and statements declarations and statements}}
Function
![Page 10: CS201- Introduction to Programming- Lecture 09](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e783b479596e4f8b461a/html5/thumbnails/10.jpg)
return-value-type function-name( argument--return-value-type function-name( argument--type-list) ; type-list) ;
main ( )main ( ){{
::}}
Declaration of Function
![Page 11: CS201- Introduction to Programming- Lecture 09](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e783b479596e4f8b461a/html5/thumbnails/11.jpg)
ExampleExampleint fint function-nameunction-name ( int , int , double ( int , int , double
) ; ) ;
void main ( ) void main ( )
{{
……..
}}
![Page 12: CS201- Introduction to Programming- Lecture 09](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e783b479596e4f8b461a/html5/thumbnails/12.jpg)
int fint function-nameunction-name ( int i , double j ) ( int i , double j )
{{
……
}}
Definition of Function
![Page 13: CS201- Introduction to Programming- Lecture 09](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e783b479596e4f8b461a/html5/thumbnails/13.jpg)
int square ( int ) ;int square ( int ) ;
Return Type of Function
int square ( int i )int square ( int i )
{{
return ( i * i ) ;return ( i * i ) ;
}}
Declaration
Definition
![Page 14: CS201- Introduction to Programming- Lecture 09](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e783b479596e4f8b461a/html5/thumbnails/14.jpg)
int x ;int x ;
x = square ( i ) ;x = square ( i ) ;
Function Call
![Page 15: CS201- Introduction to Programming- Lecture 09](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e783b479596e4f8b461a/html5/thumbnails/15.jpg)
double raiseToPow ( double x , int power )double raiseToPow ( double x , int power ){{
double result ;double result ;int i ;int i ;result = 1.0 ;result = 1.0 ;for ( i = 1 ; i <= power ; i ++ ) // braces for ( i = 1 ; i <= power ; i ++ ) // braces
firstfirst{{
result * = x ; result * = x ; // result = result *x// result = result *x}}return ( result ) ;return ( result ) ;
}}
Example: Function to calculate integer power ( Xn )
![Page 16: CS201- Introduction to Programming- Lecture 09](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e783b479596e4f8b461a/html5/thumbnails/16.jpg)
include < iostream.h >include < iostream.h >void main ( ) void main ( ) {{
double x ;double x ;int i ;int i ;cout << “ Please enter the number “ ;cout << “ Please enter the number “ ;cin >> x ;cin >> x ;cout << “ Please enter the integer power that you want this number cout << “ Please enter the integer power that you want this number raised to “ ;raised to “ ;cin >> i ;cin >> i ;cout << x << “ raise to power “ << i << “is equal to “ << raiseToPow cout << x << “ raise to power “ << i << “is equal to “ << raiseToPow ( x , i ) ; ( x , i ) ;
}}
Code to Call the raisetopow Function
![Page 17: CS201- Introduction to Programming- Lecture 09](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e783b479596e4f8b461a/html5/thumbnails/17.jpg)
Call By ValueCall By Value
![Page 18: CS201- Introduction to Programming- Lecture 09](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e783b479596e4f8b461a/html5/thumbnails/18.jpg)
Calling Calling functionfunction
Called functionCalled function
![Page 19: CS201- Introduction to Programming- Lecture 09](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e783b479596e4f8b461a/html5/thumbnails/19.jpg)
Area of the RingArea of the Ring
Outer Circle
Inner Circle
____ Area of Inner CircleArea of Outer Circle = Area of the Ring
![Page 20: CS201- Introduction to Programming- Lecture 09](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e783b479596e4f8b461a/html5/thumbnails/20.jpg)
double circleArea ( double radius )double circleArea ( double radius )
{{
return ( 3.1415926 * radius * return ( 3.1415926 * radius * radius ) ;radius ) ;
}}
Example: Function to calculate the area of a circle
![Page 21: CS201- Introduction to Programming- Lecture 09](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e783b479596e4f8b461a/html5/thumbnails/21.jpg)
main ( )main ( )
{{
::ringArea = ( 3.1415926 * rad1 * rad1 ) – ( 3.1415926 * rad2 * ringArea = ( 3.1415926 * rad1 * rad1 ) – ( 3.1415926 * rad2 * rad2 ) ;rad2 ) ;
}}
Calculating ringArea without using Function
![Page 22: CS201- Introduction to Programming- Lecture 09](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e783b479596e4f8b461a/html5/thumbnails/22.jpg)
ExercisesExercises1.1. Modify the Modify the raise to powerraise to power
function so that it can handle function so that it can handle negative power of x, zero and negative power of x, zero and positive power of x.positive power of x.
2.2. For the For the area of ringarea of ring function function put in error checking put in error checking mechanism. mechanism.
![Page 23: CS201- Introduction to Programming- Lecture 09](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e783b479596e4f8b461a/html5/thumbnails/23.jpg)
We used functions for breaking complex problems We used functions for breaking complex problems into smaller pieces, which is a top-down structured into smaller pieces, which is a top-down structured approach.approach.
Each function should be a small module, self Each function should be a small module, self contained and it should solve a well defined problem.contained and it should solve a well defined problem.
Variable names and function names should be self Variable names and function names should be self explanatory.explanatory.
Always comment your codeAlways comment your code
In today’s In today’s lecturelecture