Functions
Introduction
In this tutorial we are going to create a few houses for our village.
To do this we will learn to use functions to packaged up our code into reusable chunks. We will also learn to save our code in Python modules so that we don’t need to typing or pasting code all of the time.
Simple Function
A function is a named block of code with some parameters and a return value. Parameters are variables that are set by the code that calls the function. The return value is passed back to the calling code.
Try typing (or pasting) this very simple function
def age(birth_year, current_year=2022):
calculated_age = current_year - birth_year
return calculated_age
When you hit enter after typing the function definition above nothing appears to happen. But you have created a function which you can now call like this:
my_age = age(1965)
print(my_age)
If you substitute your own birth_year value this function will print your age at the end of the year 2022.
age(1965)
calls the function defined above and passes 1965 for the
value of birth_year
. It does not pass a value for the parameter
current_year
. The default value for current_year
was set to 2022.
The function performs a -
subtraction operation, taking the value of
current_year
and subtracting the value of birth_year
. The result
is assigned into calculated_age
with =
.
The function uses return
to pass the value of calculated_age
back to
the caller. Our caller assigns the returned value to a variable called
my_age
and prints it out.
Now if you don’t want to use the default of 2022 (when this tutorial was
written) you can pass your own value for current_year
. e.g. to find
out how old you were when the first Apple Mac was released:
print(age(1965, current_year=1984))
19
Obviously if unlike me, you were born after 1984 you would get a negative result.
Variable Naming
Notice the use of _
in the variable names. This is used to separate words
in the variable name. Typically variable names are lowercase with words separated
by underscores. It is also legal to use uppercase letters and numbers in variable
names (but a number must not be the first character).
Tower Function
Lets create a function that will create a tower. We are going to use the following function. Don’t type it in yet, we are going to create a file for it. Also do not worry too much about the details of this function yet but notice that it has the following parameters that the caller can supply:
height
- the height of the towerwidth
- the width of the towerworld
- a world object that we can use to create blocks in the worldpos
- the position of the tower
from mciwb import Direction, Item, Vec3
def tower(world, pos, width=5, height=8):
"""
Create a tower of blocks at a given position.
"""
# make sure the caller passed a variable of type Vec3 for pos
assert isinstance(pos, Vec3)
# each tower's wall is one less that width steps away from its opposite
opposite = width - 1
# outer loop does the layers of the tower
for layer in range(height):
y_offset = Direction.UP * layer
# inner loop does the blocks in each of 4 walls
for i in range(width):
# calculate the position i steps to the east of pos
eastward = pos + Direction.EAST * i + y_offset
# front wall of tower
world.set_block(eastward, Item.COBBLESTONE)
# back wall of tower
world.set_block(eastward + Direction.NORTH * opposite, Item.COBBLESTONE)
# calculate the position i steps to the north of pos
northward = pos + Direction.NORTH * i + y_offset
# left wall of tower
world.set_block(northward, Item.COBBLESTONE)
# right wall of tower
world.set_block(northward + Direction.EAST * opposite, Item.COBBLESTONE)
TODO continue