Date post: | 18-Dec-2014 |
Category: |
Education |
Upload: | bilal-ahmed |
View: | 21 times |
Download: | 3 times |
Introduction to ProgrammingIntroduction to Programming
Lecture 9Lecture 9
DecisionsDecisions LoopsLoops SequencesSequences
Programming Programming ToolkitToolkit
Laboratory StoolLaboratory Stool
Constructing a laboratory Constructing a laboratory StoolStool
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
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 ……
FunctionFunctionFunction nameFunction name
{{
Body of the Body of the functionfunction
}}
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
return-value-type function-name( argument-return-value-type function-name( argument-list )list ){{ declarations and statements declarations and statements}}
Function
return-value-type function-name( argument--return-value-type function-name( argument--type-list) ; type-list) ;
main ( )main ( ){{
::}}
Declaration of Function
ExampleExampleint fint function-nameunction-name ( int , int , double ( int , int , double
) ; ) ;
void main ( ) void main ( )
{{
……..
}}
int fint function-nameunction-name ( int i , double j ) ( int i , double j )
{{
……
}}
Definition of Function
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
int x ;int x ;
x = square ( i ) ;x = square ( i ) ;
Function Call
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 )
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
Call By ValueCall By Value
Calling Calling functionfunction
Called functionCalled function
Area of the RingArea of the Ring
Outer Circle
Inner Circle
____ Area of Inner CircleArea of Outer Circle = Area of the Ring
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
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
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.
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