Introduction
When specifying the function, you have to specify the return type, function name, parameter, list, and function body. Within the function body, you can have local definition and return statements.
Program/Example
The general format of a function is
{ executable statements; Return (expression); } For example, int f1 (int j, float f) { int k; k = l; return (k); }
Explanation
-
A function returns a value of the type that is specified by the return type. If you don't specify a written type, it is assumed that it returns an int value.
-
When the function does not return a value, you have to specify the return data type as void. When the function returns void, you may not write return in the body or you can write the return statement as return; .
-
All functions must be named.
-
You can specify parameters in the parameter list, separated by commas. While specifying the parameters, you have to specify the parameter data type and parameter name.
-
If you don't specify parameters, then you can specify only parentheses as shown here:
int f1( )
-
When you want to use variables only for the function then you can declare them just as in main.
-
A function returns a value to the caller using the return statement. You may have multiple return statements and the return expression should evaluate to a value that is compatible with the return data type.
-
A function returns to the caller after executing the first return statement it encounters during execution.
-
A call to a function should match the definition of the function.
-
The order of parameters in the call is important because the actual parameter value is copied to the formal parameter value according to the order. It means that the first argument in the call is copied to the first parameter, the second argument is copied to the second parameter, etc.
-
When you are using whole numbers as parameters, it is better to declare them by using the data type int. Because all your lower data types' actual parameters can be used for passing the value, your function can be useful for multiple data types.
-
When you are using real numbers as parameters, it is better to declare them as double so that the function can be used for both the float and double data types.
Points to Remember
-
While specifying the function you have to specify five main functions: written type, function name, parameter, list, function body and return statement.
-
Function name and function body are necessary, while the others are optional.
No comments:
Post a Comment