Functions, parameters, and return values
This page needs a recent browser (with SharedArrayBuffer support). Please update Chrome, Edge, Firefox or Safari to the latest version. · 이 페이지는 최신 브라우저(SharedArrayBuffer 지원)가 필요합니다. Chrome, Edge, Firefox 또는 Safari를 최신 버전으로 업데이트해 주세요.
English
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.
한국어
왜 함수인가
- 함수(function) 는 이름이 붙은 작업 단위입니다. 한 번 작성하고 필요할 때마다 call할 수 있습니다.
- 함수는 프로그램을 짧고 명확하게 유지하며, 작은 작업을 결합하여 큰 일을 만들게 합니다.
- 이미
printf을 사용했습니다 —<stdio.h>의 함수입니다. 이제 직접 만드세요.
English
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.
한국어
매개변수와 반환 값
- 전달되는 값은 매개변수(parameter) 입니다. 돌려받는 값은 반환 값(return value) 입니다.
int max2(int a, int b)은 두int매개변수를 받아 하나int를 반환합니다.return x;은 함수를 즉시 종료하고 caller에게x을 반환합니다.
English
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를 두 번 호출할 수 있습니다. - 각 하나씩의 일을 하는 작은 함수들은 읽고 수정하기 더 쉽습니다.
English
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;
}
English
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위에 프로타입이 있어야 합니다. - 인자는 값으로 전달됩니다: 호출자의 변수를 바꾸려면 포인터를 전달하십시오.
English
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을 작성하지 마세요; 검증程序가 제공해 줍니다.
Explore · 탐색하기
Functions: call and return
A call pushes a frame with its parameters; return pops it.
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.
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.
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.
Click Run to see the output here. · 출력을 보려면 '실행'을 클릭하세요.