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
<Return type> <Function name> <Parameter list>
{
<local
definitions>
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.
When a function is written before main it can be
called in the body of main. If it is written after main then in the declaration
of main you have to write the prototype of the function. The prototype can also
be written as a global declaration.
Program
Case 1:
#include <stdio.h>
main ( )
{
int i;
void (int
*k) // D
i = 0;
printf
(" The value of i before call %d \n", i);
f1
(&i); // A
printf
(" The value of i after call %d \n", i);
}
void (int *k) // B
{
*k = *k +
10; // C
}
Case 2:
#include <stdio.h>
void (int *k) // B
{
*k = *k +
10; // C
}
main ( )
{
int i;
i = 0;
f1
(&i); // A
printf
(" The value of i after call %d \n", i);
}
Case 3:
#include <stdio.h>
void f1(int *k) // B
{
*k = *k +
10; // C
}
.
main ( )
{
int i;
i = 0;
printf ("The value of i before call %d
\n", i);
f1
(&i); // A
printf ("The value of i after call %d
\n", i);
}
Explanation
- In Case 1, the function is
written after main, so you have to write the prototype definition in main
as given in statement D.
- In Case 2, the function is
written above the function main, so during the compilation of main the
reference of function f1 is
resolved. So it is not necessary to write the prototype definition in
main.
- In Case 3, the prototype is
written as a global declaration. So, during the compilation of main, all
the function information is known.