When a function is defined, space is allocated for that function in the memory. A function definition comprises two parts:Function header,Function body
FUNCTION
DEFINITION
When
a function is defined, space is allocated for that function in the memory. A
function definition comprises two parts:
•
Function header
•
Function body
Programming Tip:
It
is an error to place a semicolon after the function header in the function
definition.
The
syntax of a function definition can be given as:
return_data_type function_name (data_type
variable1, data_type variable2, ...)
{
………...
statements
…….......
return
(variable);
}
The
number of arguments and the order of arguments in the function header must be
same as that given in the function declaration statement.
Programming Tip:
The
parameter list in the function definition as well as function declaration must
match.
While
return data_type_function_name (data_type variable1, data_type variable2,..) is
known as the function header, the rest of the portion comprising of program statements
within {} is the function body which contains the code to perform the specific
task.
The
function header is same as function declaration. The only difference between
the two is that a function header is not followed by a semicolon. The list of
variables in the function header is known as the formal parameter list. The parameter list may have zero or more
parameters of any data type. The function body contains instructions to perform
the desired computation in a function.
Programming Tip:
A
function can be defined either before or after main().
The
function definition itself can act as an implicit function declaration. So the
programmer may skip the function declaration statement in case the function is
defined before being used.
Note
The
argument names in the function declaration and function definition need not be
the same. However, the data types of the arguments specified in function
declaration must match with that given in function definition.
Programming in C: Unit III (a): Functions : Tag: : Programming in C - Function Definition
Programming in C
CS3251 2nd Semester CSE Dept 2021 | Regulation | 2nd Semester CSE Dept 2021 Regulation