A user defined function has to be developed by the user at the time of writing a program. every function has the following elements associated with it.
- Function declaration
- Function call
- Function definition
- Function parameters
- Return statement.
Function declaration: Before using any user defined function in your C program, they must be declared. This type of declaration is called function prototype.
A function prototype tells to compiler information such as name of the function, the return value, type of the parameters. More generally a function declaration tell us what a function can do.
The general format to declare a function is
syntax: return-type function-name(argument-list);
Example: int sum(int a, int b);
Function call: function call is a postfix expression. The operand is the function name and operators are parameters list. The general form to call a function is
Syntax: function-name(argument-list);
Example: sum(a,b);
Function definition: The function definition consist of code of the function. It is made up of two parts: the function header and function body.
Syntax: return-type function-name(argument-list)
{
function body
}
Example:
int sum(int a, int b)
{
// some line of code here
}
The function header consists of three parts: return type, function name and parameter list.
Function parameters: These are input values to a function. C supports two types of arguments: actual parameters and formal parameters.
Return statement: The return statement is used to terminate the execution of a function and return control to the calling function.
A C function may or may not return a value from the function. If you don't have to return any value from the function, use void as return type.
The detail explanation about parameters type and return type can available on upcoming pots.
0 comments :
Post a Comment
Note: only a member of this blog may post a comment.