You are on page 1of 23

Functions :

Function is the block of code which is used


to perform a particular operation.

There are two type of the functions :

(i) Pre-defined functions

(ii) User -defined functions .

Monolithic Programming : In this case , we


write the entire logic in the single program.

The disadvantages of the monolithic


programming are,

(a) Difficult to understand.


(b) Difficult to modify
(c) Difficult of debug.

[Bug means computer program error and


debugging is the process of locating and
removing the computer program error.]

Modular Programming : In the case of the


modular programming , we divide the entire
complex program into the simple sub-
programs or modules.These modules are
also known as functions.

Advantages of the modular


programming :

(a) Easy to understand


(b) Easy to debug
(c) Easy to modify
(d) Resuability of code.

(i) Pre-defined Functions : These functions


are defined in the header files.

e.g. stdio.h (printf() , scanf( ))

conio.h ( clrscr( ) , getch( ) )


(ii) User-defined functions : These functions
are defined by the user for there own
prupose.

These functions contains the following


components :

(a) Function Prototype.

(b) Function call

(c) Function definition

(a) Function Prototype : The function


prototype is used to specify the layout of the
function.

The function prototype specifies the


following information :

(i) The function name

(ii) The number of arguments the


function requires

(iii) The type of arguments

(iv) The returntype.

The general form is ,

returntype functionname(argument
list);

Example 1 :

int sum(int ,int);

It specifies that there will be a function


with the name
sum( ) and this function will take two
arguments and both are of type integers
and the sum() will also return the integer
type value.
float average(float,float,float);

It speicifes that there will be a function


with the name average( ) and this function
will take the three arguments and all of
them are of type float and the average( ) will
return the float type value .

void show(char , int );

It speicfies that there will be a function


with the name show( ) and this function will
take the two arguments and the first
argument will be of type char and the
second argument will be of the type int
and the function will not return any value.

(b) Function Call :

Consider the following statement ,


main( )
{

:: : : : :

clrscr( );

: : : : : :
: : : : : :
}

In the above mentioned code , we are


calling the clrscr() function in the
main( ) .
The clrscr() function will be known
as the Called Function and the main( )
function will be known as the calling
function .

The function call transfers the


control from the calling funtion to
the called function.The function call
results in the execution of the function
definition.

There are two types of the function


calls,

(i) When the function returns a value


.

The general form is,

variablename=functionname(argument
list);

e.g.

c=sum(a,b);

(ii) When the function doesnot return


a value .

The general form is ,


functioname(argument list);

e.g.

show(ch,n);

(c) Function definition :

The function definition will define


the actual logic of the function .

The general form of the function


definition is ,

returntype functionname(argument
list)
{
body of the function

Q Write a "C++" function to add two


numbers.
#include<iostream.h>
#include<conio.h>
/*function prototype*/
int sum(int,int);
void main( )
{
int a,b,c;

clrscr();

cout<<endl<<"Enter the value of a


and b:";
cin>>a>>b;

c=sum(a,b);/*function call*/

cout<<endl<<"Sum = "<<c;

getch();
}
/*function definition */
int sum(int x,int y)
{
int z;

z=x+y;

return z;
}

(b) When the function doesnot return the


value .

#include<iostream.h>
#include<conio.h>
/*function definition */

void sum(int i,int j)


{
int c;

c=i+j;
cout<<endl<<"Sum = " <<c;
}

void main( )
{
int a,b;

clrscr();

cout<<endl<<"Enter the value of a


and b:";
cin>>a>>b;

sum(a,b);/*function call*/

getch();
}

Q Write a 'c' function to calculate the


factorial of the number
(i) When the function returns the value

#include<iostream.h>
#include<conio.h>
/*function prototype*/
int factorial(int);
void main( )
{
int num,fact;

clrscr();

cout<<endl<<"Enter the number :";


cin>>num;

fact=factorial(num); /*function call*/

cout<<endl<<"Factorial ="<<fact;

getch();
}
/*function defintion */
int factorial(int num)
{
int i,fact;

fact=1;

for(i=1;i<=num;i++)
fact=fact*i;

return fact;
}

(ii) When the function doesnot return the


value

#include<iostream.h>
#include<conio.h>
/*function prototype */

void factorial(int);

void main()
{
int num;

clrscr( );

cout<<endl<<"Enter the number :";


cin>>num;

factorial(num); /* function call*/

getch();
}

/* function definition */

void factorial(int num)


{
int i,fact;

fact=1;

for(i=1;i<=num;i++)
fact=fact*i;
cout<<endl<<"Factorial ="<<fact;
}

Q Write a 'c++' program using function to


calculate the value
of c.

c= n!
----------
r!*(n-r)!

#include<iostream.h>
#include<conio.h>
/*function prototype*/
int factorial(int);
void main( )
{
int n,r,c,factn,factr,factnr;

clrscr();

cout<<endl<<"Enter the value of n and


r:";
cin>>n>>r;

/*calculate the factorial of n */

factn=factorial(n);

/*calculate the factorial of r */

factr=factorial(r);

/* calculate the factorail of n-r*/

factnr=factorial(n-r);

/*calculate the value of c */

c=factn/(factr*factnr);

cout<<endl<<"c="<<c;

getch();
}
/*function definition */
int factorial(int num)
{
int fact=1,i;

for(i=1;i<=num;i++)
fact=fact*i;

return fact;
}

Q Write a 'C' function to calculate the x ^ y.

x=5 y=3 x^ y=5^3=5*5*5=125

(i) When the functin returns the value

(ii) When the function doesnot returns the


value .

Solution :
#include<iostream.h>
#include<conio.h>
/*function prototype*/
int power(int,int);
void main()
{
int x,y,result;

clrscr();

cout<<endl<<"Enter the value of x and


y :";
cin>>x>>y;

result=power(x,y); /*function call*/

cout<<"Result =" <<result;

getch();
}

/*function defintion*/
int power(int x,int y)
{
int i,result;

result=1;

for(i=1;i<=y;i++)
result=result*x;

return result;
}

Q WAF to convert the lowercase to the


uppcase

#include<iostream.h>
#include<conio.h>
//function prototype
char convert(char);
void main()
{
char ch;
clrscr();

//read the character

cout<<endl<<"Enter the character :";


cin>>ch;

char result;

result=convert(ch);//function call

cout<<endl<<"Result = " <<result;

getch();

//function definition

char convert(char ch)


{
if(ch>='a'&&ch<='z')
return 'A'+(ch-'a');
else
return ch;
}

Q WAF to display the ASCII value of a


character

#include<iostream.h>
#include<conio.h>
void show(char );
void main()
{
char ch;

clrscr();

//read the character

cout<<endl<<"Enter the character :";


cin>>ch;

show(ch); //function call


getch();
}

//function definition

void show(char ch)


{
cout<<endl<<"ASCII value of "<<ch<<"
is = "<<(int)ch;
}

or

void show(char ch)


{
int val;

val=ch;

cout<<endl<<"ASCII value of "<<ch<<"


is = "<<val;
}

You might also like