You are on page 1of 16

CHAPTER 4

FUNCTIONS
Aim & Objectives

Aim
 To learn writing program in C++ using function.

Objectives
 To understand the function concepts
 To learn about function declaration and definition
 To understand the two ways of passing parameters to function (by
value, by reference)
 To gain knowledge about recursion
What is function

 A function is sub program that is executed when it is called from some point
of the program.
 With the help of functions, large computing tasks can be divided in
doable smaller ones.
 Every C++ program has main() function.
 The Standard Library Functions are divided into several header files.
 The function must be declared before it is used.
 C++ function is classified into two categories
 Standard Library functions.
Example : main(),cout(),cin(), strlen(),etc
 User-defined functions.
Example : addition(),max ()
In C++ , function can be defined using below template:
return type function name ( function parameter list)
{
// body of function;
}

• Return type: valid C++ variable type.


• Function name: valid C++ identifier.
• Parameter: list of variable types followed by variable name.

• Note: multiple parameters are separated by comma.

Together Towards A Green Environment


User defined function
 A function declaration is usually declared at the top of a C++
source file

 Function Prototype or Function declaration


return_type function_name(list of parameter);
Example: int max(int n1, int n2);

 The function definition consists of the prototype and a function


body, which is a block of code enclosed in parenthesis.
return_type function_name(list of parameter)
Example: int max(int n1, int n2)
Function – To find the minimum among two numbers

#include<iostream>
using namespace std;
int min(int x , int y); Function Prototype

int min(int x , int y)


{
if(x < y)
return x; Function definition (body)
else
return y;
}

int main()
{
int i=5, j=7,s;
s=min(i,j); Calling Function
cout << “Smallest of two numbers ” << s;
return 0;
Algorithm for finding minimum of two numbers
using function

Main
1. Start
2. Declare i , j &s as integers.
3. Call the function min(i,j) and store the return value in s
4. Display the minimum value, s
5. Stop
 
Function min(int x, int y)
1. if x< y then go to step 2 else go to step 3
2. return x , go to step 4
3. return y
4. return to main
Flowchart for the finding minimum of two numbers us
function
Addition of two numbers

Function with arguments Function without arguments


#include <iostream>
#include <iostream>
using namespace std;
using namespace std;
int addition ();
int addition (int a, int b);
int addition ()
int addition (int a, int b)
{
{
int a,b,r;
int r;
cin >> a >> b;
r=a+b;
r=a+b;
return (r);
return (r);
}
}
int main ()
int main ()
{
{
int sum;
int sum;
sum=addition (5,3);
sum=addition ();
cout << “The sum is “<< sum;
cout << “The sum is “<< sum;
return 0;
return 0;
}
}
Passing arguments to function
• Pass-by-Value
A copy of the variable is passed to the function,
the change will not affect the variables used to
call the function

• Pass-by-Reference
Address of the variable is passed; the change
will reflect on the variable used to call the
function.
Pass By Value

 When arguments are passed by value, the called


function creates a new variable of the same type as the
argument and copies the arguments value into it.
 The function does not have access to the original
variable in the calling program, only the copy of it
is created.
 Passing arguments by values is useful when the
function does not need to modify the original variable
in the calling program.
 The function cannot harm the original variable.
 Only one value is returned to the Calling program.
Example for Pass by value
Pass By Reference

 A reference provides an alias – a different name – for a


variable.
 Instead of a value being passed to the function, a reference to
the original variable, in the calling program is passed.
 One advantage of passing by reference is that the function can
access the actual variables in the calling program.
 This provides a mechanism for passing more than one value
from the function back to the calling program.
Example: Pass by reference
Function recursion:
• When a function calls itself is known as recursion.
• The popular example to understand the recursion is factorial
function.
C++ recursion example:

You might also like