Before using a function, the compiler must know about the number of parameters and the type of parameters that the function expects to receive and the data type of the value that it will return to the calling function.
FUNCTION DECLARATION/ FUNCTION PROTOTYPE
Before using a function, the compiler must know about the number of parameters and the type of parameters that the function expects to receive and the data type of the value that it will return to the calling function. Placing the function declaration statement prior to its use enables the compiler to make a check on the arguments used while calling that function.
The general format for declaring a function that accepts some arguments and returns some value as a result can be given as:
return_data_type function_name (data_type variablel, data_type variable2,...);
Here, function_name is a valid name for the function. Naming a function follows the same rules as naming variables. A function should have a meaningful name that must specify the task that the function will perform. The function name is used to call it for execution in a program. Every function must have a different name that indicates the particular job that the function does.
return_data_type specifies the data type of the value that will be returned to the calling function as a result of the processing performed by the called function.
data_type variable1, data_type variable2, is a list of variables of specified data types. These variables are passed from the calling function to the called function. They are also known as arguments or parameters that the called function accepts to perform its task. Table 4.1 shows examples of valid function declarations in C.
Table 4.1 Valid function declarations
Things to remember about function declaration:
Programming Tip: Though optional, use argument names in the function declaration.
• After the declaration of every function, there should be a semicolon. If the semicolon is missing, the compiler will generate an error message.
• The function declaration is global. Therefore, the declared function can be called from any point in the program.
• Use of argument names in the function declaration statement is optional. So both declaration statements are valid in C.
int func (int, char, float);
or
int func (int num, char ch, float fnum);
• A function cannot be declared within the body of another function.
• A function having void as its return type cannot return any value.viso notbonut oi
• A function having void as its parameter list cannot accept any value. So the function declared as
void print (void);
or
void print()
does not accept any input/arguments from the calling function.
• If the function declaration does not specify any return type, then by default, the function returns an integer value. Therefore when a function is declared as
sum (int a, int b);
Then the function sum accepts two integer values from the calling function and in turn returns an integer value to the caller.
• Some compilers make it compulsory to declare the function before its usage while other compilers make it optional. However, it is a good practice to always declare the function before its usage as it allows error checking on the arguments in the function call.
Programming in C: Unit III (a): Functions : Tag: : Programming in C - Function Declaration/Function prototype
Programming in C
CS3251 2nd Semester CSE Dept 2021 | Regulation | 2nd Semester CSE Dept 2021 Regulation