Category of Functions


A function depending an whether the arguments are present or not and whether a value is returned or not, may belong to one of following categories

  1. Function with no return values, no arguments
  2. Functions with arguments, no return values
  3. Functions with arguments and return values
  4. Functions with no arguments and return values.

1.).In this category, the function has no arguments. It does not receive any data from the calling function. Similarly, it doesn’t return any value. The calling function doesn’t receive any data from the called function. So, there is no communication between calling and called functions.
2.)In this category, function has some arguments . it receives data from the calling function, but it doesn’t return a value to the calling function. The calling function doesn’t receive any data from the called function. So, it is one way data communication between called and calling functions.
Eg: Printing n Natural numbers

#include< stdio.h>
#include< conio.h>
void nat( int);
void main()
{
	int n; 
	clrscr();
	printf("\n Enter n value:");
	scanf("%d",&n);
	nat(n);
	getch();
}

void nat(int n)
{
	int i;
	for(i=1;i<=n;i++)
	printf("%d\t",i);
}
								


Output:
Enter n value: 5
1 2 3 4 5
Note:
In the main() function, n value is passed to the nat() function. The n value is now stored in the formal argument n, declared in the function definition and subsequently, the natural numbers upto n are obtained.
3.)In this category, functions has some arguments and it receives data from the calling function. Simillarly, it returns a value to the calling function. The calling function receives data from the called function. So, it is two-way data communication between calling and called functions.
Eg:

#include< stdio.h>
#include<conio.h>
int fact(int);
void main()
{  
	int n;
	clrscr();
	printf("\n Enter n:");
	scanf("%d",&n);
	printf("\n Factorial of the number : %d", fact(n));
	getch();
}

int fact(int n)
{
	int i,f;
	for(i=1,f=1;i<=n;i++)
		f=f*i;
	return(f);
}
								


Output:
Enter n: 5
Factorial of the number : 120
4.)In this category, the functions has no arguments and it doesn’t receive any data from the calling function, but it returns a value to the calling function. The calling function receives data from the called function. So, it is one way data communication between calling and called functions.
Eg:

#include< stdio.h>
#include< conio.h>
int sum();
void main()
{
	int s;
	clrscr();
	printf("\n Enter number of  elements to be added :");
	s=sum();
	printf("\n Sum of the elements :%d",p);
	getch();
}

int sum()
{
	int a[20], i, s=0,n;
	scanf("%d",&n);
	printf("\n Enter the elements:"); 
	for(i=0;i< n; i++)
		scanf("%d",& a[i]);
	for(i=0;i< n; i++)
		s=s+a[i];
	return s;
}
								


Many a times, we need to repeat the same process in a program multiple times. It is difficult for us to define functions for every function call of the same process.Based on the calling of the functions by the user in a program, functions are classified as

  1. Recursive functions
  2. Non-Recursive functions