Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming Fundamental

Similar presentations


Presentation on theme: "Programming Fundamental"— Presentation transcript:

1 Programming Fundamental
Instructor Name: Muhammad Safyan Lecture-11

2 Lecture outline Unconditional Statement (goto) Function

3 Unconditional Branch of Execution
goto Unconditional Branch of Execution

4 int i=0; while (i<=10) { goto a; cout<<"GCU"<<endl; i=i+1; } a: cout<<"CSD";

5 Minimize the use of break
Advice Minimize the use of break Minimize the use of continue Never use goto

6 What have we done till now …
Sequential Statements Decisions if , if else , switch Loops while , do while , for

7 Functions

8 Guide lines for structured programming
Modular Single entry - single exit

9 Laboratory Stool

10 Constructing a laboratory Stool

11 Constructing a laboratory Stool
Task: Making a stool Subtask: Make a seat Make legs for the stool Assemble them

12 What we will study today …
What are functions? How are they declared ? How are they defined ? What values are passed to functions ? What values do functions return ?

13 Function(Objective) Function: A piece of code that perform a specific task Divide the Task into sub-Tasks Divide and conquer approach Better Understanding Information Hiding Reusability

14 Function Steps involve in functions: Declare a function
Define a function Call a function

15 Declaration of fuction
void function-name( ) ; main ( ) { : }

16 Define a Function void function-name( ) { declarations and statements }

17 call a Function function-name( );

18 // Function Declaration
void pak(); void india(); main() { cout<<" im in main"<<endl; // Call Function pak(); india(); getch(); } // Function Definition void pak() cout<<" i am in pakistan"<<endl;; void india() cout<<" i am in india";

19 Function(in Detail) Two types of functions:
Functions that do not return a value Functions that return a value

20 Function return-value-type function-name( argument-list ) { declarations and statements }

21 Declaration of Function
return-value-type function-name( argument--type-list) ; main ( ) { : }

22 Example int function-name ( int , int , double ) ; void main ( ) { ….
}

23 Definition of Function
int function-name ( int i ,int k, double j ) { }

24 Return Type of Function
Declaration int square ( int ) ; Definition int square ( int i ) { return ( i * i ) ; }

25 Function Call int x ; x = square ( i ) ;

26 Example: Function to calculate integer power ( Xn )
double raiseToPow ( double x , int power ) { double result ; int i ; result = 1.0 ; for ( i = 1 ; i <= power ; i ++ ) // braces first result * = x ; // result = result *x } return ( result ) ;

27 Code to Call the raisetopow Function
include < iostream.h > void main ( ) { double x ; int i ; cout << “ Please enter the number “ ; cin >> x ; cout << “ Please enter the integer power that you want this number raised to “ ; cin >> i ; cout << x << “ raise to power “ << i << “is equal to “ << raiseToPow ( x , i ) ; }

28 Calling function Called function

29 Area of the Ring Area of Outer Circle = Area of the Ring

30 Calculating ringArea without using Function
main ( ) { : ringArea = ( * rad1 * rad1 ) – ( * rad2 * rad2 ) ; }

31 Example: Function to calculate the area of a circle
(with Return Value)

32 Example: Function to calculate the area of a circle
(Without Return a value)


Download ppt "Programming Fundamental"

Similar presentations


Ads by Google