Functions, parameters, and return values · 函数、参数与返回值
Why functions
- A function is a named piece of work. You write it once and call it whenever you need it.
- Functions keep programs short and clear, and let you build big jobs from small ones.
- You have already used
printf— a function from<stdio.h>. Now you write your own.
为什么要用函数
- 函数是一段有名字的工作。你只写一次,需要时就调用它。
- 函数让程序更短、更清晰,也让你用小的工作拼出大的工作。
- 你已经用过
printf—— 它是<stdio.h>里的一个函数。现在你来写自己的。
Parameters and return values
- The values you pass in are the parameters. The value you hand back is the return value.
int max2(int a, int b)takes twointparameters and returns oneint.return x;ends the function right away and givesxback to the caller.
参数与返回值
- 你传进去的值叫参数。你交回来的值叫返回值。
int max2(int a, int b)接收两个int参数,返回一个int。return x;会立刻结束函数,并把x交回给调用者。
void functions, and building big from small
- A function that returns nothing has the return type
void. It does work but hands back no value. - One function can call another.
max3can callmax2twice to do its job. - Small functions that each do one thing are easier to read and to fix.
void 函数,以及用小拼大
- 不返回任何东西的函数,返回类型写成
void。它做事,但不交回值。 - 一个函数可以调用另一个函数。
max3可以调用max2两次来完成它的工作。 - 每个只做一件事的小函数,更容易读,也更容易改。
C copies your arguments
- When you call a function, C gives it a copy of each argument.
- So changing a parameter inside the function does not change the caller's variable.
- Keep this in mind — in a later lesson, pointers let a function reach the original.
C 会复制你的参数
- 当你调用函数时,C 给它每个参数的一份副本。
- 所以在函数里面改一个参数,不会改到调用者的变量。
- 记住这一点 —— 在后面的一课里,指针会让函数能够碰到原来的变量。
#include <stdio.h>
void try_double(int x) {
x = x * 2; // changes only the COPY
}
int main(void) {
int n = 5;
try_double(n);
printf("%d\n", n); // still 5 — the original did not change
return 0;
}
Common mistakes
- A function used before it is defined needs a prototype above
main. - Arguments pass by value — pass a pointer to change the caller's variable.
常见错误
- 在定义前使用的函数,要在
main上方写原型。 - 参数按值传递——要改调用者的变量就传指针。
Now you try
- Write each function with the exact name and parameters shown, and
returnthe right value. - You may call one of your functions from another.
- Do not write a
main— the checker provides one.
现在轮到你了
- 用给出的确切名字和参数写每个函数,并
return正确的值。 - 你可以在一个函数里调用你的另一个函数。
- 不要自己写
main—— 检查器会提供。
Functions: call and return · 函数:调用与返回
A call pushes a frame with its parameters; return pops it. · 调用压入带参数的栈帧;return 弹出它。
Write int max2(int a, int b) returning the larger of two numbers, and int max3(int a, int b, int c) returning the largest of three. Make max3 call · 调用 max2. Do not · 不 write a main. · 编写 int max2(int a, int b) 返回两个数中较大的,以及 int max3(int a, int b, int c) 返回三个数中最大的。让 max3 调用 max2。不要写 main。
Click Run to see the output here. · 点击“运行”查看此处输出。
Complete int power(int base, int exp) so it returns base raised to exp (assume exp >= 0). Use a loop. power(2, 10) is 1024. Do not · 不 write a main. · 完成 int power(int base, int exp),让它返回 base 的 exp 次方(假设 exp >= 0)。用一个循环。power(2, 10) 是 1024。不要写 main。
Click Run to see the output here. · 点击“运行”查看此处输出。
Complete int clamp(int x, int lo, int hi) so it keeps x within [lo, hi]: return lo if x is below lo, hi if x is above hi, otherwise x. Do not · 不 write a main. · 完成 int clamp(int x, int lo, int hi),把 x 限制在 [lo, hi] 之内:如果 x 低于 lo 返回 lo,高于 hi 返回 hi,否则返回 x。不要写 main。
Click Run to see the output here. · 点击“运行”查看此处输出。