1
Course description
Course Code : CS-409
Course Title : Object Oriented Programming
Teacher Name : Dr. M. Milhan Afzal Khan
(Assistant Professor)
2
3
4
5
6
7
8
Function
9
Modular Programming
• Divide and Conquer.
• Chunks
• Group of statements
• Manageable and Testable.
• Best for big programs.
10
Importance of function
1. Low LOC.
2. Code duplication is avoided.
3. Code reusability .
4. Repetitively Called.
5. Modifiability.
6. Debugging.
11
Types of Functions
Function
Build in User Defined
Function Function
12
Types of Functions Cont..
• Build-in Functions ( Library Functions)
1. They are stored in different header files.
2. They are made available by compiler.
3. They Can be used in any program by including respective header
file.
14
Types of Functions Cont..
• User-defined Functions
1. Created by user or programmer.
2. Created as per requirement of the program.
3. e.g car_price, sum, cgpa etc
15
User-defined Functions
• Function declaration. (function prototype)
• Function definition.
• Function call.
• Return statement and return value
16
Function Declaration.
• It provide information regarding the structure of the function.
• It ends with a semicolon.
• They are usually placed before the main() function.
17
Function Declaration Cont..
• Parts of Function declaration
• Function return type
• Function name
• Number of parameter( values on which function perform its
functionality)
• Type of parameters.
18
Function Declaration Cont..
• Syntax
return_ type function_name ( parameters or arguments );
int pakistan ( int a , int b ) ;
19
Function Declaration Cont..
• void pakistan ( void );
• void pakistan (float);
• int add ( c , y);
• int subtract ( float , float );
• Long SSG ( int , float , double );
20
Function Declaration Cont..
#include<iostream>
using namespace std;
void pakistan (void );
main ()
{
} 21
pakistan();
}
void pakistan(void)
{
cout<<“one”;
22
Function Definition
• Set of statement that explain the execution of the function.
• No semicolon .
23
Where we can write function definition
• Before main function.
• After main function.
• In a separate file.
24
Function Definition Cont..
• Parts of Function Definition
Function Header.
Function Body.
25
Function Definition Cont..
• Function Header.
First line is termed as Function Header.
e.g int pakistan ( int a )
Similar to prototype, except
Semicolon is missing at the end .
The number and sequence of parameters must be same.
26
Function Definition Cont..
• Function Body
• The statements to be executed are written here.
• Written in curly brackets.
• It follows the function header part.
27
Syntax of Function Definition
return_ type function_name ( parameters or arguments ) //function header
return expression;
}
28
Return statement and Return values
• It is used to exit from the function definition body.
• It returns a value to the calling code.
• The general form of the return statement is.
• return expression;
29
Function call
• The statement that calls the function definition.
• A function must be called by its name followed by argument list
enclosed in semicolon.
• Called from main() function.
30
Passing parameters to Functions
1. The values given at the time of function call.
2. Written in parenthesis.
3. The sequence and type of parameters should be matched with
function prototype.
4. Actual parameters are given in function call.
5. Formal parameters are given in function header.
31
Pass by Value
• Value of actual parameters is copied to formal parameters.
• It is the default mechanism of passing parameters.
32
Function overloading
Similar name function having different parameter.
Should be differ in one of the following ways.
1. Number of Parameters
2. Type of Parameters
3. Sequence of Parameters
33
Function overloading
int marks ( int , int );
int marks ( float , int );
int marks ( int , float );
34