Chapter 4 Functions




c function can be classified into two categories namely:
1. Library function
2.User-defined functions.
main() is a example of user defined function. printf and scanf belong to a the category of library function.
2.User-defined functions :
There are times when some type of operation or calculation is repeated at many points throughout a program. In such situation we may repeat the program statements whenever they are needed. Another approach is to design a function that can be called and used whenever required. This saves both time and space.
A function, depending on whether arguments are present or not and whether a value is returned or not, may belong to one of the following categories.
1. Function with no arguments and no return values :
When a function has no arguments, it does not receive any data from the calling function. Similarly, when it does not return a value. the calling function does not receive any data from the called function. In effect, there is no data transfer between the calling function and the called function. In fig. the dotted lines indicate that there is only a transfer of control but not data.
Example :
#include<stdio.h>
main()
{
void sqr();
sqr();
}
void sqr()
{
int no;
printf("Enter no : ");
scanf("%d",&no);
printf("sqr is %d",no*no);
}



2. Function with arguments and no return values :
The function receives data from the calling function through arguments, but does not send back any value. Such functions will have one-way data communitcation.The actual and formal arguments should match in number, type , and order.
Example :

#include<stdio.h>
main()
{
void sqr(int no);
int n;
printf("\n Enter no : ");
scanf("%d",&n);
sqr(n);
}
void sqr(int no)
{
printf("sqr is %d",no*no);
}


3. Function with arguments and return values :
The function value in fig receives data from the calling function through arguments. And send a value to calling function. Such functions will have two-way data communication.

Example:
#include<stdio.h>
main()
{
int  sqr(int no);
int s,n;
printf("\n Enter no : ");
scanf("%d",&n);
s=sqr(n);
printf("sqr is %d",s);
}
int sqr(int no)
{
return(no*no);
}


Recursion :
When a called function in turn calls another function a process of 'chaining' occurs. Recursion is a special case of this process, where a function calls itself. A very simple example of recursion presented below :
main()
{

printf(" Cdac computer centre");

main();
}\
When executed, this program will produce an output something like this :
Cdac computer centre Cdac computer centre Cdac computer centre Cdac computer centre Cdac computer centre Cdac computer centre Cdac computer centre......
Execution is terminated abruptly.
Another useful example of recursion is the evaluation of factorials of given number.
/* Calculate factorial of given no. using recursion*/
#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
int fact(int m);
int n,factno;
printf("INPUT N :");
scanf("%d",&n);
factno=fact(n);
printf("factorial = %d",factno);
getch();
}

int fact(int n)
{
if(n>1)
return(n*fact(n-1));
else
return(1);
}