Function Definition

A function definition contains block of code that performs a specified task (also called function body). The function definition also contains an implicit declaration so if the function definition is written before the function call then function declaration can be omitted.

The syntax of a function definition is as below

returnType functionName([type arg_1],...[type arg_N]) {
variable_declarations;
statements;
}
  • returnType is the type of value returned by the function
  • functionName is the name of the function
  • type arg_1, type arg_N etc., declares the arguments that are passed to the function. This is same like declaring variables

The function body is enclosed in curly braces { } and contains variable declarations for any additional variables used in the function, a set of statements that performs the intended operation and return statement that pass some data back to the function call. A function will terminate when it reaches a return statement and the execution control is transferred back to the function calling statement. The return statement can be written anywhere in the function body.

Example of a function definition

int sum(int i, int j) {
	
int s;

s = i + j;
return s;

}

This example defines a function named sum which has two integer arguments i and j. The sum of i and j is assigned to variable s and that value is returned back to the function call statement.

Last Updated: 17-February-2014


Post a comment

Name

Your Comment

Email (We dont publish it)

Comments

Nothing yet..be the first to share wisdom.