Professional Documents
Culture Documents
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;
}
#include<iostream>
using namespace std;
int min(int x , int y); Function Prototype
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
• Pass-by-Reference
Address of the variable is passed; the change
will reflect on the variable used to call the
function.
Pass By Value