# Functions
Functions
# 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](https://docs.qualcomm.com/doc/80-N3040-23/topic/cpp_stdarg.html#varying%20number%20of%20arguments). In this instance, the argument must be an
[rvalue expression](https://docs.qualcomm.com/doc/80-N3040-23/topic/cpp_express.html#rvalue%20expression). Hence:
- An integer argument type is promoted.
- An lvalue of type array of `Ty` becomes an rvalue of type
pointer to `Ty`.
- A function designator of type function returning `Ty` becomes
an rvalue of type pointer to function returning `Ty`.
- An argument of type *float* is converted to *double.*
A ***do* statement**
executes a statement one or more times, while its
[test-context expression](https://docs.qualcomm.com/doc/80-N3040-23/topic/cpp_function.html#test-context%20expression)
has a nonzero value:
do
statement
while (test);Copy to clipboard
An ***expression* statement**
evaluates an expression in a
[side-effects context](https://docs.qualcomm.com/doc/80-N3040-23/topic/cpp_function.html#side-effects%20context):
printf("hello\n"); call a function
y = m * x + b; store a value
++count; alter a stored valueCopy to clipboard
A ***for* statement**
executes a statement zero or more times, while the optional
[test-context expression](https://docs.qualcomm.com/doc/80-N3040-23/topic/cpp_function.html#test-context%20expression)
`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](https://docs.qualcomm.com/doc/80-N3040-23/topic/cpp_function.html#side-effects%20context):
for (se-1; test; se-2)
statementCopy to clipboard
An ***if* statement**
executes a statement only if the
[test-context expression](https://docs.qualcomm.com/doc/80-N3040-23/topic/cpp_function.html#test-context%20expression)
has a nonzero value:
if (test)
statementCopy to clipboard
An ***if-else* statement**
executes one of two statements, depending on whether the
[test-context expression](https://docs.qualcomm.com/doc/80-N3040-23/topic/cpp_function.html#test-context%20expression)
has a nonzero value:
if (test)
statement-1
else
statement-2Copy to clipboard
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](https://docs.qualcomm.com/doc/80-N3040-23/topic/cpp_express.html#rvalue%20expression)
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;Copy to clipboard
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
}Copy to clipboard
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)
statementCopy to clipboard
* * *
See also the
**[Table of Contents](https://docs.qualcomm.com/doc/80-N3040-23/topic/cpp_index.html#Table%20of%20Contents)** and the
**[Index](https://docs.qualcomm.com/doc/80-N3040-23/topic/_cpp_index.html)**.
*[Copyright](https://docs.qualcomm.com/doc/80-N3040-23/topic/cpp_crit_pjp.html) © 1992-2006by P.J. Plauger and Jim Brodie. All rights reserved.*
Last Published: Oct 06, 2026
[Previous Topic
STL Container](https://docs.qualcomm.com/bundle/publicresource/80-N3040-23/topics/cpp_lib_cont.md) [Next Topic
Expressions](https://docs.qualcomm.com/bundle/publicresource/80-N3040-23/topics/cpp_express.md)