Sns College of Technology: Mr. Selvakumar N Assistant Professor Department of Computer Science and Engineering
Sns College of Technology: Mr. Selvakumar N Assistant Professor Department of Computer Science and Engineering
Coimbatore-35.
An Autonomous Institution
I YEAR/ I SEMESTER
Topic: Functions
Mr. Selvakumar N
Assistant Professor
Department of Computer Science and Engineering
Functions
➢ Like variables, all functions in a C program must be declared, before they are invoked.
➢ A function declaration (also known as function prototype) consists of four parts.
➢ Function type (return type).
➢ Function name.
➢ Parameter list.
➢ Terminating semicolon.
➢ They are coded in the following format:
➢ Function-type function-name (parameter list);
➢ This is very similar to the function header line except the terminating semicolon.
➢ For example, mul function defined in the previous section will be declared as:
➢ int mul (int m, int n); /* Function prototype */
FUNCTION DECLARATION
Points to Note
1. The parameter list must be separated by commas.
2. The parameter names do not need to be the same in the prototype declaration and the function
definition.
3. The types must match the types of parameters in the function definition, in number and
order.
4. Use of parameter names in the declaration is optional.
5. If the function has no formal parameters, the list is written as (void).
6. The return type is optional, when the function returns int type data.
7. The retype must be void if no value is returned.
8. When the declared types do not match with the types in the function definition, compiler will
produce an error.
FUNCTION DECLARATION
A prototype declaration may be placed in two places in a program.
1. Above all the functions (including main).
2. Inside a function definition.
When we place the declaration above all the functions (in the global declaration section), the prototype is
referred to as a global prototype.
Such declarations are available for all the functions in the program.
When we place it in a function definition (in the local declaration section), the prototype is called a local
prototype.
Such declarations are primarily used by the functions containing them.
The place of declaration of a function defines a region in a program in which the function may be used by other
functions.
This region is known as the scope of the function.
It is a good programming style to declare prototypes in the global declaration section before main.
It adds flexibility, provides an excellent quick reference to the functions used in the program, and enhances
documentation.
FUNCTION DECLARATION
Prototypes: Yes or No
Prototype declarations are not essential.
If a function has not been declared before it is used, C will assume that its details available at the time of
linking.
Since the prototype is not available, C will assume that the return type is an integer and that the types of
parameters match the formal definitions.
If these assumptions are wrong, the linker will fail and we will have to change the program.
The moral is that we must always include prototype declarations, preferably in global declaration section.
Parameters Everywhere!
Parameters (also known as arguments) are used in following three places:
1. in declaration (prototypes),
2. in function call, and
3. in function definition.
The parameters used in prototypes and function definitions are called formal parameters and those
used in function calls are called actual parameters.
Actual parameters used in a calling statement may be simple constants, variables, or expressions.
The formal and actual parameters must match exactly in type, order and number.