Jan 16

Declaration of a function or function prototype

Every function has its declaration and function definition. When we talk of declaration only,it means only the function name,its arguments and return type are specified and the function body or definition is not attached to it.

int square(int num)

{

int result;

result = num * num;

return(result);

}

void main()

{

int n,sq;

printf(”Enter a number to calculate square value \n”);

scanf(”%d”,&n);

sq= square(n);

}

In above program for calculating square of a given number,we have declared function square() before main() function,this means before coming to main() ,compiler knows about square(),as the compilation process starts with the first statement of any program. Now,suppose we reverse the sequence of functions in this program i.e. writing the main() function and later on writing the square() function ,what happens?

The C compiler will give an error.

Function prototype require that every function which is to be accessed should be declared in the calling function.

Program to calculate square of number using function prototype

#include<stdio.h>

void main()

{

int n,sq;

int square(int); /*function prototype*/

printf(”Enter a number\n”);

scanf(”%d”,&n);

sq=square(n);

printf(”Square of a number is %d “,sq);

}

/*square function*/

int square (int num)

{

int result;

result = num* num;

return(result);

}

Output

Enter a number

5

Square of the number is 25

Note

  • Function prototype requires that the function declaration must include the return type of function as well as number of arguments or parameter passed.
  • The variable name of arguments need not be declared in prototype.
Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Blogplay
  • LinkedIn
  • MySpace
  • Reddit
  • RSS
  • Twitter

written by Shweta