Functions prototypes (or) Function categories (or) Types of user defined functions - C-Tutorial

Latest

Friday, 11 November 2016

Functions prototypes (or) Function categories (or) Types of user defined functions

Functions prototypes (or) Function categories (or) Types of user defined functions

Depending on whether arguments are passed or not and function returns any value or not, functions are classified into different categories as:

1. Function with arguments (parameters) and with return type
2. Function with arguments (parameters) and without return type
3. Function without arguments (parameters) and without return type
4. Function without arguments (parameters) and with return type


1 with arguments and with return values
int function ( int );         // function declaration
function ( a );                // function call
int function( int a )       // function definition
{
statements;
return a;
}

2 with arguments and without return values
void function ( int );     // function declaration
function( a );                // function call
void function( int a )   // function definition
{
statements;
}

3 without arguments and without return values
void function();             // function declaration
function();                     // function call
void function()              // function definition
{
statements;
}

4 without arguments and with return values
int function ( );             // function declaration
function ( );                  // function call
int function( )               // function definition
{
statements;
 return a;
}

Note:
If the return data type of a function is “void”, then, it can’t return any values to the calling function.
If the return data type of the function is other than void such as “int, float, double etc”, then, it can return values to the calling function.

Case1 : Function with arguments and with return type
#include<stdio.h>
#include<conio.h>
int sum(int, int);  
void main()
{
 int i=10,j=20,k;
 clrscr();
 k=sum(i,j);  
 printf("%d",k);
 getch();
}

int sum(int a, int b)  
{
return a+b;
}


Case2: Function with arguments and without return type
#include<stdio.h>
#include<conio.h>
void sum(int, int);  

void main()
{
 int i=10,j=20;
 clrscr();
 sum(i,j);    
 getch();
}
void sum(int a,int b)  
{
 printf("\n Sum=%d", a+b);
}

Case3 : Function without arguments and with return type
#include<stdio.h>
#include<conio.h>
int sum();

void main()
{
 int k;
 clrscr();
 k=sum();    
 getch();
}
int sum()  
{
 int a=10,b=20;
 return a+b;
}

Case4: Function without arguments and without return type
#include<stdio.h>
#include<conio.h>
void sum();  
void main()
{
sum();    
}
void sum()  
{
int a=10,b=20;
printf("sum=%d",a+b);
}

No comments:

Post a Comment