Functions in C++
Introduction
In our daily life , most of the complex works are done by a group of people.
This results in better productivity and quality.
Work is done at a great speed.
Normally, big work is distributed among many people.
For Example:
To declare the result of examination in an autonomous institute, marks/score of all
subjects, practical, seminar, projects etc., are collected from different departments
through professors and the result is compiled, declared, and displayed.
The same concept is applied while developing complex program/software applications
using programming languages.
In case the size of the program also hard to identify the flow of data.
It preferred to divide the large program into small modules called functions.
Each function takes the data that are provided by the main () function and carries out
operations as per the requirement, and results can be written to the calling function.
Thus, programs are made more efficient.
When the concept of function or sub-program was not introduced, the program sizes
were large and few codes get repeated as shown in the below figure
int main()
{
statement-1;
statement-2;
statement-3;
statement-4; Large Program
statement-5;
statement-6
return 0;
}
Program without function
The above concept was very difficult to debug and update these large programs because
many bugs get encountered.
When the functions and sub-programs are introduced , the programs are divided into a
number of segments and code duplication is avoided as shown in the below figure
Sum1()
{
Statement-2;
void main() Statement-3;
{ Statement-4;
}
statement-1;
sum1();
sum2();
Sum2()
{
Statement-5;
Statement-6;
Statement-7;
}
Program with function
Bugs in small programs can be searched easily and this step leads to the development of
complex programs with functions.
FUNCTION
A function is a block of code which only runs when it is called.
You can pass data, known as parameters, into a function.
Functions are used to perform certain actions, and they are important for reusing code:
Define the code once, and use it many times.
A function is a set of statements that takes input, does some specific computation, and
produces output.
The idea is to put some commonly or repeatedly done tasks together to make
a function so that instead of writing the same code again and again for different inputs,
we can call this function.
In simple terms, a function is a block of code that runs only when it is called.
The function in C++ language is also known as procedure or subroutine in other
programming languages.
Advantages of Functions
1. Reusability: A function once written can be invoked again and again , thus helping us to
reuse the code and removing data redundancy.
2. Modularity: Function can help us in breaking a large , hard to manage problem into
smaller manageable sub-problems. It is easier to understand the logic of sub-programs.
3. Reduced program size: function can reduce the size of the program by removing data
redundancy.
4. Easy Debugging: using functions , debugging of a program becomes very easy , as it is
easier to locate and rectify the bug in the program if functions are used.
5. Easy updating: if we need to update some code in the program , then it is much more
easier in case we have used functions , as the changes need to be made in one place
only.
Part of functions
1. Function prototype declaration
2. Function call
3. Definition of a function
4. Actual and formal arguments
5. Return statement.
int sum(int a,int b); Function prototype
void main()
{
clrscr(); Actual Arguments
int x=5,y=2,z;
z=sum(x,y);
getch();
}
int sum(int a,int b) Function call
{
return (a+b); Formal Arguments
}
Function definition
Return statement
Function prototype
We use many built-in-functions. The prototype of these functions are given in the
respective header files, and we include them using #include directive.
In C++ , while defining user-define function it is unavoidable to declare its prototype.
A prototype statement helps the compiler to check the return and argument type of the
function
A function prototype declaration consists of function’s return type , name and
arguments list. It tells the compiler.
o Name of the function
o Type of value returned
o The type and number of arguments
Function call
It gets activated only when a call to a function is invoked.
A function must be called by its name followed by argument, or without argument , list
enclosed in parenthesis and terminated by semicolon.
Syntax :
Function-name (with/without argument-list);
Function definition
The first line is called function definition and function body follows it
The function definition and function prototype should match with each other.
The function body is enclosed within curly braces.
The function can be defined anywhere.
If the function is defined before its caller, the its prototype declaration is optional.
Syntax :
Return_data_Type function-name(argument/parameter-list)
{
Variable_declaration;
Function_statement;
}
Actual and formal argument
The arguments declared in caller function and given in the function call are called actual
arguments.
The arguments declared in the function definition are known as formal arguments.
void main()
{
X=sum( y, z); Actual Argument
}
float sum( float j, int k) Formal Argument
{
return (j+k);
}
In above example variables y and z are actual arguments and variables j and k are formal
arguments.
The value of y and z are stored in j and k respectively.
The values of actual arguments are assigned to formal arguments. The function uses
formal arguments for computing.
Return statement
The return statement is used to return value to the caller function.
The return statement returns only one value at a time.
When return statements is encountered , compiler transfers the control of the program
to caller function,
Syntax
Return (variable name); (or) return variable name;
Types of Functions
There are two types of functions in C programming:
1. Library Functions: are the functions which are declared in the C++ header files such as
print(),scanf(),clrscr(),getch(), etc.
2. User-defined functions: are the functions which are created by the C++ programmer, so
that he/she can use it many times. It reduces complexity of a big program and optimizes
the code.
Function Categories
1. Without arguments, without return
2. With arguments , without return
3. Without arguments , with return
4. With arguments , with return
Type-1: without arguments , without return
#include<iostream.h>
#include<conio.h>
void summation() // function definition area
{
int a,b,c;
cout<<"\n Enter A and B for Addition:";
cin>>a>>b;
c=a+b;
cout<<"\n Addition is:"<<c;
}
void subtraction()
{
float x,y,z;
cout<<"\n Enter X and Y for subtraction:";
cin>>x>>y;
z=x-y;
cout<<"\n subtraction is:"<<z;
}
void main()
{
clrscr();
summation(); // calling user define function
subtraction();
getch();
}
OUTPUT
Enter A and B for Addition: 5 2
Addition is : 7
Enter X and Y for subtraction: 5 2
Subtraction is : 3
Type-2: with arguments , without return
#include<iostream.h>
#include<conio.h>
void transfer(int x) // here variable 'x' will receive the value from 's'
{
int y;
y=4*x;
cout<<"\n Area of square is :"<<y;
}
void main()
{
clrscr();
int s;
cout<<"\n Enter Number of sides:";
cin>>s;
transfer(s); // calling with 1-argument
getch();
}
OUTPUT
Enter Number of sides: 5
Area of square is: 20
Type-3: without argument, with return
// without argument, with return
#include<iostream.h>
#include<conio.h>
float area()
{
float b,h,ans;
cout<<"\n Enter breadth of the triangle:";
cin>>b;
cout<<"\n Enter height of the triangle:";
cin>>h;
ans=0.5*b*h;
return ans; // here we are return the final result to main function
}
void main()
{
clrscr();
float a;
a=area(); // calling without argument
// in above line variable 'a' will receive the final result from variable 'ans'
cout<<"\n Area of the triangle is:"<<a;
getch();
}
OUTPUT
Enter breadth of the triangle: 5
Enter height of the triangle: 3
Area of the triangle is: 7.5
Type-4: with Argument, with return
#include<iostream.h>
#include<conio.h>
int eligible(int e) //here variable 'res' will receive the value from 'uage'
{
if(e>=18)
return 1; // true statement
else
return 0; // false statement
}
void main()
{
clrscr();
char uname[40];
int uage,res;
cout<<"\n Enter User name:";
cin>>uname;
cout<<"\n Enter User age:";
cin>>uage;
res=eligible(uage); // calling with one argument
//in above line variable 'res' will receive the value 1(or) 0 from 'e'
if(res==1)
{
cout<<uname<<" U are Eligible to vote";
}
else
{
cout<<uname<<" U are not eligible to vote";
}
getch();
}
OUTPUT
Enter User name: Krishnan
Enter User age: 34
Krishnans U are Eligible to vote