* remove "\r" nonsense
[mascara-docs.git] / C / the.ansi.c.programming.language / c.programming.notes / sx2g.html
blob8bdf5bc7fcf390816cb3327a313811add8b229c9
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. -->
5 <html>
6 <head>
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>
13 </head>
14 <body>
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
23 of parentheses.
24 If the function takes any arguments, you place the arguments
25 between the parentheses, separated by commas.
26 These are all function calls:
27 <pre>
28 printf("Hello, world!\n")
29 printf("%d\n", i)
30 sqrt(144.)
31 getchar()
32 </pre>
33 The arguments to a function can be arbitrary expressions.
34 Therefore, you don't have to say things like
35 <pre>
36 int sum = a + b + c;
37 printf("sum = %d\n", sum);
38 </pre>
39 if you don't want to;
40 you can instead collapse it to
41 <pre>
42 printf("sum = %d\n", a + b + c);
43 </pre>
44 Many functions return values, and when they do, you can embed
45 calls to these functions within larger expressions:
46 <pre>
47 c = sqrt(a * a + b * b)
48 x = r * cos(theta)
49 i = f1(f2(j))
50 </pre>
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>.
54 (In other words,
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>.
62 The third expression
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>.
67 </p><hr>
68 <p>
69 Read sequentially:
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>
74 </p>
75 <p>
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>
79 </p>
80 </body>
81 </html>