Functions
لم يُترجم بعد؛ يُعرض باللغة الإنجليزية
A function is a named group of steps you write once and run whenever you need it. Step through a seven-line program, avg.py, that defines average(nums) and calls it twice: watch def store the function, each call hand in a list and get its own call frame, the body work out the answer, and return send one value back to the line that asked. Finish with the most common slip: forget return and you get None.
5 أسئلة سريعة
اختر إجابة لتعرف السبب. تُحفَظ نتيجتك مع تقدّمك.
01A file contains only these two lines: def greet(): and, indented below it, print("Hi") . What does running the file print?
def only creates the function and stores it under the name greet. Its body runs when something calls it, as in greet().
02In def average(nums): and the call average([8, 6, 10]) , which is the parameter and which is the argument?
A parameter is the name written in the def line. An argument is the value you hand in when you call the function; on the call, nums names [8, 6, 10].
03What does average([4, 5]) return, if average is total = sum(nums) then return total / len(nums) ?
sum([4, 5]) is 9 and len([4, 5]) is 2, and 9 / 2 is 4.5. The / operator always gives a float.
04After a = average([8, 6, 10]) has finished, you type total at the top level of the program. What happens?
total is a local variable: it lived in that call's frame, which was cleared away when the call returned.
05You delete the word return from the last line of average. What does print(average([4, 5])) show?
The line still works out 4.5 but throws it away. A function that ends without return sends back None, and print shows None.
المسرد
- Function
- A named group of steps. You define it once with def and run it by calling it, as many times as you like.
- def
- The keyword that defines a function: def, the name, parameters in brackets, a colon, then an indented body.
- Call
- Running a function: its name followed by brackets holding the arguments, such as average([8, 6, 10]).
- Parameter
- A name in the def line, such as nums, that stands for whatever value a caller hands in.
- Argument
- The actual value handed to a function in a call, such as [8, 6, 10].
- return
- Ends the call and sends a value back to the line that made the call; the call is replaced by that value.
- Local variable
- A name made inside a function, such as total. It lives in the call's frame and is gone when the call returns.
- None
- Python's value for "nothing". A function that ends without return gives back None.
الملاحظات والمصادر
- def is itself a statement that runs: it builds a function object and binds the name average to it. The figure writes that object as function; Python shows it as <function average at 0x…>, with a memory address that changes from run to run.
- The tray is a call frame: each call gets a fresh table for its local names (here nums and total). When the call returns, the frame is released and those names can no longer be reached from outside.
- The / operator always gives a float, even when the division is exact: 24 / 3 is 8.0, not 8. Use // for whole-number (floor) division.
- A function that ends without a return statement, or with a bare return, sends back None. Calling average([]) on an empty list would stop with ZeroDivisionError, because len([]) is 0.
- On macOS and Linux the command may be python3; on Windows, py avg.py also works. The $ is the terminal's prompt: you do not type it.
- Python tutorial: Defining functions
- Python reference: Function definitions
- Python reference: Calls
- Python reference: The return statement
- Python FAQ: What is the difference between arguments and parameters?
- Python reference: Naming and binding (local names, NameError)
- Python library: Built-in functions (sum, len, print)
- Python library: Built-in constants (None)
اختبار يتاح بعد الخطوة الأخيرة.