Society & Lifestyle
|Computing Articles||Share This Page|
Handling of Functions
|by Sachin Mehta|
We have seen that C functions by default return int value. This is obvious to raise a question , whether there are other return types for functions. Yes there are, not only the basic data types like int, float, char but also user-defined types like structures and arrays.
For allowing other than the int return types we have the type-specifier for functions. This will help you to return other return types. We must do two things to enable a calling function to receive a non-integer value from a called function:
( NOTE: The following explanations are in accordance to the older method of C functions, which doesn't declare function arguments on the same line as the function header. The modern or ANSI method declares the function on the same line as function header. There are also a few other subtle but important distinctions in the two methods. The latter method we will see later.)
type-specifier function-name (argument list)
2. The called function must be declared at the start of the body in the calling function, like any other variable. This is to tell the calling function the type of data that the function is actually returning.
For example consider the following segment of program
float mul(t,e) //function definition
double div(i,o) //function definition
The declaration part of main function declares not only the variables but the functions mul and div as well. This only tells the compiler that mul return a float-type value and div a double-type value. Parentheses that follow mul and div specify that they are functions instead of variables.
If we have a mismatch between the type of data that the called function returns and the type of data that the calling function expects, we will have unpredictable results. We must, therefore be careful to make sure that both types are compatible.
In cases when the function doesn't return any values, in that situation we specify the return type as void. This has to be done in the declaration of the function in the main as well in the function header.
We will continue with few other function handling concepts and start on the topic of variables in functions next time.
|More by : Sachin Mehta|
|Views: 1748 Comments: 0|
|Top | Computing Articles|