Functions allow programmers to organize code into reusable blocks. There are three main types of functions: library functions, user-defined functions, and main(). Functions can return values and take parameters. Parameters can be passed by value, reference using an alias, or reference using pointers. Passing by value copies the values, while passing by reference or pointer passes the actual arguments so any changes made in the function also change the original variables. Well-defined functions promote code reuse and modular programming.
1. AmritKaur Functions in C++
1
Chapter 5: Functions
A function is a named, independent section of C++ code that performs a specific task and
optionally returns a value to the calling program.
A function is a part of program called by another part of a program. In a program, there are
many calling function (caller) and called functions (callee).
5.1 Function Definition
Function Definition is the actual function body, which contains the code that will be
executed. Function definition must not end with semicolon. A function can be
defined as follows
Syntax:
returntype functionname (parameter(s))
{
//function body
}
Example 1: function definition to find average of 5 nos
float average(int a, int b,int c, int d, int e )
{
float avg;
avg=(a+b+c+d+e)/5.0f;
return (avg)
}
Here floatspecifiesreturntype; average is name of function; int a, int b, int c, int d , int e are
parameters
Example 2: function definition and function call
#include<iostream.h>
void func()
{
cout<<” IN FUNCTION func()”<<endl;
}
void main()
{
cout<<”IN FUNCTION main()”<<endl;
func();//call to a function
cout<<” BACK IN main() FUNCTION”<<endl;
}
2. AmritKaur Functions in C++
2
5.2 Function Prototype
A function must be defined before using it. That is, if the function definition does not
precedes the function call, the function need to declare. The process of declaring
function is known as function prototyping.
In other words, if programmer wants to define a function after using it they have to
declare the function at the top just after pre-processor directives. It includes
returntype , function name and paramterlist followed by semicolon.
Syntax: returntype functionname (paramtere(s));
Example 3 : float average(int a, int b,int c, int d, int e ) ;
5.3. Types of Function
In C++, the function are classified into types
Library Functions:
Library functions are pre defined and pre compiled functions present in a library file
and found in include directory with extension .h
Example 4:
a. sqrt(), pow() are available in header file math.h
b. strlen(), strcpy() are present in string.h
User defined function
It is small independent program designed by user for performing the specific
operations.
5.4 Elements of User defined Function
Return Types
Functions can return a value to the caller. When a called function sends the data
back to calling program it is known as return type. If the function doesn’t return a
value use void. Otherwise it can return one value by using the return keyword. The
value to be returned must match with the value of return type.
3. AmritKaur Functions in C++
3
Function Name
It is name of the function that is used while calling. It can be any valid identifier.
Parameters or Arguments
Passing a values during function call takes place through arguments. The argument(s)
or parameter(s) of a function is/are the data the function must receive when called
from another function. When a calling function sends the data to a called function,
data is known as parameters or arguments. Types of Arguments
Actual Parameters / Arguments : Arguments listed in function calling statement.
These are the values passed to function to compute.
Formal Parameters / Arguments : The arguments used in the function
declaration. They receive the values supplied by calling program.
Default Parameters / Arguments : When a default value is assigned to formal
arguments it is known as default arguments. Useful in case when argument is
not passed in call statement
Example 5: Program to find average of 5 nos
#include<iostream.h>
#include<conio.h>
float average (int a, int b, int c, int d, int e) // formal parameters
{
float avg;
avg=(a+b+c+d+e)/5.0;
return avg;
}
void main()
{ int a,b,c,d,e;
cout<<"Enter Five Numbers";
cin>>a>>b>>c>>d>>e;
float result=average(a,b,c,d,e); // actual parameters
cout<<"Avergare of "<<a<<" "<<b<<" "<<c<<" "<<d<<" "<<e<<" = "<<result;
}
5.5 Invoking Function
In C++, function can be invoked or called in one of the following ways
a. Call By Value
4. AmritKaur Functions in C++
4
b. Call By Reference
5.5.1 Call By Value
In Call by Value, the value of a parameter(s) is passed.
When parameters are passed by value, the called function creates the new variables
of same type and copies the argument values into it.
In call by value, the value of actual (original) parameter remain unchanged.
Example: Program to swap (interchange) two numbers using call by value.
#include<iostream.h>
#include<conio.h>
void swap(int p, int q) // p and q are formal arguments
{ // p is the copy of a and q is copy of b
int temp;
cout<<"nValue of p and q...... BEFORE swapping";
cout<<"np="<<p<<" t q="<<q;
temp=p;
p=q;
q=temp;
cout<<"nValue of p and q...... AFTER swapping";
cout<<"np="<<p<<" t q="<<q;
}
void main()
{
int a,b;
clrscr();
cout<<"n Enter value of a:";
cin>>a;
cout<<"n Enter value of b:";
cin>>b;
cout<<"nValue of a and b IN main ()";
cout<<"na="<<a<<" t b="<<b;
//call to swap function - call by value
swap(a,b);//a and b are actual parameter, their value is passed
cout<<"nValue of a and b IN main () after swap";
cout<<"na="<<a<<" t b="<<b;
}
5 10
A B
0x12 0x70
5 10
A B
0x12 0x70
5. AmritKaur Functions in C++
5
5.5.2 Call By Reference using an Alias
A reference provides an alias (alternate names) for the variable.
In call be reference using alias, a reference of the parameter in the calling program is
passed.
While passing, call by reference, the value of actual (original) variable changes
because there is only one copy of data is maintained with two different names.
Example: Program to swap (interchange) two numbers using call by reference using
alias.
#include<iostream.h>
#include<conio.h>
void swap(int &p, int &q)// p and q are formal arguments
{ // ampersand indicates p is alias of a
// and q is alias of b
int temp;
cout<<"nValue of p and q...... BEFORE swapping";
cout<<"np="<<p<<" t q="<<q;
temp=p;
p=q;
q=temp;
cout<<"nValue of p and q...... AFTER swapping";
cout<<"np="<<p<<" t q="<<q;
}
void main()
{
int a,b;
clrscr();
cout<<"n Enter value of a:";
cin>>a;
6. AmritKaur Functions in C++
6
cout<<"n Enter value of b:";
cin>>b;
cout<<"nValue of a and b IN main ()";
cout<<"na="<<a<<" t b="<<b;
//call to swap function - call by reference using alias
swap(a,b);//a and b are actual parameter, their value is passed
cout<<"nValue of a and b IN main () after swap";
cout<<"na="<<a<<" t b="<<b;
}
5.5.3 Call By Reference using an Pointers
In call be reference using pointers, an address of actual (original) parameter is
passed to the calling program.
While passing, call by reference, the value of actual (original) variable changes
because there is only one copy of data is maintained.
Example: Program to swap (interchange) two numbers using call by reference using
pointers.
#include<iostream.h>
#include<conio.h>
void swap(int *p, int *q)// p and q are formal arguments
{ // p contain address of a
// q contain address of b
int temp;
cout<<"nValue of p and q...... BEFORE swapping";
cout<<"np="<<*p<<" t q="<<*q;
temp=*p;
*p=*q;
*q=temp;
cout<<"nValue of p and q...... AFTER swapping";
cout<<"np="<<*p<<" t q="<<*q;
}
void main()
{
int a,b;
Afterswap() functionexecuted
10 5
P Q
A B
7. AmritKaur Functions in C++
7
clrscr();
cout<<"n Enter value of a:";
cin>>a;
cout<<"n Enter value of b:";
cin>>b;
cout<<"nValue of a and b IN main ()";
cout<<"na="<<a<<" t b="<<b;
//call to swap function - call by reference using pointer
swap(&a,&b);//a and b are actual parameter, their address is passed
cout<<"nValue of a and b IN main () after swap";
cout<<"na="<<a<<" t b="<<b;
}
5 10
A B
0x12 0x70
0x12 0x70
P Q
0x40
00
0x90
Afterswap() functionexecuted