Tuesday, June 8, 2010

*Handling of Functions

Handling of Functions

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.)
1. The explicit type-specifier, corresponding to the data type required must be mentioned in the function header. The general form of the function definition is

type-specifier function-name (argument list)
argument declaration;
{
function statements;
}
The type-specifier tells the compiler, the type of data type the function is to return.

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
main()
{
float x,a,b, mul(); //function declaration
double y, div(); //function declaration
//Program statements
x = mul(a,b); //function call
y = div(a,b); //function call
}

float mul(t,e) //function definition
float t,e;
{
// Body
}

double div(i,o) //function definition
float i,o;
{
//body
}

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.

Ravinder Singh
*ks*

No comments:

DESCLAIMER:

KIMONGSHAW (RAVINDER SINGH c/o Principal, GDC Chamba, Distt Chamba himachal pradesh 176311 india) ACKNOWLEDGES THAT THOUGH i TRY TO REPORT ACCURATELY, i CANNOT VERIFY THE ABSOLUTE FACTS OF EVERYTHING POSTED. POSTINGS MAY CONTAIN FACT, SPECULATION OR RUMOR. i FIND IMAGES FROM THE WEB THAT ARE BELIEVED TO BELONG IN THE PUBLIC DOMAIN. IF ANY STORIES OR IMAGES THAT APPEAR ON THE SITE ARE IN VIOLATION OF COPYRIGHT LAW, PLEASE EMAIL or call me AND i WILL REMOVE THE OFFENDING INFORMATION AS SOON AS POSSIBLE. contact me kimongshaw@yahoo.co.uk