You are on page 1of 15

ISLAMIC REPUBLIC OF AFGHANISTAN

MINISTRY OF HIGHER EDUCATION


GHALIB UNIVERSITY
COMPUTER SCEINE FACULTY
2TH SEMESTER
Advance Programming Structure
LECTURER : Zabihullah Rahmani

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.

 Another reason to write functions is that they simplify programs. If a specific

task is performed in several places in a program, a function can be written once


to perform that task, and then be executed anytime it is needed.

3 Ghalib University
Different Types of Functions
1) Built-in Functions/ Library Functions

2) User Defined Functions

4 Ghalib University
Built-in Functions/ Library Functions
 The functions that have already been defined as part of the language

and can be used in any program.

 These function are provided for general use.

 These functions are placed in the header files of C++.

 For Example, <cmath>, <string> are the headers that have in-built

math functions and string functions respectively.

5 Ghalib University
(2)- User Defined Function
 The function is written by a users for solving their problems are

called user defined function.

 There are three parts of user define function.

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.

 In function declaration the following information is provided to the

compiler.

1) The name of function.

2) The type of data that will be returned by the function.

3) The types of arguments or parameters used in the function.

4) At the end of function declaration semicolon is required.

 The function declaration is similar to a variable declaration.

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

through the arguments of the function.

 The arguments are placed in parentheses. The arguments are either

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

corresponding data types in the function declaration.

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

You might also like