Professional Documents
Culture Documents
Group members
• Moaz Raza Khan (02-134212-057)
Introduction to functions in C++
• Saad Siddiqui (02-134212-084)
Pass By reference
• Ibtihaj Bin Azhar (02-134212-085)
Pass By Value
• Muhammad Furqan (02-134212-096)
How to pass value of array in
function
• Abdul Rehman Abdul Wahid (02-134212-051)
Method Overloading
Functions
• A function is a block of instructions that
together performs a specific task .
abs(int x);
cos(double x);
exp(double x);
log(double x);
sin(double x);
sqrt( ) #include <cmath> double Calculates the positive square root of the number.
sqrt(double x);
tan(double x);
User defined functions
• User defined functions are
defined by the programmer
for custom requirement.
• There are three main
actions associated with
User Defined Functions
handling in C++ language:
1. Function Prototype (Also
called Function
Declaration)
2. Function Definition.
3. Function Calling.
Example Program to calculate area
• #include<iostream>
• Using namespace std;
• Void area(); // function declaration or prototype telling that the name of function is area,
and the data type is void.
• Int main()
• {
• Area(); //Function calling in main
• }
• //Function definition
• Void area()//Function Header
• {
• Int a,b,c; //function Body
• C=0.5*a*b;
• Cout<<“the area is “<<c;
• }
Functions
• Function • Function definition Function calling
declaration • The function definition A user defined function
is the function itself. In
the definition the is called from the main
• The declaration of a
program defines the program simply by
function is called body of the function
comprised of using the name
Prototype.
programming including the
• statements. parenthesis which
The prototype tells the
compiler in advance • follows the name.
about some The function definition
characteristics of a has two parts:
function used in the
program. 1. Function Header
2. Function Body
Pass By Value
Means that a copy of data is made and stored by
way of the name of parameter. Any changes to the
parameter have NO effect in the calling function.
Program
Int add (int a , int b);
Void main(){
Int a=1 , b=2;
Cout << add (a,b); // OUTPUT
Cout << a << b;
} 02
2
12
Int add ( int a, int b ) {
a= 0;
Cout << a << b;
return a+b;
}
Pass By Reference
• Pass By Reference method of passing arguments to a
function copies the reference of an argument into the
parameter.
• The reference is used to access the actual argument used in
the call, inside the function.
• A reference parameter "refers" to the original data in the
calling function. Thus any changes made to the parameter
are also made to the original variable.
• The symbol “ &” is used in pass by reference
Example
Passing Value of Array In Function
OUTPUT:
What is Function Overloading?
• Function Overloading is a feature of object oriented programming
where two or more functions can have the same name but different
parameters, different return types and it can have a different
function body for each individual function if the user wants.
• CONDITIONS FOR FUNCTION OVERLOADING (IMPORTANT)
2. Different parameters
3. Sequence of parameters can be different
Examples