* remove "\r" nonsense
[mascara-docs.git] / C / the.ansi.c.programming.language / c.programming.notes / sx5a.html
blob498b27db990b45fa36b9c2cbd00e7e9150d2a9d5
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. -->
5 <html>
6 <head>
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>
13 </head>
14 <body>
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
21 to direct its work;
22 it has a <dfn>body</dfn>
23 containing the actual instructions
24 (statements)
25 for carrying out the task the function is supposed to perform;
26 and it may give you back a <dfn>return value</dfn>,
27 of a particular type.
28 </p><p>Here is a very simple function,
29 which accepts one argument, multiplies it by 2, and hands that
30 value back:
31 <pre>
32 int multbytwo(int x)
34 int retval;
35 retval = x * 2;
36 return retval;
38 </pre>
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,
44 of type <TT>int</TT>,
45 named <TT>x</TT>.
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.
53 (In particular,
54 the caller does not have to pass the value of a variable
55 named <TT>x</TT>.)
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>)
60 and two statements.
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>
68 variable;
69 the function could be collapsed to
70 <pre>
71 int multbytwo(int x)
73 return x * 2;
75 </pre>
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
80 full detail.
81 Here is a tiny skeletal program to call <TT>multby2</TT>:
82 <pre>
83 #include &lt;stdio.h&gt;
85 extern int multbytwo(int);
87 int main()
89 int i, j;
90 i = 3;
91 j = multbytwo(i);
92 printf("%d\n", j);
93 return 0;
95 </pre>
96 This looks much like our other test programs, with the
97 exception of the new line
98 <pre>
99 extern int multbytwo(int);
100 </pre>
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:
109 the function's name,
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
118 declarations.)
119 Finally, to remind us that this is an external declaration and not a
120 defining instance,
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
130 should be obvious:
131 the line
132 <pre>
133 j = multbytwo(i);
134 </pre>
135 calls <TT>multbytwo</TT>, passing it the value of <TT>i</TT> as its
136 argument.
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
149 as well call
150 <pre>
151 j = multbytwo(3);
152 </pre>
153 And the variable <TT>j</TT> isn't really needed,
154 either, since we could just
155 as well call
156 <pre>
157 printf("%d\n", multbytwo(3));
158 </pre>
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
162 to <TT>printf</TT>.
164 (Here,
165 as in general,
166 we see the flexibility
167 and generality
169 of expressions in C.
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
175 surrounding
176 expressions.)
177 </p><p>
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>
186 variable like this:
187 <pre>
188 int multbytwo(int x)
190 x = x * 2;
191 return x;
193 </pre>
194 We might wonder,
195 if we wrote it this way,
196 what would happen to the value of the variable <TT>i</TT>
197 when we called
198 <pre>
199 j = multbytwo(i);
200 </pre>
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?
204 The answer is no.
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,
213 and furthermore,
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)
219 in the next chapter.
220 </p><hr>
222 Read sequentially:
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>
227 </p>
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>
232 </p>
233 </body>
234 </html>