cs201- introduction to programming- lecture 09

23
Introduction to Programmin Introduction to Programmin Lecture 9 Lecture 9

Upload: bilal-ahmed

Post on 18-Dec-2014

27 views

Category:

Education


3 download

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

Introduction to ProgrammingIntroduction to Programming

Lecture 9Lecture 9

Page 2: CS201- Introduction to Programming- Lecture 09

DecisionsDecisions LoopsLoops SequencesSequences

Programming Programming ToolkitToolkit

Page 3: CS201- Introduction to Programming- Lecture 09

Laboratory StoolLaboratory Stool

Page 4: CS201- Introduction to Programming- Lecture 09

Constructing a laboratory Constructing a laboratory StoolStool

Page 5: CS201- Introduction to Programming- Lecture 09

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

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

FunctionFunctionFunction nameFunction name

{{

Body of the Body of the functionfunction

}}

Page 8: CS201- Introduction to Programming- Lecture 09

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

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

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

ExampleExampleint fint function-nameunction-name ( int , int , double ( int , int , double

) ; ) ;

void main ( ) void main ( )

{{

……..

}}

Page 12: CS201- Introduction to Programming- Lecture 09

int fint function-nameunction-name ( int i , double j ) ( int i , double j )

{{

……

}}

Definition of Function

Page 13: CS201- Introduction to Programming- Lecture 09

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

int x ;int x ;

x = square ( i ) ;x = square ( i ) ;

Function Call

Page 15: CS201- Introduction to Programming- Lecture 09

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

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

Call By ValueCall By Value

Page 18: CS201- Introduction to Programming- Lecture 09

Calling Calling functionfunction

Called functionCalled function

Page 19: CS201- Introduction to Programming- Lecture 09

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

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

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

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

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