1 <!DOCTYPE HTML PUBLIC
"-//W3O//DTD W3 HTML 2.0//EN">
2 <!-- This collection of hypertext pages is Copyright 1995-7 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>2.7 Function Calls
</title>
10 <link href=
"sx2f.html" rev=precedes
>
11 <link href=
"sx3.html" rel=precedes
>
12 <link href=
"sx2.html" rev=subdocument
>
15 <H2>2.7 Function Calls
</H2>
17 <p>We'll have much more to say about functions in a later chapter,
18 but for now let's just look at how they're called.
19 (To review: what a function
<em>is
</em> is a piece of code,
20 written by you or by someone else, which
21 performs some useful, compartmentalizable task.)
22 You call a function by mentioning its name followed by a pair
24 If the function takes any arguments, you place the arguments
25 between the parentheses, separated by commas.
26 These are all function calls:
28 printf(
"Hello, world!\n")
33 The arguments to a function can be arbitrary expressions.
34 Therefore, you don't have to say things like
37 printf(
"sum = %d\n", sum);
40 you can instead collapse it to
42 printf(
"sum = %d\n", a + b + c);
44 Many functions return values, and when they do, you can embed
45 calls to these functions within larger expressions:
47 c = sqrt(a * a + b * b)
51 The first expression squares
<TT>a
</TT> and
<TT>b
</TT>,
52 computes the square root of the sum of the squares,
53 and assigns the result to
<TT>c
</TT>.
55 it computes
<TT>a * a + b * b
</TT>,
56 passes that number to the
<TT>sqrt
</TT> function,
57 and assigns
<TT>sqrt
</TT>'s return value to
<TT>c
</TT>.)
58 The second expression passes the value of the variable
<TT>theta
</TT> to
59 the
<TT>cos
</TT> (cosine) function,
60 multiplies the result by
<TT>r
</TT>,
61 and assigns the result to
<TT>x
</TT>.
63 passes the value of the variable
<TT>j
</TT> to the function
<TT>f2
</TT>,
64 passes the return value of
<TT>f2
</TT>
65 immediately to the function
<TT>f1
</TT>,
66 and finally assigns
<TT>f1
</TT>'s return value to the variable
<TT>i
</TT>.
70 <a href=
"sx2f.html" rev=precedes
>prev
</a>
71 <a href=
"sx3.html" rel=precedes
>next
</a>
72 <a href=
"sx2.html" rev=subdocument
>up
</a>
73 <a href=
"top.html">top
</a>
76 This page by
<a href=
"http://www.eskimo.com/~scs/">Steve Summit
</a>
77 //
<a href=
"copyright.html">Copyright
</a> 1995-
1997
78 //
<a href=
"mailto:scs@eskimo.com">mail feedback
</a>