Tuesday, 19 February 2013

Function Definition



Function definition contains programming codes to perform specific task.

Syntax of function definition:
return_type function_name(type(1) argument(1),……, type(n) argument(n))
{
          //body of definition
}

Function definition has two major components:

1.     Function Declarator
Function declaratory is the first line of function definition. When a function is invoked from calling function, control of the program is transferred to function declaratory or called function.

Syntax of Function Declarator
return_type function_name(type(1) argument(1),….., type(n) argument(n))

Syntax of function declaration and declaratory are almost same except, there is no semicolon at the end of declaratory and function is followed by function body.

2.     Function Body
Function declarator is followed by body of function which is composed of statements.


Passing Arguments to Functions
In programming, argument/parameter is a piece of data (constant or variable) passed from a program to the function.
In below example two variable, num1 and num2 are passed to function during function call and these arguments are accepted by arguments a and b in function definition.


Arguments that are passed in function call and arguments that are accepted in function definition should have some data type. 
For Example:
If argument num1 was of int type and num2 was of float type the, argument should be of int type and b should be of float type in function definition i.e., type of argument during function call and function definition should be same.
A function can be called with or without an argument.

No comments:

Post a Comment