Professional Documents
Culture Documents
1 Ghalib University
Table of contents
1. Function
2. Different Types of Functions
1. Built-in Functions/ Library Functions
2. User Defined Functions
3. Passing Arguments to Functions
2 Ghalib University
Function
A function is a collection of statements that performs a specific task.
Functions are commonly used to break a problem down into small manageable
parts.
Instead of writing one long function that contains all of the statements
necessary to solve a problem, several small functions that each solve a specific
part of the problem can be written.
3 Ghalib University
Different Types of Functions
1) Built-in Functions/ Library Functions
4 Ghalib University
Built-in Functions/ Library Functions
The functions that have already been defined as part of the language
For Example, <cmath>, <string> are the headers that have in-built
5 Ghalib University
(2)- User Defined Function
The function is written by a users for solving their problems are
1. Function Declaration(Prototype)
2. Function Definition
3. Function Calling
6 Ghalib University
1-Function Declaration
The model of a function is called function declaration or function
prototype.
compiler.
7 Ghalib University
Example of function Declaration
#include<iostream>
using namespace std;
void show(void);
int main(){
return 0;
}
8 Ghalib University
2-Function Definition
The actual code of a function is called function definition.
We will write the function definition outside from main() function.
The general form of a user define function is.
Return_Type Function_Name(Optional list of Parameters) //
Declarator
{
//function body;
9 Ghalib University
Example of function Definition
#include<iostream>
using namespace std;
void show(void);
int main(){
return 0;
}
void show()
{
cout<<"hello";
}
10 Ghalib University
3-Function Calling
A function call is a statement that causes a function to execute.
Once we declare and define a function then we can use that function by
function calling.
11 Ghalib University
Example of function calling
#include<iostream>
using namespace std;
void show(void);
int main(){
show();
return 0;
}
void show()
{
cout<<"hello";
}
12 Ghalib University
Passing Arguments to Functions
If a function needs data to perform a specific task, this data is provided
constants or variables.
They are written in the same sequence in which they are defined in the
function declaration.
The data type of an argument in the function call must also match the
13 Ghalib University
Example
#include<iostream>
using namespace std;
void sum (int ,int);
int main(){
sum(10,20);
return 0;
}void sum(int a,int b){
int s;
s=a+b;
cout<<"Sum="<<s<<endl;
}
14 Ghalib University
Any question…?????????????
15 Ghalib University