A function definition starts with the keyword def
, followed by its name, parenthesis containing an optional list of arguments, and a colon. The body of the function is made of the indented following lines. As usual for any python variable, the function arguments can receive any value, whatever their type.
def powers(x, ps):
for p in ps:
print(x**p)
powers(2, [2, 3, 4])
4 8 16
If you want to be sure not to make an error about the order of the arguments, you can recall the argument name before each value. In such a case, the order does not matter:
powers(ps=[2, 3, 4], x=2)
4 8 16
The return
instruction enables to exit from a fonction, and return a result.
def square(x):
return x*x
x2 = square(2)
print(x2)
4
If you do not include anay return
statement in your function body, then the function will return None
.
The value returned can be anything, including a collection of values:
def powers(x, ps):
result = []
for p in ps:
result.append(x**p)
return result
res = powers(2, [2, 3, 4])
print(res)
[4, 8, 16]
Within a function, when an instruction reads the value of a variable, the interpreter first search for a local variable, then for a global one.
Below, f1()
does not have any text
argument so the print(text)
instruction will read the global text
. On the contrary, f2(text)
has a local argument called text
, which is somehow "hiding" the global one.
text = 'global text'
def f1():
print(text)
def f2(text):
print(text)
f1()
f2('argument text')
global text argument text
Within a function, when an instruction affects a new value to a variable which does not exist locally (there is no argument with the required name), then a new local variable is created, even if there is another existing global variable with the required name. Thus the global one will be hidden.
def f3():
text = 'f3 text'
print(text)
f1()
f3()
f1()
global text f3 text global text
Below, text
is a local variable (argument) which receive as initial value the one given in the f4
call. The variable value is immediatly changed to 'f4 text'
and displayed.
def f4(text):
text = 'f4 text'
print(text)
f4('argument text')
f1()
f4 text global text
f4(text)
f1()
f4 text global text
If you want a function to modify a global variable, you must say it with a global
instruction:
def f5():
global text
text = 'f5 text'
print(text)
f5()
f1()
f5 text f5 text
Warning: do not to reuse everywhere the same short variable names, and you will avoid the hiding problems which are described above.
Warning: as with any programming language, the use of global variables is not recommended, unless you have no other alternative. On the one hand, the use of global variables reduce the number of arguments which are exchanged between functions, but on the other hand, as their number grows, it becomes tricky to trace which function modify wich variable and when.
Given a function which receive a collection as argument, if this function affects a new value to the variable which refers to the whole collection, the original collection is not modified. On the contrary, if the function affects new values to one or several elements, the original collection is modified.
def f(arg):
arg = [1, 20, 3]
a = [1, 2, 3]
print(a)
f(a) # equivalent to "arg = a ; arg = [1, 20, 3]
print(a)
[1, 2, 3] [1, 2, 3]
def f(arg):
arg[1] = 20
a = [1, 2, 3]
print(a)
f(a) # equivalent to "arg = a ; arg[1] = 20"
print(a)
[1, 2, 3] [1, 20, 3]