[Previous] [Contents] [Next]

Caution: This version of this document is no longer maintained. For the latest documentation, see http://www.qnx.com/developers/docs.

Functions


You write functions to specify all the actions that a program performs when it executes. The type of a function tells you the type of result it returns (if any). It can also tell you the types of any arguments that the function expects when you call it from within an expression.

This document describes briefly just those aspect of functions most relevant to the use of the Standard C library:

Argument promotion occurs when the type of the function fails to provide any information about an argument. Promotion occurs if the function declaration is not a function prototype or if the argument is one of the unnamed arguments in a varying number of arguments. In this instance, the argument must be an rvalue expression. Hence:

A do statement executes a statement one or more times, while its test-context expression has a nonzero value:

    do
        statement
        while (test);

An expression statement evaluates an expression in a side-effects context:

    printf("hello\n");            call a function
    y = m * x + b;                store a value
    ++count;                      alter a stored value

A for statement executes a statement zero or more times, while the optional test-context expression test has a nonzero value. You can also write two expressions, se-1 and se-2, in a for statement that are each in a side-effects context:

    for (se-1; test; se-2)
        statement

An if statement executes a statement only if the test-context expression has a nonzero value:

    if (test)
        statement

An if-else statement executes one of two statements, depending on whether the test-context expression has a nonzero value:

    if (test)
        statement-1
    else
        statement-2

A return statement terminates execution of the function and transfers control to the expression that called the function. If you write the optional rvalue expression within the return statement, the result must be assignment-compatible with the type returned by the function. The program converts the value of the expression to the type returned and returns it as the value of the function call:

    return expression;

An expression that occurs in a side-effects context specifies no value and designates no object or function. Hence, it can have type void. You typically evaluate such an expression for its side effects -- any change in the state of the program that occurs when evaluating an expression. Side effects occur when the program stores a value in an object, accesses a value from an object of volatile qualified type, or alters the state of a file.

A switch statement jumps to a place within a controlled statement, depending on the value of an integer expression:

    switch (expr)
        {
    case val-1:
        stat-1;
        break;
    case val-2:
        stat-2;            falls through to next
    default:
        stat-n
    }

In a test-context expression the value of an expression causes control to flow one way within the statement if the computed value is nonzero or another way if the computed value is zero. You can write only an expression that has a scalar rvalue result, because only scalars can be compared with zero.

A while statement executes a statement zero or more times, while the test-context expression has a nonzero value:

    while (test)
        statement

See also the Table of Contents and the Index.

Copyright © 1989-2002 by P.J. Plauger and Jim Brodie. All rights reserved.

[Previous] [Contents] [Next]