A function can take arguments
and return
values:
In the following example, the function say_hello()
receives the argument "name" and prints a greeting:
>>> def say_hello(name):
... print('Hello', name)
...
>>> say_hello('Carlos')
# Hello Carlos
>>> say_hello('Wanda')
# Hello Wanda
>>> say_hello('Rose')
# Hello Rose
The above function does not return any value (implicitly, it returns None
) and is called void function. The following function, on the other hand, returns values and is thus called the fruitful function
>>> def my_sum(x,y):
... "Compute x + y."
... return x + y
>>> my_sum(1,2)
# 3
When we define functions, we list the input parameters. These are called positional parameters (or positional arguments) because the position in the def
statement determines which parameter is which.
>>> def poly(x,y):
... "Compute x + y**2."
... return x + y**2
>>> poly(1,2)
# 5
>>> poly(2,1)
# 3
We can set default values for parameters:
>>> def psum(x,y,p=2):
... return x**p + y**p
>>> psum(1,2)
# 5
To improve code readability, we should be as explicit as possible. We can achieve this in our functions by using Keyword Arguments
:
>>> def say_hi(name, greeting):
... print(greeting, name)
...
>>> # with positional arguments
>>> say_hi('John', 'Hello')
# Hello John
>>> # with keyword arguments
>>> say_hi(name='Anna', greeting='Hi')
# Hi Anna
When creating a function using the def
statement, you can specify what the return value should be with a return
statement. A return statement consists of the following:
return
keyword.>>> def sum_two_numbers(number_1, number_2):
... return number_1 + number_2
...
>>> result = sum_two_numbers(7, 8)
>>> print(result)
# 15
*args
and **kwargs
allow you to pass an undefined number of arguments and keywords when calling a function.
>>> def some_function(*args, **kwargs):
... pass
...
>>> # call some_function with any number of arguments
>>> some_function(arg1, arg2, arg3)
>>> # call some_function with any number of keywords
>>> some_function(key1=arg1, key2=arg2, key3=arg3)
>>> # call both, arguments and keywords
>>> some_function(arg, key1=arg1)
>>> # or none
>>> some_function()
**kwargs
used to collect nonspecific keyword arguments. The **kwargs
must be the rightmost paramter.
*args
Positional arguments through the *args
variable:
>>> def some_function(*args):
... print(f'Arguments passed: {args} as {type(args)}')
...
>>> some_function('arg1', 'arg2', 'arg3')
# Arguments passed: ('arg1', 'arg2', 'arg3') as <class 'tuple'>
**kwargs
Keyword arguments are accessed through the **kwargs
variable:
>>> def some_function(**kwargs):
... print(f'keywords: {kwargs} as {type(kwargs)}')
...
>>> some_function(key1='arg1', key2='arg2')
# keywords: {'key1': 'arg1', 'key2': 'arg2'} as <class 'dict'>
spam
and a global variable also named spam
.global_variable = 'I am available everywhere'
>>> def some_function():
... print(global_variable) # because is global
... local_variable = "only available within this function"
... print(local_variable)
...
>>> # the following code will throw error because
>>> # 'local_variable' only exists inside 'some_function'
>>> print(local_variable)
Traceback (most recent call last):
File "<stdin>", line 10, in <module>
NameError: name 'local_variable' is not defined
global
StatementIf you need to modify a global variable from within a function, use the global statement:
>>> def spam():
... global eggs
... eggs = 'spam'
...
>>> eggs = 'global'
>>> spam()
>>> print(eggs)
There are four rules to tell whether a variable is in a local scope or global scope:
To start importing functions, we first need to create a module. A module is a file ending in .py
that contains the code you want to import into your program. Let’s consider a module that contains the function make_pizza()
.
import pizza
pizza.make_pizza(16, 'pepperoni')
pizza.make_pizza(12, 'mushrooms', 'green peppers', 'extra cheese')
from
from pizza import make_pizza
make_pizza(16, 'pepperoni')
make_pizza(12, 'mushrooms', 'green peppers', 'extra cheese')
as
to Give a Function or Module an Aliasfrom pizza import make_pizza as mp
mp(16, 'pepperoni')
mp(12, 'mushrooms', 'green peppers', 'extra cheese')
from pizza import *
make_pizza(16, 'pepperoni')
make_pizza(12, 'mushrooms', 'green peppers', 'extra cheese')