You are on page 1of 18

FUNCTIONS In C++

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 .

• A function is the fundamental element of


Modular Programming and Top down
Programming approach.

• A function is a block of instructions that is executed


when it is called from some other point of the
program.
Advantages of Functions
• The complexity of entire program can be divided into simple
subtasks and function subprograms can be written for each
subtask.

• Function help to avoid unnecessary repetition of code.


Using function a single section of code can be used many
times in the same program.

• The functions are short, easier to write, understand and


debug.

• A function can be shared by other programs by compiling it


separately and loading them together.
Classification of Functions
• Built-in functions/ Library functions:

• These functions are pre-defined by the vendors for


solving general programming problems. Such as
abs(), cos() etc.
• User defined function:

• These functions are defined by the programmer for


custom requirement Such as addition(), getdata()
etc.
Common Mathematical Functions
Function Syntax Description
abs( ) #include <cstdlib> int Returns the absolute value of an integer.

abs(int x);

cos( ) #include <cmath> double calculates the cosine of a value.

cos(double x);

exp( ) #include <cmath> double Calculates the exponential e to the x.

exp(double x);

log( ) #include <cmath> double Calculates the natural logarithm of x.

log(double x);

pow( ) #include <cmath> double Calculates x to the power of y.

pow(double x, double y);

sin( ) #include <cmath> double Calculates the sine of a value.

sin(double x);

sqrt( ) #include <cmath> double Calculates the positive square root of the number.

sqrt(double x);

tan( ) #include <cmath> double Calculates the tangent of a value.

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)

1. Same function name

2. Different parameters
3. Sequence of parameters can be different
Examples

In the above shown example,


In the above shown example, all the four functions the code would give an error
are overloaded. as the parameter list is the
• They have different return types. same, this is not allowed.
• They take in different parameters. Even though their return
• BUT they share the same name. types are different, it’s not
valid.
Function Overloading in action

• In the code shown:


• When the function is called, its called based
upon the parameters given to it in the
brackets.
• When display() is called the first time, the
third function body would run because the
data that is given to the function is of int
type(Integer).
• When display() is called the second time,
the second function body would run
because the data that is given to the
function is of double type.
• When display() is called the third time,
the first function body would run because
the data that is given to the function is of
int type and double type.

You might also like