C Programming

Calling a Function

Posted on 17th February 2014

A function can be called from anywhere within the program after the function declaration statement. When a function is called, the program control is passed to the function definition block. The statements within the function definition block are executed and the control is handed back.

A function call has the following syntax

functionName([arg_1], [arg_2],...[arg_N]);

The function name is followed by any arguments that are passed to the function inside parenthesis. Multiple arguments are separated by comma. The return value of a function can be assigned to a variable in the program.

Example of function call

#include <stdio.h>

/* Function Declaration */
int sum(int, int);

/* Function Definition */
int sum(int i, int j) {
	int s;
	s = i + j;
	return s;
}

/* Main Function */
int main()
{	int a, b, c;
	printf("Enter two numbers \n");
	scanf("%d %d", &a,&b);
	
	/* Function Call */
	c = sum(a,b);
	
	printf("The sum of %d and %d is %d \n", a,b,c);
return 0;
}

The above program calls the function sum with the input variables a and b as arguments. The sum of these two variables is calculated by the function definition block and the result is returned to the main program.

Post a comment

Comments

Nothing yet..be the first to share wisdom.