1 <!DOCTYPE HTML PUBLIC
"-//W3O//DTD W3 HTML 2.0//EN">
2 <!-- This collection of hypertext pages is Copyright 1995, 1996 by Steve Summit. -->
3 <!-- This material may be freely redistributed and used -->
4 <!-- but may not be republished or sold without permission. -->
7 <link rev=
"owner" href=
"mailto:scs@eskimo.com">
8 <link rev=
"made" href=
"mailto:scs@eskimo.com">
9 <title>5.1 Function Basics
</title>
10 <link href=
"sx5.html" rev=precedes
>
11 <link href=
"sx5b.html" rel=precedes
>
12 <link href=
"sx5.html" rev=subdocument
>
15 <H2>5.1 Function Basics
</H2>
17 <p>So what defines a function?
18 It has a
<em>name
</em> that you call it by,
19 and a list of zero or more
<dfn>arguments
</dfn> or
20 <dfn>parameters
</dfn> that you hand to it for it to act on or
22 it has a
<dfn>body
</dfn>
23 containing the actual instructions
25 for carrying out the task the function is supposed to perform;
26 and it may give you back a
<dfn>return value
</dfn>,
28 </p><p>Here is a very simple function,
29 which accepts one argument, multiplies it by
2, and hands that
39 On the first line we see the return type of the function (
<TT>int
</TT>),
40 the name of the function (
<TT>multbytwo
</TT>),
41 and a list of the function's arguments, enclosed in parentheses.
42 Each argument has both a name and a type;
43 <TT>multbytwo
</TT> accepts one argument,
46 The name
<TT>x
</TT> is arbitrary, and is used only within the
47 definition of
<TT>multbytwo
</TT>.
48 The caller of this function only needs to know that a single
49 argument of type
<TT>int
</TT> is expected;
50 the caller does not need to know what name the function will
51 use internally to refer to that argument.
54 the caller does not have to pass the value of a variable
56 </p><p>Next we see, surrounded by the familiar braces,
57 the body of the function itself.
58 This function consists of one declaration
59 (of a local variable
<TT>retval
</TT>)
61 The first statement is a conventional expression statement,
62 which computes and assigns a value to
<TT>retval
</TT>,
63 and the second statement is a
<TT>return
</TT> statement, which
64 causes the function to return to its caller, and also
65 specifies the value which the function returns to its caller.
66 </p><p>The
<TT>return
</TT> statement can return the value of any
67 expression, so we don't really need the local
<TT>retval
</TT>
69 the function could be collapsed to
76 </p><p>How do we call a function?
77 We've been doing so informally since day one,
79 but now we have a chance to call one that we've written, in
81 Here is a tiny skeletal program to call
<TT>multby2
</TT>:
83 #include
<stdio.h
>
85 extern int multbytwo(int);
96 This looks much like our other test programs, with the
97 exception of the new line
99 extern int multbytwo(int);
101 This is an
<dfn>external function prototype declaration
</dfn>.
102 It is an external declaration, in that it declares something which is
103 defined somewhere else.
104 (We've already seen the defining instance of the function
105 <TT>multbytwo
</TT>, but maybe the compiler hasn't seen it yet.)
106 The function prototype declaration contains the three
107 pieces of information about the function
108 that a caller needs to know:
110 return type, and argument type(s).
111 Since we don't care what name
112 the
<TT>multbytwo
</TT> function will use to refer to its first argument,
113 we don't need to mention it.
114 (On the other hand, if a function takes several arguments,
115 giving them names in the prototype
116 may make it easier to remember which is
117 which, so names may optionally be used in function prototype
119 Finally, to remind us that this is an external declaration and not a
121 the prototype is preceded by the keyword
<TT>extern
</TT>.
122 </p><p>The presence of the function prototype declaration lets the
123 compiler know that we intend to call this function,
<TT>multbytwo
</TT>.
124 The information in the prototype lets the compiler generate the
125 correct code for calling the function,
126 and also enables the compiler to check up on our code
127 (by making sure, for example, that we pass the correct number
128 of arguments to each function we call).
129 </p><p>Down in the body of main, the action of the function call
135 calls
<TT>multbytwo
</TT>, passing it the value of
<TT>i
</TT> as its
137 When
<TT>multbytwo
</TT> returns,
138 the return value is assigned to the variable
<TT>j
</TT>.
140 (Notice that the value of
141 <TT>main
</TT>'s local variable
<TT>i
</TT>
142 will become the value of
143 <TT>multbytwo
</TT>'s parameter
<TT>x
</TT>;
144 this is absolutely not a problem,
145 and is a normal sort of affair.)
146 </p><p>This example is written out in ``longhand,'' to make
147 each step equivalent.
148 The variable
<TT>i
</TT> isn't really needed, since we could just
153 And the variable
<TT>j
</TT> isn't really needed,
154 either, since we could just
157 printf(
"%d\n", multbytwo(
3));
159 Here, the call to
<TT>multbytwo
</TT> is a subexpression which
160 serves as the second argument to
<TT>printf
</TT>.
161 The value returned by
<TT>multbytwo
</TT> is passed immediately
166 we see the flexibility
170 An argument passed to a function
171 may be an arbitrarily complex subexpression,
172 and a function call is itself an expression
173 which may be embedded as a subexpression
174 within arbitrarily complicated
178 We should say a little more about the mechanism by which an
179 argument is passed down from a caller into a function.
180 Formally, C is
<dfn>call by value
</dfn>,
181 which means that a function receives
<em>copies
</em>
182 of the values of its arguments.
183 We can illustrate this with an example.
184 Suppose, in our implementation of
<TT>multbytwo
</TT>,
185 we had gotten rid of the unnecessary
<TT>retval
</TT>
195 if we wrote it this way,
196 what would happen to the value of the variable
<TT>i
</TT>
201 When our implementation of
<TT>multbytwo
</TT> changes the
202 value of
<TT>x
</TT>, does that change the value of
203 <TT>i
</TT> up in the caller?
205 <TT>x
</TT> receives a copy of
<TT>i
</TT>'s value,
206 so when we change
<TT>x
</TT> we don't change
<TT>i
</TT>.
207 </p><p>However, there is an exception to this rule.
208 When the argument you pass to a function is not a single variable,
209 but is rather an array,
210 the function does
<em>not
</em> receive a copy of the array,
211 and it therefore
<em>can
</em> modify the array in the caller.
212 The reason is that it might be too expensive to copy the entire array,
214 it can be useful for the function to write into the caller's array,
215 as a way of handing back more data
216 than would fit in the function's single return value.
217 We'll see an example of an array argument
218 (which the function deliberately writes into)
223 <a href=
"sx5.html" rev=precedes
>prev
</a>
224 <a href=
"sx5b.html" rel=precedes
>next
</a>
225 <a href=
"sx5.html" rev=subdocument
>up
</a>
226 <a href=
"top.html">top
</a>
229 This page by
<a href=
"http://www.eskimo.com/~scs/">Steve Summit
</a>
230 //
<a href=
"copyright.html">Copyright
</a> 1995,
1996
231 //
<a href=
"mailto:scs@eskimo.com">mail feedback
</a>