Professional Documents
Culture Documents
Chapter-4
Functions
Function is a program segment or a sub program (or a module) that performs
a specific and well-defined task. It reduces the complexity of the program.
strcpy( ), strlen( ), strupr( ) etc are defined under the header file string.h
User defined functions: C++ allows programmers to define our own function
within a program. A program may contain one or more functions. The execution
always starts from the main; all other functions are invoked from the main.
Function definition: Function definition is writing the actual code for the function.
The general format of the function definition is as follows
Return-Type the data type of the return value of the function. If nothing is returned
the data type is void.
Function-name a valid identifier
Parameter list list of variable that receives value from calling function.
return() is a keyword that returns a value from sub function to calling function.
Function prototype:
The function prototype is the function declaration that provides the following
information to the compiler.
The return statement: return is a key word used to send the o/p of the function
back to the calling function. It is a means of communication from the calling function
to the called function. The constant or variable or expression will return the value to
the calling function after processing.
Syntax: 1. return;
2. return (expression);
The first return statement does not return any value. It simply returns control
to the point from where it was called.
The second return statement returns the value of the expression.
Note: A function can have multiple return statements, but at any given time only one return
statement will be executed.
Call by value
In this method the copy of the value of actual parameters is passed to the
function. This mechanism of data transfer between the actual and formal parameters
is called call by value. The current value of actual parameter becomes the initial value
of formal parameters. Any change made to the formal parameters will not change
the value of actual parameters.
Call by address (pass by address): In this method instead of passing the values,
the memory address of the corresponding arguments is passed. Hence the receiving
parameter (formal parameter) should be a pointer. Hence any change made to the
contents of the formal parameter will reflect on the original value of the variable.
The function can not return more than It simulates a function that can return
one value more then one value.
Constants can be passed as arguments Constants can not be passed as
arguments.
Example : For the function with the Example: For the function with the
following prototype following prototype
k=add(10,4); k = add(10,4);
Definition: Several functions have same name but differ in input parameter list are
called overloaded functions. OR
“Two or more functions are said to be overloaded if they have same name but
have different argument list”. Argument list refers to the number of arguments &
data type of the arguments.
Using the concept of function overloading, we can design a family of functions
with one function name but with different argument list. The function would perform
different operations depending on the argument list in the function call. The correct
function to be invoked is determined by checking the number and type of the
arguments but not on the function type.
Declarations:
e.g. int add(int a, int b); //prototype-1
float add (float a, float b); //prototype-2
long int add (long int a, long int b); //prototype-3
Function call:
cout<<add (5, 9);
cout<<add(12.3,22.8);
cout<<add(45L,9L);
A function call first matches the prototype having the same number and type
of arguments and then calls the appropriate function for execution. A best match
must be unique. The function selection involves the following steps:
1. The compiler first tries to find an exact match in which the types of actual
arguments are the same and use that function.
2. If an exact match is not found, the compiler uses the integral promotions to
the actual arguments, such as,
char to int
float to double
to find a match.
3. When either of them fails, the compiler tries to use the built-in conversions
(the implicit assignment conversions) to the actual arguments and then uses
the function whose match is unique. If the conversion is possible to have
multiple matches, then the compiler will generate an error message.
Suppose we use the following two functions:
long square(long n)
double square(double x)
A function call such as
square(10);
will cause an error because int argument can be converted to either long or
double, thereby creating an ambiguous situation as to which version of
square() should be used.
4. If all of the steps fail, then the compiler will try the user-defined conversions in
combination with integral promotions and built-in conversions to find a unique
match. User-defined conversions are often used in handling class objects.
Advantages:
Default arguments are used to combine similar functions or group of functions into a
single function.
The default arguments to add new parameters to the existing functions.
The Default arguments are given only in the function prototype.
To solve such problems C++ provides a new feature called inline function. It
reduces the runtime function overhead.
“An inline function is a function that is expanded in line when it is invoked i.e. the
compiler replaces the function call with the corresponding code itself”.
Syntax: inline return-type function-name (arg-list)
{
Function-body
}
P37. WAP to find the square of a given integer using inline function.
P38. WAP to check whether the given number is a prime or not using inline functions.
Note: i) The qualifier inline is used only if the function body is too small
ii) The inline function must be defined before it is called.
iii) The inline is not a command; it is a request made to the complier. Hence there is no
guarantee that the code gets inserted when the call is made.
iv) If the function is too long, the compiler may ignore the request.
STORAGE CLASSES: The storage class is a class of variables that define the scope
and lifetime of variable in a program.
#include<iostream.h>
#include<conio.h>
void main()
{
auto int a = 100; Output:
function1();
cout<< a <<endl; 1
} 10
100
function1()
{
auto int a = 10;
function2();
cout<< a <<endl;
}
function2()
{
auto int a=1;
cout<<a<<endl;
}
#include<iostream.h>
#include<conio.h>
void main()
{
extern int n=100;
output ();
} Output
n=100
int n
output ()
{
cout<<“n=”<<n;
getch();
}
function() P=10
{ P=20
static int p=0; P=30
cout<<“p=”<< p+10;
getch();
}
P=10
P=10
P=10
4. Register storage class: At the time of execution the value of the variable is
stored in CPU memory. If the value of the variable is repeatedly required as in
looping statement, a lot of computer time is wasted at the time of processing for that
we are storing a value in CPU register. This register is much faster than memory.
Syntax: register int a, b, c;
void main()
{
register int n;
for(n=1;n<=3000;n++)
cout<<endl<<” n = “ << n;
}
Review Questions