Functions
لم يُترجم بعد؛ يُعرض باللغة الإنجليزية
A function is a named block of code with typed inputs and a typed output. Follow one small program as main calls area(3, 4) and area(5, 2): watch each call get its own frame on the call stack, the arguments get copied into the parameters, and return carry one answer back to the line that asked. Then see the compiler reject a call with a missing argument before anything runs.
5 أسئلة سريعة
اختر إجابة لتعرف السبب. تُحفَظ نتيجتك مع تقدّمك.
01In int area(int w, int h), what does the first int tell you?
The type before the name is the return type: area hands back an int. The int w and int h inside the brackets are the parameters, each with its own type.
02main runs int a = area(3, 4);. What happens first?
A call pauses the caller where it is. A new stack frame is made for area, and each parameter is initialized with a copy of its argument, in order: w = 3, h = 4.
03Where does the value from return w * h; go?
return ends the call and sends its value back to the calling line. area(3, 4) becomes 12, so int a = area(3, 4); stores 12 in a.
04After area(3, 4) has returned, main tries to use w. What happens?
w and h are local to area. They only exist while a call to area is running, in its own frame, and the name w means nothing inside main, so the compiler rejects it.
05What does the compiler do with int a = area(3); when area takes two ints?
Every call is checked against the function's declaration before anything runs. One argument for two parameters breaks the rules, so the compiler stops (Clang: no matching function for call to 'area'; GCC: too few arguments).
المسرد
- Function
- A named block of code that can take inputs and give back a value. You write it once and run it by calling its name.
- Return type
- The type written before a function's name (int in int area(...)): the kind of value the function gives back. void means it gives back nothing.
- Parameter
- A typed name in a function's first line, such as int w. It is a local variable of the function, set from an argument at each call.
- Argument
- The value you pass in a call, such as 3 in area(3, 4). For an int parameter it is copied into the parameter.
- Call
- Running a function by writing its name and arguments, area(3, 4). The caller pauses until the function returns.
- return
- The statement that ends a function call and sends a value back to the place of the call.
- Stack frame
- The memory for one running call: its parameters and local variables. Frames pile up on the call stack and are removed when the call returns.
- Pass by value
- Giving a function a copy of each argument. Changing the parameter inside the function does not change the caller's variable.
الملاحظات والمصادر
- The drawing shows each call's local variables on a tray, its stack frame, and the trays pile up as the call stack. The C++ standard only says that local variables and parameters live until their block or call ends; real compilers keep them in a stack frame or in CPU registers.
- The figure gives a and b their places when main starts, as compilers usually reserve a whole frame at once. In C++ terms each variable's life starts at its own line: a when line 6 initializes it.
- int parameters are passed by value: the parameter is a new variable initialized with a copy of the argument. In void twice(int x) { x = x * 2; }, calling twice(n) leaves n unchanged. A later lesson shows references, which let a function change the caller's variable.
- A function must be declared before the line that calls it. Here area is defined above main. You can also write just the first line, int area(int w, int h);, near the top and the full definition further down.
- main is special: if it reaches its closing } without a return, it acts as return 0. Every other function that returns a value must reach a return statement; a function that returns nothing has the return type void.
- The terminal shortens the compiler's message. Clang says: no matching function for call to 'area' (candidate function not viable: requires 2 arguments, but 1 was provided). GCC says: too few arguments to function 'int area(int, int)'.
- The program on the card is real, standard C++. Build it with g++ calc.cpp -o calc (or clang++), run ./calc and it prints 22.
- cppreference.com: Function declaration
- cppreference.com: return statement
- cppreference.com: Other operators (the function call operator)
- cppreference.com: Storage duration (automatic: local variables and parameters)
- cppreference.com: Main function (falling off the end of main acts as return 0)
- cppreference.com: Declarations
- Standard C++ Foundation (isocpp.org): Get Started!
- C++ Core Guidelines (isocpp.github.io): F: Functions
- Bryant and O'Hallaron, Computer Systems: A Programmer's Perspective, 3rd ed., section 3.7 Procedures (the run-time stack)
اختبار يتاح بعد الخطوة الأخيرة.