At My Fingertips
Rapid Playground
In this activity you learn how to use types to clarify what kinds of values a function accepts and produces.
Have a look at the documentation of the rectangle function. Click on the function name with your mouse, and check out the popup.
Three!
If you want to call the rectangle
function,
you need to provide three arguments,
one argument for each parameter.
The arguments provide the values
for the rectangle's width
, height
, and color
parameters.
Let's see what happens if you call rectangle
and don't provide three arguments.
First, run the code below, which calls the function but provides no arguments:
You should see an error message that tells us that the function requires three arguments.
Edit the above code cell, to make it look like this:
rectangle(100, 50)
Run your changed code.
You should get an error message about the missing third argument, color
.
Edit your code by adding red
as a third argument
(red
is the name of a color we imported in the first line):
rectangle(100, 50, red)
The code should execute without any error.
Our function call provides one argument
for each parameter of the rectangle
function.
The function produces and returns a rectangle.
But we don't do anything with that rectangle.
We certainly do not output it.
We have a correct program; it's just not very useful,
because running it has no observable effect.
If we want to output the rectangle,
we have to call the show_graphic
function
and pass our rectangle as an argument:
Have another look at the documentation of the rectangle function.
The word width
tells us that the name of the function's first parameter is "width".
But what is that colon (:
) and the word float
about?
This is a type annotation.
It specifies the type of that parameter.
It tells us that we have to provide a floating-point number as the first argument
in a call to rectangle
.
Each of the three parameters of the rectangle
function expects values of a certain type: the width
and height
have to be provided as values of type float
(floating-point numbers like 1.5, integer numbers like 50 are fine, too),
and the color
expects values of type Color
.
When we call a function, we should always pass it arguments of types it expects.
rectangle(red, 100, 100)
is not correct.
rectangle(100, 100, red)
is type-correct.
Let's have a last look at the documentation of the rectangle function.
The arrow (->
) is followed by the return type of the function.
It tells us what kind of value we will get when we call the function.
According to this return type annotation,
the rectangle
function will produce and return a value of type Graphic
.
While it doesn't guarantee that the returned graphic will be rectangular,
it does guarantee that it will be a graphic,
and not a number or a value of some other type.
The return type annotation of a function allows us to know that the function will always return a value of that type. We don't need to call the function to figure it out. We can simply look at its documentation.
square
FunctionWe want to define a function that can create squares.
If you completed the Circles activity, you already learned how to define a function.
Your circle
function did its work by calling the (more general) ellipse
function.
The situation for squares and rectangles is similar.
We can define a square
function, which internally will call the more general rectangle
function.
This could look something like this:
def square(side, color):
...
However, now that we know about type annotations,
let's write a more informative function definition.
In the code below, replace the underscores (_
)
with the types of the two parameters and the return value.
Note that types Color
and Graphic
are part of the PyTamaro library,
and we import them in the first line:
Click on the "RUN" button to check. Does it look like a square?
In the following code cell, create and show a green square with side 150, and show it.
First import the definition of the name green
,
and then replace the _
with the appropriate square
call.
Now create and show a black square with side 80.
This time, don't define a name,
but simply call the square
function where the _
is.
You learned that there are different types of values;
for example floating-point numbers (float
),
colors (Color
), and
graphics (Graphic
).
You also learned that when defining a function, you can specify the type of each parameter, and the type of the return value.
This allows developers who will call your function, to quickly understand what type of arguments your function expects, and what type of return value it will produce. In fact, providing type annotations also allows automated tools to find bugs in your code, such as when parameter types and argument types don't match.
This activity has been created by LuCE Research Lab and is licensed under CC BY-SA 4.0.
Squares
PyTamaro is a project created by the Lugano Computing Education Research Lab at the Software Institute of USI
Privacy Policy ⢠Platform Version c08406b (Wed, 20 Nov 2024 12:30:00 GMT)