Jan 16

<return data type>  <function_name(list of arguments)>

{

data type declaration of the arguments;

executable statements;

return(expression);

}

where

  • return data type is the same as the data type of the variable that is returned by function using return statement.
  • function_name is formed in the same way as variables names/identifier are formed.
  • list of arguments as parameters are valid variable names.

Example

(int x, float y, char z)

  • arguments give the value which are passed from calling function.
  • body of function contains executable statement.
  • return statement returns a single value to the calling function.

Program to calculate square of integer using functions

/*square function*/

int square(int num) /*passing of arguments */

{

int result; /*local variable to function square */

result = num * num;

return(result); /*returns an integer value*/

}

/*it will be called from main() as follows*/

void main()

{

int n,sq; /* local variable to function main */

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

scanf(”%d”,&n);

sq= square(n); /* function call with parameter passing */
}

/*program ends*/

Output

Enter a number to calculate square value

5

Square  of  the number is 25


Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Blogplay
  • LinkedIn
  • MySpace
  • Reddit
  • RSS
  • Twitter

written by Shweta