1030 lines
39 KiB
HTML
1030 lines
39 KiB
HTML
<HTML><HEAD><TITLE>Functions</TITLE></HEAD><BODY BGCOLOR="#FFFFFF">
|
|
|
|
<H1><A NAME="Functions">Functions</A></H1><HR>
|
|
|
|
<P><B><A HREF="#Function Declarations">Function Declarations</A>
|
|
· <A HREF="#Function Definitions">Function Definitions</A>
|
|
· <A HREF="#Statements">Statements</A>
|
|
· <A HREF="#Expression Contexts">Expression Contexts</A>
|
|
· <A HREF="#Block">Block</A>
|
|
· <A HREF="#Break Statement">Break Statement</A>
|
|
· <A HREF="#Case Label">Case Label</A>
|
|
· <A HREF="#Continue Statement">Continue Statement</A>
|
|
· <A HREF="#Default Label">Default Label</A>
|
|
· <A HREF="#Do Statement">Do Statement</A>
|
|
· <A HREF="#Expression Statement">Expression Statement</A>
|
|
· <A HREF="#For Statement">For Statement</A>
|
|
· <A HREF="#Goto Label">Goto Label</A>
|
|
· <A HREF="#Goto Statement">Goto Statement</A>
|
|
· <A HREF="#If Statement">If Statement</A>
|
|
· <A HREF="#If-Else Statement">If-Else Statement</A>
|
|
· <A HREF="#Null Statement">Null Statement</A>
|
|
· <A HREF="#Return Statement">Return Statement</A>
|
|
· <A HREF="#Switch Statement">Switch Statement</A>
|
|
· <A HREF="#While Statement">While Statement</A>
|
|
· <A HREF="#Function Calls">Function Calls</A>
|
|
</B></P>
|
|
<HR>
|
|
|
|
<P>You write <B>functions</B> 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.</P>
|
|
|
|
<P>This document shows how to declare a function. It describes all
|
|
the statements you use to specify the actions
|
|
that the function performs. And it shows what happens when you call
|
|
a function.</P>
|
|
|
|
<H2><A NAME="Function Declarations">Function Declarations</A></H2>
|
|
|
|
<P>When you declare a function, you specify the type of result
|
|
it returns. If the function does not return a value, then you declare
|
|
it to return a
|
|
<I><A HREF="types.html#void types" tppabs="http://ccs.ucsd.edu/c/types.html#void types">void</A></I> type.
|
|
Otherwise, a function can
|
|
return any object or incomplete type except an array type or a bitfield
|
|
type. (The type must be complete before any
|
|
<A HREF="#Function Calls">call</A> to the function.)</P>
|
|
|
|
<P>You can also declare the types of the arguments that the function
|
|
expects. You write a list of one or more declarations separated by
|
|
commas and enclosed within the parentheses of the function decoration.
|
|
If the function does not expect any arguments, you write only the
|
|
keyword <CODE>void</CODE>.</P>
|
|
|
|
<P>For example:</P>
|
|
|
|
<PRE>
|
|
void reset(void); <B>no arguments, no return</B>
|
|
double base_val(void); <B>no arguments, double return</B></PRE>
|
|
|
|
<P>If the function expects a fixed number of arguments,
|
|
you declare a corresponding
|
|
<B><A NAME="function parameter">function parameter</A></B>
|
|
for each of them. You list the parameter
|
|
declarations in the same order that the arguments appear in a call
|
|
to the function. You can omit the names of any of the parameters if
|
|
you are not also defining the function.</P>
|
|
|
|
<PRE>
|
|
void seed(int val); <B>one int argument</B>
|
|
int max(int, int); <B>two int arguments</B></PRE>
|
|
|
|
<P>The translator
|
|
<B><A NAME="parameter conversion">converts</A></B>
|
|
a parameter declared with type <I>array
|
|
of T</I> to type <I>pointer to T.</I> It converts a parameter declared
|
|
with type <I>function returning T</I> to type <I>pointer to function
|
|
returning T.</I> Otherwise, each parameter must have an
|
|
<A HREF="types.html#object types" tppabs="http://ccs.ucsd.edu/c/types.html#object types">object type</A>.</P>
|
|
|
|
<PRE>
|
|
int scanx(char a[]); <B>changed to char *a</B>
|
|
void callit(int f(void)); <B>changed to int (*f)(void)</B></PRE>
|
|
|
|
<P>If the function expects a
|
|
<A HREF="stdarg.html#varying number of arguments" tppabs="http://ccs.ucsd.edu/c/stdarg.html#varying number of arguments">varying number
|
|
of arguments</A>, you end the list of parameters
|
|
with an ellipsis (<CODE>...</CODE>). You must write
|
|
at least one parameter declaration before the ellipsis.</P>
|
|
|
|
<PRE>
|
|
char *copy(char *s, ...); <B>one or more arguments</B></PRE>
|
|
|
|
<P>Here, the function <CODE>copy</CODE> has a mandatory argument of type
|
|
<I>pointer to char.</I> It can also accept zero or more additional
|
|
arguments whose number and types are unspecified.</P>
|
|
|
|
<P>All the function declarations shown above that provide type
|
|
information about the arguments within the function decoration are called
|
|
<B><A NAME="function prototypes">function prototypes</A></B>.</P>
|
|
|
|
<P>You can also declare a function and not provide information
|
|
about the number or types of its arguments. Do not write declarations
|
|
within the parentheses of the function decoration.</P>
|
|
|
|
<PRE>
|
|
double bessel(); <B>no argument information</B></PRE>
|
|
|
|
<P>Here, the function <CODE>bessel</CODE> has some fixed, but unspecified,
|
|
number of arguments, whose types are also unspecified.</P>
|
|
|
|
<P>You can create an
|
|
<B><A NAME="implicit declaration">implicit declaration</A></B>
|
|
for a function within an expression. If the left operand of a
|
|
<A HREF="express.html#Function Call" tppabs="http://ccs.ucsd.edu/c/express.html#Function Call">function call</A> operator is a name
|
|
with no visible declaration as a function, object, enumeration constant,
|
|
or type definition, then the translator declares it in the current
|
|
name space as a <I>function returning int</I> without argument information.
|
|
The name has external linkage. It is much better, however, to declare
|
|
all functions explicitly before you call them.</P>
|
|
|
|
<PRE>
|
|
y = min(a, b); <B>implies extern int min();</B></PRE>
|
|
|
|
<P>The translator uses argument type information to check and to
|
|
<A HREF="#argument conversion">convert</A> argument expressions
|
|
that you write when you call the function.
|
|
The behavior is as if the argument value is assigned to the object
|
|
corresponding to the parameter. When you specify no type information
|
|
for an argument, the translator determines its type by
|
|
<A HREF="#argument promotion">promoting</A> the type
|
|
of the argument expression.</P>
|
|
|
|
<H2><A NAME="Function Definitions">Function Definitions</A></H2>
|
|
|
|
<P>You define a function by writing a <I>function definition,</I>
|
|
a special form of declaration that ends with a
|
|
<A HREF="#Block">block</A>:</P>
|
|
|
|
<P><IMG SRC="fun_def.gif" tppabs="http://ccs.ucsd.edu/c/gif/fun_def.gif"></P>
|
|
|
|
<P>Within the block you write any
|
|
<A HREF="declare.html#Block Declaration" tppabs="http://ccs.ucsd.edu/c/declare.html#Block Declaration">declarations</A>
|
|
visible only within the function, and the sequence of
|
|
<A HREF="#Statements">statements</A> that specifies the actions that the
|
|
function performs when you execute it. Any statement can be another
|
|
block, containing additional declarations and statements.</P>
|
|
|
|
<P>The <A HREF="syntax.html#Declarators" tppabs="http://ccs.ucsd.edu/c/syntax.html#Declarators">declarator</A>
|
|
part of a function definition must contain a
|
|
name for the function. The name must have a
|
|
<A HREF="types.html#Function Types" tppabs="http://ccs.ucsd.edu/c/types.html#Function Types">function type</A>. The declarator
|
|
must also contain a
|
|
<A HREF="syntax.html#function decoration" tppabs="http://ccs.ucsd.edu/c/syntax.html#function decoration">function decoration</A>
|
|
that names the parameters for the function. In a
|
|
<A HREF="#function prototypes">function prototype</A> that is also
|
|
a function definition, you cannot omit any of the parameter names.
|
|
Some examples are:</P>
|
|
|
|
<PRE>
|
|
int min(int a, int b)
|
|
{
|
|
return (a < b ? a : b);
|
|
}</PRE>
|
|
|
|
void swap(char *x, char *y)
|
|
{
|
|
char t;
|
|
t = *x, *x = *y, *y = t;
|
|
}
|
|
|
|
<P>Here, the function definitions for both
|
|
<CODE>min</CODE> and <CODE>swap</CODE>
|
|
also serve as function prototypes. Wherever these names are visible
|
|
later in the translation unit, the translator uses the argument type
|
|
information to check and convert argument expressions on any calls
|
|
to these functions. </P>
|
|
|
|
<P>You can also define a function and not provide argument information.
|
|
(Do not use this capability in programs that you write: It is retained
|
|
in Standard C only to support programs written in older C dialects.)</P>
|
|
|
|
<P>You define a function without arguments by writing a function
|
|
decoration with empty parentheses. For example:</P>
|
|
|
|
<PRE>
|
|
void clear_error() <B>no arguments, no information</B>
|
|
{errno = 0; }</PRE>
|
|
|
|
<P>You define a function with arguments that provides no argument
|
|
information for subsequent checking and conversion during function
|
|
calls by writing a list of parameter names within the function decoration.
|
|
You declare the parameters in a sequence of zero or more parameter
|
|
declarations before the block part of the function definition.</P>
|
|
|
|
<PRE>
|
|
long lmax(a, b)
|
|
long a, b;
|
|
{return (a < b ? b : a); }</PRE>
|
|
|
|
<P>You can declare the parameters in any order. You declare each
|
|
parameter no more than once. If you do not declare a parameter, the
|
|
translator takes its type as <I>int.</I> To avoid an ambiguity, do
|
|
not write a parameter name that is visible as a type name.</P>
|
|
|
|
<P>A function that you define without parameter information is
|
|
<A HREF="types.html#Compatible and Composite Types" tppabs="http://ccs.ucsd.edu/c/types.html#Compatible and Composite Types">compatible</A>
|
|
with a function prototype that specifies:</P>
|
|
|
|
<UL>
|
|
<LI>a compatible return type</LI>
|
|
|
|
<LI>the same (fixed) number of arguments</LI>
|
|
|
|
<LI>a parameter of
|
|
<A HREF="#argument promotion">promoted</A> type
|
|
for each parameter in the definition</LI>
|
|
</UL>
|
|
|
|
<H2><A NAME="Statements">Statements</A></H2>
|
|
|
|
<P>You express the actions that a function performs by writing
|
|
<B>statements</B> within the
|
|
<A HREF="#Block">block</A> part of a
|
|
<A HREF="declare.html#Function Definition" tppabs="http://ccs.ucsd.edu/c/declare.html#Function Definition">function definition</A>.
|
|
Statements evaluate expressions and determine
|
|
<B><A NAME="flow of control">flow of control</A></B> through
|
|
a function. This section describes each statement and how it determines
|
|
flow of control.</P>
|
|
|
|
<P>When you
|
|
<A HREF="#Function Calls">call</A> a function,
|
|
control passes to the first statement
|
|
within the block part of a function definition. Except for the
|
|
<B><A NAME="jump statement"><I>jump</I> statements</A></B>
|
|
(<I><A HREF="#Break Statement">break</A>,
|
|
<A HREF="#Continue Statement">continue</A>,
|
|
<A HREF="#Goto Statement">goto</A>,</I> and
|
|
<I><A HREF="#Return Statement">return</A></I>), each
|
|
statement within a block passes control (after it has completed its
|
|
execution) to the next statement within the block. Some statements
|
|
can execute a contained statement repeatedly, and some can execute
|
|
a contained statement only when a certain condition is true, but all
|
|
these statements pass control to the next statement within the block,
|
|
if any. If a next statement is not within the block, control passes
|
|
to the statement following the block.</P>
|
|
|
|
<P>Because no statement follows the block part of a function definition,
|
|
the translator inserts a <I>return</I> statement (without an expression)
|
|
at the end of that block. A <I>return</I> statement returns control
|
|
to the expression that invoked the function.</P>
|
|
|
|
<P>You can write a sequence of
|
|
<A HREF="declare.html#Block Declaration" tppabs="http://ccs.ucsd.edu/c/declare.html#Block Declaration">declarations</A>
|
|
at the beginning of each block.
|
|
When control enters the block, the program allocates any
|
|
objects that you declare within the block with
|
|
<A HREF="declare.html#dynamic duration" tppabs="http://ccs.ucsd.edu/c/declare.html#dynamic duration">dynamic duration</A>. The
|
|
program allocates these objects even if control enters the block via
|
|
a jump to some form of label
|
|
(<I><A HREF="#Case Label">case</A>,
|
|
<A HREF="#Default Label">default</A>,</I> or
|
|
<I><A HREF="#Goto Label">goto</A></I>)
|
|
within the block.</P>
|
|
|
|
<P>A <A HREF="declare.html#dynamic initializer" tppabs="http://ccs.ucsd.edu/c/declare.html#dynamic initializer">dynamic initializer</A>
|
|
behaves just like an
|
|
<A HREF="#Expression Statement"><I>expression</I> statement</A>
|
|
that assigns the initializer to the object that you declare.
|
|
Any dynamic initializers that you specify within a block form a sequence
|
|
of statements that the translator prefixes to the sequence of statements
|
|
within the block. If control enters the block via a jump to some form
|
|
of label within the block, these initializers are not executed.</P>
|
|
|
|
<P>In the descriptions that follow, a syntax diagram shows how
|
|
to write each statement. A verbal description tells what the statement
|
|
does, and then a
|
|
<B><A NAME="flowchart">flowchart</A></B> illustrates the
|
|
<A HREF="#flow of control">flow of control</A>
|
|
through the statement:</P>
|
|
|
|
<UL>
|
|
<LI>Control enters the statement from the previous statement along
|
|
the arrow leading in from the left margin.</LI>
|
|
|
|
<LI>Control passes to the next statement along an arrow leading
|
|
out to the right margin.</LI>
|
|
</UL>
|
|
|
|
<P>A <A HREF="#jump statement"><I>jump</I> statement</A>
|
|
causes control to pass to another designated target.</P>
|
|
|
|
<H3><A NAME="Expression Contexts">Expression Contexts</A></H3>
|
|
|
|
<P>Expressions appear in three different contexts within statements:</P>
|
|
|
|
<UL>
|
|
<LI>a <A HREF="#test context">test context</A></LI>
|
|
|
|
<LI>a <A HREF="#value context">value context</A></LI>
|
|
|
|
<LI>a <A HREF="#side-effects context">side-effects context</A>
|
|
</UL>
|
|
|
|
<P>In a <B><A NAME="test context">test context</A></B>,
|
|
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
|
|
<A HREF="types.html#scalar types" tppabs="http://ccs.ucsd.edu/c/types.html#scalar types">scalar</A>
|
|
<A HREF="express.html#rvalue expression" tppabs="http://ccs.ucsd.edu/c/express.html#rvalue expression">rvalue</A> result,
|
|
because only scalars can be compared with zero.
|
|
A test-context expression appears within a
|
|
flowchart inside a diamond that has one arrow entering and two arrows
|
|
leaving it.</P>
|
|
|
|
<P>In a <B><A NAME="value context">value context</A></B>,
|
|
the program makes use of the value of an expression. A
|
|
<A HREF="#Return Statement"><I>return</I> statement</A>,
|
|
for example, returns the value
|
|
of any expression you write as the value of the function. You can
|
|
write only an expression with a result that the translator can convert
|
|
to an rvalue whose type is
|
|
<A HREF="types.html#assignment compatible" tppabs="http://ccs.ucsd.edu/c/types.html#assignment compatible">assignment compatible</A>
|
|
with the type required by the context.
|
|
A value-context expression appears within a flowchart
|
|
inside a rectangle with one arrow entering it and one arrow leaving
|
|
it. (It does not alter the flow of control.)</P>
|
|
|
|
<P>In a <B><A NAME="side-effects context">side-effects context</A></B>,
|
|
the program evaluates an expression only for its
|
|
<B><A NAME="side effects">side effects</A></B>.
|
|
A side effect is a change in the state of the program
|
|
that occurs when evaluating an expression. Side effects occur when
|
|
the program:</P>
|
|
|
|
<UL>
|
|
<LI>stores a value in an object</LI>
|
|
|
|
<LI>accesses a value from an object of <I>volatile</I> qualified
|
|
type</LI>
|
|
|
|
<LI>alters the state of a file</LI>
|
|
</UL>
|
|
|
|
<P>You can write only a
|
|
<A HREF="express.html#void expression" tppabs="http://ccs.ucsd.edu/c/express.html#void expression"><I>void</I> expression</A>
|
|
(an arbitrary expression that computes no useful value or discards
|
|
any value that it computes) or an expression that the translator can
|
|
convert to a <I>void</I> result. A side-effects context expression
|
|
appears within a flowchart inside a rectangle with one arrow entering
|
|
it and one arrow leaving it. (It does not alter the flow of control.)</P>
|
|
|
|
<H2><A NAME="Block">Block</A></H2>
|
|
|
|
<P>A <B>block</B> lets you write a series of
|
|
<A HREF="declare.html#Block Declaration" tppabs="http://ccs.ucsd.edu/c/declare.html#Block Declaration">declarations</A>,
|
|
followed by a series of statements,
|
|
in a context where the translator permits only a single statement.</P>
|
|
|
|
<P><IMG SRC="block.gif" tppabs="http://ccs.ucsd.edu/c/gif/block.gif"></P>
|
|
|
|
<P>You also use it to limit the visibility or duration of declarations
|
|
used only within the block. Using the notation:</P>
|
|
|
|
<PRE>
|
|
{ <I>decl-1; decl-2; ... decl-n;
|
|
stat-1; stat-2; ... stat-n;</I> }</PRE>
|
|
|
|
<P>the flowchart for a typical block is:</P>
|
|
|
|
<P><IMG SRC="block_f.gif" tppabs="http://ccs.ucsd.edu/c/gif/block_f.gif"></P>
|
|
|
|
<P>For example:</P>
|
|
|
|
<PRE>
|
|
if ((c = getchar()) != EOF)
|
|
{
|
|
putchar(c);
|
|
++nc;
|
|
}</PRE>
|
|
|
|
<H2><A NAME="Break Statement">Break Statement</A></H2>
|
|
|
|
<P>A <B>break</B> statement transfers control to the statement
|
|
following the innermost
|
|
<I><A HREF="#Do Statement">do</A>,
|
|
<A HREF="#For Statement">for</A>,
|
|
<A HREF="#Switch Statement">switch</A>,</I> or
|
|
<A HREF="#While Statement"><I>while</I> statement</A> that contains the
|
|
<I>break</I> statement. You can write a <I>break</I> statement only
|
|
within one of these statements.</P>
|
|
|
|
<P><IMG SRC="break_s.gif" tppabs="http://ccs.ucsd.edu/c/gif/break_s.gif"></P>
|
|
|
|
<P>The flowchart for a <I>break</I> statement is:</P>
|
|
|
|
<P><IMG SRC="break_f.gif" tppabs="http://ccs.ucsd.edu/c/gif/break_f.gif"></P>
|
|
|
|
<P>For example:</P>
|
|
|
|
<PRE>
|
|
for (s = first; s[0]; ++s)
|
|
if (s[0] == escape && s[1] == wanted)
|
|
break; <B>leave the for statement</B></PRE>
|
|
|
|
<H2><A NAME="Case Label">Case Label</A></H2>
|
|
|
|
<P>A <B>case</B> label serves as a target within a
|
|
<A HREF="#Switch Statement"><I>switch</I> statement</A>. It has
|
|
no other effect on the
|
|
<A HREF="#flow of control">flow of control</A>,
|
|
nor does it perform any action. The expression is in a
|
|
<A HREF="#value context">value context</A> and must be an
|
|
<A HREF="express.html#integer constant expression" tppabs="http://ccs.ucsd.edu/c/express.html#integer constant expression">integer
|
|
constant expression</A>.</P>
|
|
|
|
<P><IMG SRC="casel_s.gif" tppabs="http://ccs.ucsd.edu/c/gif/casel_s.gif"></P>
|
|
|
|
<P>The flowchart for a <I>case</I> label is:</P>
|
|
|
|
<P><IMG SRC="casel_f.gif" tppabs="http://ccs.ucsd.edu/c/gif/casel_f.gif"></P>
|
|
|
|
<P>For example:</P>
|
|
|
|
<PRE>
|
|
switch (c = getchar())
|
|
{
|
|
case EOF:
|
|
return;
|
|
case ' ':
|
|
case '\n':
|
|
break;
|
|
default:
|
|
process(c);
|
|
}</PRE>
|
|
|
|
<H2><A NAME="Continue Statement">Continue Statement</A></H2>
|
|
|
|
<P>A <B>continue</B> statement transfers control
|
|
out of the statement controlled by the innermost
|
|
<I><A HREF="#Do Statement">do</A>,
|
|
<A HREF="#For Statement">for</A>,</I> or
|
|
<A HREF="#While Statement"><I>while</I> statement</A>
|
|
that contains the <I>continue</I>
|
|
statement. It begins the next iteration of the loop. You can write
|
|
a <I>continue</I> statement only within one of these statements.</P>
|
|
|
|
<P><IMG SRC="cont_s.gif" tppabs="http://ccs.ucsd.edu/c/gif/cont_s.gif"></P>
|
|
|
|
<P>The flowchart for a <I>continue</I> statement is:</P>
|
|
|
|
<P><IMG SRC="cont_f.gif" tppabs="http://ccs.ucsd.edu/c/gif/cont_f.gif"></P>
|
|
|
|
<P>For example:</P>
|
|
|
|
<PRE>
|
|
for (p = head; p; p = p->next)
|
|
{
|
|
if (p->type != wanted)
|
|
continue;
|
|
process(p);
|
|
}</PRE>
|
|
|
|
<H2><A NAME="Default Label">Default Label</A></H2>
|
|
|
|
<P>A <B>default</B> label serves as a target within a
|
|
<A HREF="#Switch Statement"><I>switch</I> statement</A>.
|
|
Otherwise, it has no effect on the
|
|
<A HREF="#flow of control">flow of control</A>, nor does it perform
|
|
any action.</P>
|
|
|
|
<P><IMG SRC="def_s.gif" tppabs="http://ccs.ucsd.edu/c/gif/def_s.gif"></P>
|
|
|
|
<P>The flowchart for a <I>default</I> label is:</P>
|
|
|
|
<P><IMG SRC="def_f.gif" tppabs="http://ccs.ucsd.edu/c/gif/def_f.gif"></P>
|
|
|
|
<P>For example:</P>
|
|
|
|
<PRE>
|
|
switch (lo = strtol(s, NULL, 10))
|
|
{
|
|
case LONG_MIN:
|
|
case LONG_MAX:
|
|
if (errno == ERANGE)
|
|
oflo = YES;
|
|
default:
|
|
return (lo);
|
|
}</PRE>
|
|
|
|
<H2><A NAME="Do Statement">Do Statement</A></H2>
|
|
|
|
<P>A <B>do</B> statement executes a statement one or more times, while the
|
|
<A HREF="#test context">test-context</A> expression has a nonzero value.</P>
|
|
|
|
<P><IMG SRC="do_s.gif" tppabs="http://ccs.ucsd.edu/c/gif/do_s.gif"></P>
|
|
|
|
<P>Using the notation:</P>
|
|
|
|
<PRE>
|
|
do
|
|
<I>statement</I>
|
|
while (<I>test</I>);</PRE>
|
|
|
|
<P>the flowchart for a <I>do</I> statement is:</P>
|
|
|
|
<P><IMG SRC="do_f.gif" tppabs="http://ccs.ucsd.edu/c/gif/do_f.gif"></P>
|
|
|
|
<P>If the program executes a
|
|
<A HREF="#Break Statement"><I>break</I></A> statement within the
|
|
controlled statement, control transfers to the statement following
|
|
the <I>do</I> statement. A <I>break</I> statement for this <I>do</I>
|
|
statement can be contained within another statement (inside the controlled
|
|
statement) but not within an inner <I>do,
|
|
<A HREF="#For Statement">for</A>,
|
|
<A HREF="#Switch Statement">switch</A>,</I> or
|
|
<A HREF="#While Statement"><I>while</I> statement</A>.</P>
|
|
|
|
<P>If the program executes a
|
|
<A HREF="#Continue Statement"><I>continue</I> statement</A> within the
|
|
controlled statement, control transfers to the
|
|
<A HREF="#test context">test-context</A> expression
|
|
in the <I>do</I> statement. A <I>continue</I> statement for this <I>do</I>
|
|
statement can be contained within another statement (inside the controlled
|
|
statement) but not within an inner <I>do, for,</I> or <I>while</I>
|
|
statement.</P>
|
|
|
|
<P>For example:</P>
|
|
|
|
<PRE>
|
|
do
|
|
putchar(' ');
|
|
while (++col % cols_per_tab);</PRE>
|
|
|
|
<H2><A NAME="Expression Statement">Expression Statement</A></H2>
|
|
|
|
<P>An <B>expression</B> statement evaluates an expression in a
|
|
<A HREF="#side-effects context">side-effects</A> context.</P>
|
|
|
|
<P><IMG SRC="expr_s.gif" tppabs="http://ccs.ucsd.edu/c/gif/expr_s.gif"></P>
|
|
|
|
<P>The flowchart for an <I>expression</I> statement is:</P>
|
|
|
|
<P><IMG SRC="expr_f.gif" tppabs="http://ccs.ucsd.edu/c/gif/expr_f.gif"></P>
|
|
|
|
<P>For example:</P>
|
|
|
|
<PRE>
|
|
printf("hello\n"); <B>call a function</B>
|
|
y = m * x + b; <B>store a value</B>
|
|
++count; <B>alter a stored value</B></PRE>
|
|
|
|
<H2><A NAME="For Statement">For Statement</A></H2>
|
|
|
|
<P>A <B>for</B> statement executes a statement zero or more times,
|
|
while the optional
|
|
<A HREF="#test context">test-context</A> expression
|
|
has a nonzero value. You can also write two
|
|
<A HREF="#side-effects context">side-effects context</A>
|
|
expressions in a <I>for</I> statement.</P>
|
|
|
|
<P><IMG SRC="for_s.gif" tppabs="http://ccs.ucsd.edu/c/gif/for_s.gif"></P>
|
|
|
|
<P>The program executes the optional expression,
|
|
called <CODE><I>se-1</I></CODE>
|
|
below, before it first evaluates the test-context expression. (This
|
|
is typically a loop initializer of some form.) The program executes
|
|
the optional expression, called <CODE><I>se-2</I></CODE> below,
|
|
after it executes the controlled statement each time.
|
|
(This is typically an expression
|
|
that prepares for the next iteration of the loop.) If you write no
|
|
test-context expression, the translator uses the expression <CODE>1</CODE>,
|
|
and therefore executes the statement indefinitely.</P>
|
|
|
|
<P>Using the notation:</P>
|
|
|
|
<PRE>
|
|
for (<I>se-1</I>; <I>test</I>; <I>se-2</I>)
|
|
<I>statement</I></PRE>
|
|
|
|
<P>the flowchart for a <I>for</I> statement is:</P>
|
|
|
|
<P><IMG SRC="for_f.gif" tppabs="http://ccs.ucsd.edu/c/gif/for_f.gif"></P>
|
|
|
|
<P>If the program executes a
|
|
<A HREF="#Break Statement"><I>break</I></A> statement within the
|
|
controlled statement, control transfers to the statement following
|
|
the <I>for</I> statement. A <I>break</I> statement for this <I>for</I>
|
|
statement can be contained within another statement (inside the controlled
|
|
statement) but not within an inner
|
|
<I><A HREF="#Do Statement">do</A>, for,
|
|
<A HREF="#Switch Statement">switch</A>,</I> or
|
|
<A HREF="#While Statement"><I>while</I> statement</A>.</P>
|
|
|
|
<P>If the program executes a
|
|
<A HREF="#Continue Statement"><I>continue</I></A> statement within the
|
|
controlled statement, control transfers to the expression that the
|
|
program executes after it executes the controlled statement
|
|
(<CODE><I>se-2</I></CODE> above). A <I>continue</I> statement
|
|
for this <I>for</I> statement can be contained within another statement
|
|
(inside the controlled statement)
|
|
but not within an inner <I>do, for,</I> or <I>while</I> statement.</P>
|
|
|
|
<P>For example:</P>
|
|
|
|
<PRE>
|
|
for (i = 0; i < sizeof a / sizeof a[0]; ++i)
|
|
process(a[i]); <B>for each array element</B>
|
|
|
|
for (p = head; p; p = p->next)
|
|
process(p); <B>for each linked list item</B>
|
|
|
|
for (; ; ) <B>forever</B>
|
|
do_x(get_x());</PRE>
|
|
|
|
<H2><A NAME="Goto Label">Goto Label</A></H2>
|
|
|
|
<P>A <B>goto</B> label serves as the target for a
|
|
<A HREF="#Goto Statement"><I>goto</I> statement</A>. It has
|
|
no other effect on the
|
|
<A HREF="#flow of control">flow of control</A>,
|
|
nor does it perform any action.
|
|
Do not write two <I>goto</I> labels within the same function that
|
|
have the <A HREF="declare.html#Name Spaces" tppabs="http://ccs.ucsd.edu/c/declare.html#Name Spaces">same name</A>.</P>
|
|
|
|
<P><IMG SRC="gotol_s.gif" tppabs="http://ccs.ucsd.edu/c/gif/gotol_s.gif"></P>
|
|
|
|
<P>The flowchart for a <I>goto</I> label is:</P>
|
|
|
|
<P><IMG SRC="gotol_f.gif" tppabs="http://ccs.ucsd.edu/c/gif/gotol_f.gif"></P>
|
|
|
|
<P>For example:</P>
|
|
|
|
<PRE>
|
|
panic: <B>jump here if hopeless</B>
|
|
printf("PANIC!\n");
|
|
close_all();
|
|
exit(EXIT_FAILURE);</PRE>
|
|
|
|
<H2><A NAME="Goto Statement">Goto Statement</A></H2>
|
|
|
|
<P>A <B>goto</B> statement transfers control to the
|
|
<A HREF="#Goto Label"><I>goto</I> label</A> (in the same function)
|
|
named in the <I>goto</I> statement.</P>
|
|
|
|
<P><IMG SRC="goto_s.gif" tppabs="http://ccs.ucsd.edu/c/gif/goto_s.gif"></P>
|
|
|
|
<P>The flowchart for a <I>goto</I> statement is:</P>
|
|
|
|
<P><IMG SRC="goto_f.gif" tppabs="http://ccs.ucsd.edu/c/gif/goto_f.gif"></P>
|
|
|
|
<P>For example:</P>
|
|
|
|
<PRE>
|
|
if (MAX_ERRORS <= nerrors)
|
|
goto panic;</PRE>
|
|
|
|
<H2><A NAME="If Statement">If Statement</A></H2>
|
|
|
|
<P>An <B>if</B> statement executes a statement only if the
|
|
<A HREF="#test context">test-context</A> expression has a nonzero value.</P>
|
|
|
|
<P><IMG SRC="if_s.gif" tppabs="http://ccs.ucsd.edu/c/gif/if_s.gif"></P>
|
|
|
|
<P>Using the notation:</P>
|
|
|
|
<PRE>
|
|
if (<I>test</I>)
|
|
<I>statement</I>;</PRE>
|
|
|
|
<P>the flowchart for an <I>if</I> statement is:</P>
|
|
|
|
<P><IMG SRC="if_f.gif" tppabs="http://ccs.ucsd.edu/c/gif/if_f.gif"></P>
|
|
|
|
<P>For example:</P>
|
|
|
|
<PRE>
|
|
int t;
|
|
if (a < b)
|
|
t = a, a = b, b = t; <B>swap a and b</PRE>
|
|
|
|
<H2><A NAME="If-Else Statement">If-Else Statement</A></H2>
|
|
|
|
<P>An <B>if-else</B> statement executes one of two statements,
|
|
depending on whether the
|
|
<A HREF="#test context">test-context</A> expression
|
|
has a nonzero value.</P>
|
|
|
|
<P><IMG SRC="ifelse_s.gif" tppabs="http://ccs.ucsd.edu/c/gif/ifelse_s.gif"></P>
|
|
|
|
<P>Using the notation:</P>
|
|
|
|
<PRE>
|
|
if (<I>test</I>)
|
|
<I>statement-1</I>
|
|
else
|
|
<I>statement-2</I></PRE>
|
|
|
|
<P>the flowchart for an <I>if-else</I> statement is:</P>
|
|
|
|
<P><IMG SRC="ifelse_f.gif" tppabs="http://ccs.ucsd.edu/c/gif/ifelse_f.gif"></P>
|
|
|
|
<P>For example:</P>
|
|
|
|
<PRE>
|
|
if (min < 0) <B>do one of three cases</B>
|
|
printf("loss is %d\n", -min);
|
|
else if (min == 0) <B>second if-else statement</B>
|
|
printf("break even\n");
|
|
else
|
|
printf("gain is %d\n", min);</PRE>
|
|
|
|
<H2><A NAME="Null Statement">Null Statement</A></H2>
|
|
|
|
<P>A <B>null</B> statement does nothing. You use it where
|
|
the translator requires a statement but you do not want
|
|
to perform an action.</P>
|
|
|
|
<P><IMG SRC="null_s.gif" tppabs="http://ccs.ucsd.edu/c/gif/null_s.gif"></P>
|
|
|
|
<P>The flowchart for a <I>null</I> statement is:</P>
|
|
|
|
<P><IMG SRC="null_f.gif" tppabs="http://ccs.ucsd.edu/c/gif/null_f.gif"></P>
|
|
|
|
<P>For example:</P>
|
|
|
|
<PRE>
|
|
if (done)
|
|
while (getchar() != EOF)
|
|
; <B>nothing else to do</B></PRE>
|
|
|
|
<H2><A NAME="Return Statement">Return Statement</A></H2>
|
|
|
|
<P>A <B>return</B> statement terminates execution
|
|
of the function and transfers control
|
|
to the expression that called the function.
|
|
If you write the optional expression (a
|
|
<A HREF="#value context">value-context</A> expression)
|
|
within the <I>return</I> statement, the
|
|
<A HREF="express.html#rvalue expression" tppabs="http://ccs.ucsd.edu/c/express.html#rvalue expression">rvalue</A> result must be
|
|
<A HREF="types.html#assignment compatible" tppabs="http://ccs.ucsd.edu/c/types.html#assignment compatible">assignment compatible</A>
|
|
with the type returned by the function. The program
|
|
<A HREF="express.html#Type Conversions" tppabs="http://ccs.ucsd.edu/c/express.html#Type Conversions">converts</A>
|
|
the value of the expression
|
|
to the type returned and returns it as the value of the function call.
|
|
If you do not write an expression within the <I>return</I> statement,
|
|
the program must execute that <I>return</I> only for a function call
|
|
that occurs in a
|
|
<A HREF="#side-effects context">side-effects context</A>.</P>
|
|
|
|
<P><IMG SRC="return_s.gif" tppabs="http://ccs.ucsd.edu/c/gif/return_s.gif"></P>
|
|
|
|
<P>Using the notation:</P>
|
|
|
|
<PRE>
|
|
return <I>expression</I>;</PRE>
|
|
|
|
<P>the flowchart for a <I>return</I> statement is:</P>
|
|
|
|
<P><IMG SRC="return_f.gif" tppabs="http://ccs.ucsd.edu/c/gif/return_f.gif"></P>
|
|
|
|
<P>For example:</P>
|
|
|
|
<PRE>
|
|
if (fabs(x) < 1E-6)
|
|
return (x);</PRE>
|
|
|
|
<P>(The consistent use of parentheses around the expression part
|
|
of a <I>return</I> statement, in this document, is a matter of
|
|
style, not necessity.)</P>
|
|
|
|
<H2><A NAME="Switch Statement">Switch Statement</A></H2>
|
|
|
|
<P>A <B>switch</B> statement jumps to a place
|
|
within a controlled statement, depending
|
|
on the value of an integer expression. The controlled statement is
|
|
almost invariably a
|
|
<A HREF="#Block">block</A>. The expression is in a
|
|
<A HREF="#value context">value context</A>.</P>
|
|
|
|
<P><IMG SRC="switch_s.gif" tppabs="http://ccs.ucsd.edu/c/gif/switch_s.gif"></P>
|
|
|
|
<P>The program evaluates the expression and then compares the value
|
|
with each of the
|
|
<A HREF="#Case Label"><I>case</I> labels</A> contained
|
|
in the controlled statement. A <I>case</I> label can be contained
|
|
within another statement (inside the controlled statement)
|
|
but not within an inner <I>switch</I> statement.</P>
|
|
|
|
<P>Each <I>case</I> label contains an
|
|
<A HREF="express.html#integer constant expression" tppabs="http://ccs.ucsd.edu/c/express.html#integer constant expression">integer constant
|
|
expression</A> whose value is converted to the
|
|
<A HREF="express.html#Promoting" tppabs="http://ccs.ucsd.edu/c/express.html#Promoting">promoted type</A> of the expression in
|
|
the <I>switch</I> statement before it is compared to the value of
|
|
that expression. Do not write two <I>case</I> labels whose expressions
|
|
have the same converted value within the same <I>switch</I> statement.</P>
|
|
|
|
<P>If the value of a <I>case</I> label expression equals the value
|
|
of the <I>switch</I> statement expression, control transfers to the
|
|
<I>case</I> label. Otherwise, control transfers to a
|
|
<A HREF="#Default Label"><I>default</I> label</A>
|
|
contained within the <I>switch</I> statement.
|
|
A <I>default</I> label can be contained within another statement
|
|
(inside the controlled statement) but not within an inner <I>switch</I>
|
|
statement. You can write no more than one <I>default</I> label within
|
|
a <I>switch.</I></P>
|
|
|
|
<P>If you do not write a <I>default</I> label, and the value of
|
|
the <I>switch</I> statement expression does not match any of the <I>case</I>
|
|
label expressions, control transfers to the statement following the
|
|
<I>switch</I> statement.</P>
|
|
|
|
<P>If the program executes a
|
|
<A HREF="#Break Statement"><I>break</I> statement</A> within the
|
|
controlled statement, control transfers to the statement following
|
|
the <I>switch</I> statement. A <I>break</I> statement for this <I>switch</I>
|
|
statement can be contained within another statement (inside the controlled
|
|
statement) but not within an inner
|
|
<I><A HREF="#Do Statement">do</A>,
|
|
<A HREF="#For Statement">for</A>, switch,</I> or
|
|
<A HREF="#While Statement"><I>while</I> statement</A>.</P>
|
|
|
|
<P>A <I>switch</I> statement takes many forms. Using
|
|
the representative example:</P>
|
|
|
|
<PRE>
|
|
switch (<I>expr</I>)
|
|
{
|
|
case <I>val-1</I>:
|
|
<I>stat-1</I>;
|
|
break;
|
|
case <I>val-2</I>:
|
|
<I>stat-2</I>; <B>falls through to next</B>
|
|
default:
|
|
<I>stat-n</I>
|
|
}</PRE>
|
|
|
|
<P>the flowchart for this particular <I>switch</I> statement is:</P>
|
|
|
|
<P><IMG SRC="switch_f.gif" tppabs="http://ccs.ucsd.edu/c/gif/switch_f.gif"></P>
|
|
|
|
<P>For example:</P>
|
|
|
|
<PRE>
|
|
switch (*s)
|
|
{
|
|
case '0':
|
|
case '1':
|
|
case '2':
|
|
case '3':
|
|
val = (val << 2) + *s - '0';
|
|
break;
|
|
default:
|
|
return (val);
|
|
}</PRE>
|
|
|
|
<H2><A NAME="While Statement">While Statement</A></H2>
|
|
|
|
<P>A <B>while</B> statement executes a statement
|
|
zero or more times, while the
|
|
<A HREF="#test context">test-context</A> expression
|
|
has a nonzero value.</P>
|
|
|
|
<P><IMG SRC="while_s.gif" tppabs="http://ccs.ucsd.edu/c/gif/while_s.gif"></P>
|
|
|
|
<P>Using the notation:</P>
|
|
|
|
<PRE>
|
|
while (<I>test</I>)
|
|
<I>statement</I></PRE>
|
|
|
|
<P>The flowchart for a <I>while</I> statement is:</P>
|
|
|
|
<P><IMG SRC="while_f.gif" tppabs="http://ccs.ucsd.edu/c/gif/while_f.gif"></P>
|
|
|
|
<P>If the program executes a
|
|
<A HREF="#Break Statement"><I>break</I> statement</A>
|
|
within the controlled statement, control transfers to the statement
|
|
following the <I>while</I> statement. A <I>break</I> statement for
|
|
this <I>while</I> statement can be contained within another statement
|
|
(inside the controlled statement) but not within an inner
|
|
<I><A HREF="#Do Statement">do</A>,
|
|
<A HREF="#For Statement">for</A>,
|
|
<A HREF="#Switch Statement">switch</A>,</I> or <I>while</I> statement.</P>
|
|
|
|
<P>If the program executes a
|
|
<A HREF="#Continue Statement"><I>continue</I> statement</A> within the
|
|
controlled statement, control transfers to the test-context expression
|
|
in the <I>while</I> statement. A <I>continue</I> statement for this
|
|
<I>while</I> statement can be contained within another statement (inside
|
|
the controlled statement) but not within an inner <I>do, for,</I>
|
|
or <I>while</I> statement.</P>
|
|
|
|
<P>For example:</P>
|
|
|
|
<PRE>
|
|
while ((c = getchar()) != EOF)
|
|
process(c);</PRE>
|
|
|
|
<H2><A NAME="Function Calls">Function Calls</A></H2>
|
|
|
|
<P>You <B>call a function</B> by writing a
|
|
<A HREF="express.html#Function Call" tppabs="http://ccs.ucsd.edu/c/express.html#Function Call">function call</A> operator within
|
|
an expression. When the program evaluates the expression, it suspends
|
|
execution of the statement containing the expression and transfers
|
|
control to the first statement in the block that defines the actions
|
|
of the called function. Objects with
|
|
<A HREF="declare.html#dynamic duration" tppabs="http://ccs.ucsd.edu/c/declare.html#dynamic duration">dynamic duration</A>
|
|
remain in existence for the block containing the function call.
|
|
A function can call itself, or call another function that calls it,
|
|
<B><A NAME="recursive function calls">recursively</A></B>.
|
|
The program allocates a separate set of objects
|
|
with dynamic duration for each activation of a function.</P>
|
|
|
|
<P>Before the called function gets control, the program stores
|
|
the value of each argument expression in a newly allocated object
|
|
associated with the corresponding
|
|
<A HREF="#function parameter">parameter</A>. You access the object
|
|
corresponding to the named parameter by writing the parameter name.
|
|
Unless you declare the parameter to have a <I>const</I> type, you
|
|
can also alter the value stored in its object. You can access the
|
|
values stored in the unnamed arguments to a function with a
|
|
varying number of arguments
|
|
by using the macros defined in the standard header
|
|
<CODE><A HREF="stdarg.html" tppabs="http://ccs.ucsd.edu/c/stdarg.html"><stdarg.h></A></CODE>. When
|
|
the function returns control to its caller, it deallocates the objects
|
|
created to hold argument values.</P>
|
|
|
|
<P>When a function executes a
|
|
<A HREF="#Return Statement"><I>return</I> statement</A>, it returns
|
|
control to its caller. You call a <I>function returning
|
|
a void type,</I> or any function that executes
|
|
a <I>return</I> statement without an
|
|
expression (either explicit or implicit), only from a
|
|
<A HREF="#side-effects context">side-effects context</A>.
|
|
Any other function call is an
|
|
<A HREF="express.html#rvalue expression" tppabs="http://ccs.ucsd.edu/c/express.html#rvalue expression">rvalue expression</A> whose type
|
|
is the type returned by the function and whose value is the value
|
|
of the expression in the <I>return</I> statement.</P>
|
|
|
|
<P>When you call a function with a fixed number of arguments, write
|
|
exactly as many arguments as the function has parameters. When you
|
|
call a function with a
|
|
<A HREF="stdarg.html#varying number of arguments" tppabs="http://ccs.ucsd.edu/c/stdarg.html#varying number of arguments">varying number
|
|
of arguments</A>, write at least as many arguments
|
|
as the function has parameters (before the ellipsis).</P>
|
|
|
|
<P>The type of the function can provide information about the type
|
|
of an argument if it corresponds to one of the declared parameters
|
|
in a function prototype. In this instance, the argument expression must be
|
|
<A HREF="types.html#assignment compatible" tppabs="http://ccs.ucsd.edu/c/types.html#assignment compatible">assignment compatible</A>
|
|
with its corresponding parameter. Its value is
|
|
<B><A NAME="argument conversion">converted</A></B>
|
|
as if by assignment before it is stored in the
|
|
parameter object.</P>
|
|
|
|
<P>For example:</P>
|
|
|
|
<PRE>
|
|
double fun(double);
|
|
y = fun(0); <B>integer 0 converted to double</B></PRE>
|
|
|
|
<P>The type of the function can also fail to provide any information
|
|
about an argument, if the function declaration is not a function prototype
|
|
or if the argument is one of the unnamed arguments in a
|
|
<A HREF="stdarg.html#varying number of arguments" tppabs="http://ccs.ucsd.edu/c/stdarg.html#varying number of arguments">varying number
|
|
of arguments</A>. In this instance, the argument must be an
|
|
<A HREF="express.html#rvalue expression" tppabs="http://ccs.ucsd.edu/c/express.html#rvalue expression">rvalue expression</A> that is
|
|
<B><A NAME="argument promotion">promoted</A></B> as follows:</P>
|
|
|
|
<UL>
|
|
<LI>An integer argument type is
|
|
<A HREF="express.html#Promoting" tppabs="http://ccs.ucsd.edu/c/express.html#Promoting">promoted</A>.</LI>
|
|
|
|
<LI>An argument of type <I>float</I> is converted to
|
|
<I>double.</I></LI>
|
|
|
|
<LI>An lvalue of type <I>array of T</I> becomes an rvalue of type
|
|
<I>pointer to T.</I></LI>
|
|
|
|
<LI>A function designator of type <I>function returning T</I> becomes
|
|
an rvalue of type <I>pointer to function returning T.</I></LI>
|
|
</UL>
|
|
|
|
<P>Any other argument expression type is unchanged when promoted.</P>
|
|
|
|
<P>For example:</P>
|
|
|
|
<PRE>
|
|
char ch;
|
|
int i;
|
|
float f(), a[10];
|
|
f( a, <B>array becomes pointer to float</B>
|
|
f, <B>function becomes pointer to function</B>
|
|
i, <B>int remains int</B>
|
|
ch, <B>char becomes int or unsigned int</B>
|
|
a[2] ); <B>float becomes double</B></PRE>
|
|
|
|
<P>A function call you write for a function that does not have
|
|
argument information behaves the same as one for a function prototype
|
|
that specifies:</P>
|
|
|
|
<UL>
|
|
<LI>the same return type as the actual function</LI>
|
|
|
|
<LI>the same (fixed) number of arguments as the actual function</LI>
|
|
|
|
<LI>a parameter of promoted type for each argument expression in
|
|
the function call</LI>
|
|
</UL>
|
|
|
|
<P>All declarations for the same function must be compatible. While
|
|
these rules permit you to write compatible function declarations with
|
|
and without argument information,
|
|
you should write only function prototypes.</P>
|
|
|
|
<HR>
|
|
<P>See also the
|
|
<B><A HREF="index.html#Table of Contents" tppabs="http://ccs.ucsd.edu/c/index.html#Table of Contents">Table of Contents</A></B> and the
|
|
<B><A HREF="_index.html" tppabs="http://ccs.ucsd.edu/c/_index.html">Index</A></B>.</P>
|
|
|
|
<P><I>
|
|
<A HREF="crit_pb.html" tppabs="http://ccs.ucsd.edu/c/crit_pb.html">Copyright</A> © 1989-1996
|
|
by P.J. Plauger and Jim Brodie. All rights reserved.</I></P>
|
|
|
|
</BODY></HTML>
|