All functions, except those of type void, return a value. This value is specified by the
return statement. In C, if a non-void function does not explicitly return a value via a
return statement, then a garbage value is returned. In C++, a non-void function must
contain a return statement that returns a value. That is, in C++, if a function is specified
as returning a value, any return statement within it must have a value associated with
it. However, if execution reaches the end of a non-void function, then a garbage value
is returned. Although this condition is not a syntax error, it is still a fundamental error
and should be avoided.
As long as a function is not declared as void, you may use it as an operand in an
expression. Therefore, each of the following expressions is valid:
x = power(y);
if(max(x,y) > 100) printf("greater");
for(ch=getchar(); isdigit(ch); ) ... ;
As a general rule, a function cannot be the target of an assignment. A statement
such as swap(x,y) = 100; /* incorrect statement */
is wrong. The C/C++ compiler will flag it as an error and will not compile a program
that contains it. (As is discussed in Part Two, C++ allows some interesting exceptions
to this general rule, enabling some types of functions to occur on the left side of an
assignment.)
When you write programs, your functions generally will be of three types. The
first type is simply computational. These functions are specifically designed to
perform operations on their arguments and return a value based on that operation.
A computational function is a "pure" function. Examples are the standard library
functions sqrt() and sin() , which compute the square root and sine of their arguments.
The second type of function manipulates information and returns a value that
simply indicates the success or failure of that manipulation. An example is the library
function fclose() , which is used to close a file. If the close operation is successful, the
function returns 0; if the operation is unsuccessful, it returns EOF.
The last type of function has no explicit return value. In essence, the function is
strictly procedural and produces no value. An example is exit() , which terminates a
program. All functions that do not return values should be declared as returning type
void. By declaring a function as void, you keep it from being used in an expression,
thus preventing accidental misuse.
Sometimes, functions that really don't produce an interesting result return
something anyway. For example, printf() returns the number of characters written.
Yet it would be unusual to find a program that actually checked this. In other words,
although all functions, except those of type void, return values, you don't have to use
the return value for anything. A common question concerning function return values
is, "Don't I have to assign this value to some variable since a value is being returned?"
The answer is no. If there is no assignment specified, the return value is simply
discarded. Consider the following program, which uses the function mul() :
#include <stdio.h>
int mul(int a, int b);
int main(void)
{
int x, y, z;
x = 10; y = 20;
z = mul(x, y); /* 1 */
printf("%d", mul(x,y)); /* 2 */
mul(x, y); /* 3 */
return 0;
}
int mul(int a, int b)
{
return a*b;
}
In line 1, the return value of mul() is assigned to z. In line 2, the return value is not
actually assigned, but it is used by the printf() function. Finally, in line 3, the return
value is lost because it is neither assigned to another variable nor used as part of an
expression.
No comments:
Post a Comment