Chap 2 Functions in C++
Chap 2 Functions in C++
The ways of supporting function has changed and improved in C++ as compared to
C.Most of these changes are simple and straightforward.However there are a few
changes which require you to adopt a new way of thinking and organizing your
program's code.Many of these requirements were driven by object oriented facilities
of C++.As we go along you would realise that these facilities were invented to make
C++ programs safer and more readable than their C equivalents.Let us know the
various issues involved in C++ functions.
Function Prototypes
A function prototype is a declaration that defines both : the arguments passed to the
function and the type of value returned by the function. If we were to call a
function fool( ) which receives a float and int as arguments and returns a double
value its prototype would look like this:
double fool(float,int);
The C++ compiler uses the prototype to ensure that the types of the arguments you
pass in a function call are same as those mentioned in the prototype.If there is a
mismatch the compiler points out immediately.This is known as strong type
checking,something which C lacks.
Without strong type checking,it is easier to pass illegal values to functions.For
example,a non-prototyped function would allow you to pass anint to a pointer
variable,or a float to a long int.The C++ compiler would immediately report such
types of errors.In C++ prototypes do more than making sure that actual arguments
(those used in calling function) and formal arguments (those used in called function)
match in number,order and type.As we would see later,C++ internally generates
names for functions,including the argument type information.This information is used
when several functions have same names.
Remember that all functions in C++ must be prototyped.This means that every
function must have its argument list declared,and the actual definition of a function
must exactly match its prototype in the number,order and types of parameters.
//prototype-like style
double fool(int a,float b)
{
//some code
}
Function Overloading
Another significant addition made to the capabilities of functions in C++
is that of function overloading.With this facility you can have multiple
functions with the same name,unlike C,where all the functions in a
program must have unique names.
#include<iostream.h>
void main( )
{
int i=-25,j;
long l=-100000L,m;
double d=-12.34,e;
int abs(int);
long abs(long);
double abs(double);
j=abs(i);
m=abs(l);
e=abs(d);
cout<<endl<<j<<endl<<m<<e;
}
How does the C++ compiler know which of the abs( )s should be called when a call
is made? It decides from the type of the argument being passed during function
call.For example,if an int is being passed the integer version of abs( ) gets called,if
a double is being passed then the double version ofabs( ) gets called and so
on.That's quite logical,you would agree.
ch=abs('A')
We have not declared abs( ) function to handle a char.Hence the C++ compiler
would report an error.If we want that this call should result into a call to
the int version of abs( ),we must make use of typecasting during the call,as shown
below:
ch=abs((int);A');
Default Arguments in Functions
void main( )
{
clrscr( );
box(10,20,22,70); //Passing all 4 arguments
box(10,20,15); //Passing 3 arguments
box(5,10); //Passing 2 arguments
box( ); //Passing no arguments
}
When we call the function box( ) with 4 arguments the box is drawn with the
arguments passed.However when we call it with 3 arguments the default value
mentioned in the prototype of box( ) is considered for the last argument.Likewise
when we call the function with 2 arguments the default values for the last 2
arguments are considered.Finally,when we call it with no arguments, a box is drawn
with all the default values mentioned in the prototype. Thus the default arguments
are used if the calling function doesn't supply them when the function is called.
Note: If one argument is missing when the function is called, it is assumed to be the
last argument.Thus,the missing arguments must be the trailing ones./you can leave
out the last 3 arguments,but you cannot leave out the last but one and put in the last
one.
a) While making a function call if you don't want to take the trouble of writing
arguments which almost always has the same value.
b) They are also useful in such cases where,after having written a program
we decide to increase the capabilities of a function by adding another
argument.Using default arguments means that the existing function calls can
continue to use old number of arguments,while new function calls can use
more.
Operator Overloading
Operator Overloading is one of the most fascinating features of C++.It can
transform,obscure program listings into intuitive obvious ones.By overloading
operators we can give additional meaning to operators like +,*,-,<=,>=,etc.
which by default are supposed to work only on standard data types
likeints,floats etc. For example if str1 and str2 are two character arrays
holding strings "Bombay" and "Nagpur" in them then to store
"BombayNagpur" in a third string str3, in C we need to perform the following
operations:
No doubt this does the desired task but don't you think that the following form
would have made more sense:
Such a form would obviously not work with C,since we are attempting to
apply the + operator on non-standard data types (strings) for which addition
operation is not defined. That's the place where C++ scores over C,because it
permits the + operator to be overloaded such that it knows how to add two
strings.
#include<iostream.h>
#include<iomanio.h>
matrix c,d,e,f;
c=a+b;
d=a*b;
e=a+b*c;
f=a-b*c+d;
mat_print(c);
mat_print(d);
mat_print(e);
mat_print(f);
for(i=0;i<MAXROW;i++)
{
for(j=0;j<MAXROW;j++)
c.arr[i][j] = a.arr[i][j] + b.arr[i][j];
}
return c;
}
for(i=0;i<MAXROW;i++)
{
for(j=0;j<MAXROW;j++)
c.arr[i][j] = a.arr[i][j] - b.arr[i][j];
}
return c;
}
{
matrix c;
int i,j,k;
for(i=0;i<MAXROW;i++)
{
for(j=0;j<MAXROW;j++)
{
c.arr[i][j]=0;
for(k=0;k<MAXCOL;k++)
c.arr[i][j] + = a.arr[i][k] * b.arr[k][j];
}
}
return c;
}
void mat_print(matrix p)
{
int i,j;
cout<<endl<<endl;
for(i=0;i<MAXROW;i++)
{
cout<<endl;
for(j=0;j<MAXCOL;j++)
cout<<setw(5)<<p.arr[i][j];
}
}
Inline Functions
One of the important advantages of using functions is that they help us save memory
space.As all the calls to the function cause the same code to be executed;the
function body need not be duplicated in memory.
#include<iostream.h>
void main( )
{
//code to open source file
if (fileopeningfailed)
reporterrer("Unable to open source file");
int main( )
{
float a = 12.345;
float b = 9.82;
cout<<mul(a,b)<<endl;
cout<<div(a,b)<<endl;
return 0;
}
Output:
121.228
1.25713