Alterna · учебный материал
Functions and Parameters
39.1: Functions and parameters: Short theory, one example, and Python step-by-step.
Что в этом материале
- How it works.
- One example in execution
- Remember
Содержание и основные понятия
The function receives arguments, creates local parameters and returns the value. The challenge can be used within a larger expression.
substance and practice
Grade 11
EGE No. 16
01
How it works.
The function describes the repeated action with the contract: what arguments it accepts, what returns and what assumptions it makes about the data.
def.
Creates a function; its body is not performed before the call.
Parameter.
Local name in the function definition.
Argument.
Specific value at the call point.
return.
Immediately completes the call and transmits the value to the code.
02
One example in execution
Start the short program and go over the lines. The visualiser only shows the code, current line, variables and output.
print
shows the value to the user but returns
None
; this is not a replacement
return
The function can return multiple values — actually one motorcade that can be unpacked. The default value is convenient for unchangeable objects, but the default list is created once and usually leads to errors.
Algorithm map
Read from left to right, then run the code.
1
Start
2
Call twice(5)
3
Create local x
4
Return x * 2
5
Save and Remove Answer
03
Remember
Tell me in the contract if zero divisor is allowed.
The code after return in the same branch is impossible.
Don't use the variable list as default without a special reason.
Syntax of this page
def name(parameters):
Determines function with body block.
return value
Returns value and completes the current challenge.
name(argument)
It causes a function; arguments are linked to parameters.
print
The value goes out and returns None.
docstring
The first line in the function describes her contract and is available as name.
a // b, a % b
Two values shall be packed in a motorcade and may be unpacked on the left.
keyword argument
The call f(limit=10) connects the argument by the name of the parameter.
default parameter
In def f(x=0) the value is used when the argument is not transmitted.
while condition
It repeats the body as long as the condition is true; the condition must approach the stop.
+= и //=
Abbreviated attributions for accumulation and a whole division.
int(input())
Reads and converts the input value.