Programming in C: Unit III (a): Functions

Return Statement

with Example C Programs

The return statement is used to terminate the execution of a function and returns control to the calling function.

RETURN STATEMENT

The return statement is used to terminate the execution of a function and returns control to the calling function. When the return statement is encountered, the program execution resumes in the calling function at the point immediately following the function call. Refer Figure 4.2 in which the control passes from the called function to the calling function when the return statement is encountered. A return statement may or may not return a value to the calling function. The syntax of return statement can be given as

return <expression>;

Programming Tip:

It is an error to use a return statement in a function that has void as its return type.

Here expression is placed in between angular brackets because specifying an expression is optional. The value of expression, if present, is returned to the calling function. However, in case expression is omitted, the return value of the function is undefined.

The expression, if present, is converted to the type returned by the function. A function that has void as its return type cannot return any value to the calling function. So in a function that has been declared with return type void, a return statement containing an expression generates a warning and the expression is not evaluated.

For functions that have no return statement, the control automatically returns to the calling function after the last statement of the called function is executed. In other words an implicit return takes place upon execution of the last statement of the called function, and control automatically returns to the calling function.

Note

The programmer may or may not place the expression in a return statement within parentheses.

By default, the return type of a function is int.

Programming Tip: When the value returned by a function is assigned to a variable, then the returned value is converted to the type of the variable receiving it.

A function may have more than one return statement. For example, consider the program given below.

#include <stdio.h>

#include <conio.h>

int check_relation (int a, int b);

int main()

{

int a=3, b=5, res;

clrscr();

res = check relation (a, b);

if (res ==0) // Test the returned value

 printf("\n EQUAL");

if (res==1)

printf("\n a is greater than b");

if (res==-1)

printf("\n a is less than b");

getch();

return 0;

}

int check_relation (int a, int b)

{

if (a==b)

return 0;

else if (a>b)

return 1;

else

return -1;

}

Output

a is less than b

Programming Tip: An error is generated when the function does not return data of the specified type.

In the above program there are multiple return statements, but only one of them will get executed depending upon the condition. The return statement, like the break statement, is used to cause premature termination of the function.

Programming Tip: A function that does not return any value cannot be placed on the right side of the assignment operator.

An expression appearing in a return statement is converted to the return type of the function in which the statement appears. If no implicit conversion is possible, the return statement is invalid. Since the return type of the function check_relation () is int, so the result either 0, 1, or -1 is evaluated as an integer.

We have mentioned earlier that the variables declared inside the function cease to exist after the last statement of the function is executed. So how can we return the value of sum to the program that adds two integers using a function? The answer to this question is that a copy of the value being returned is made automatically and this copy is available to the return point in the program.

Using Variable Number of Arguments

Some functions have a variable number of arguments and data types that cannot be known at the compile time. Typical examples of such functions include the printf() and scanf() functions. ANSI C offers a symbol called ellipsis to handle such functions. The ellipsis consists of three periods (...). It can be used as:

int func (char ch, ...);

The function declaration statement given above states that func is a function that has an arbitrary number and type of arguments. However, one must ensure that both the function declaration and function definition should use the ellipsis symbol.

Programming in C: Unit III (a): Functions : Tag: : with Example C Programs - Return Statement