At My Fingertips
Rapid Playground
In this activity you learn how to define your own functions.
Assume you have a friend who does not see the figure below:
Which of the following descriptions is better?
It seems to be circular, so let's call it a circle.
A circle really is a special kind of ellipse. An ellipse where width and height are the same. You may have created a circle in PyTamaro before. Use the ellipse function to create a red circle with diameter 100:
Click on the "RUN" button to check. Does it look like a circle?
It wasn't particularly convenient to create a circle by calling the ellipse
function.
Writing the word "ellipse" in our code, when we really mean the special case of a circle,
isn't that intuitive.
And having to provide the diameter twice, once to specifiy the width, and once to specify the height,
is annoying!
Wouldn't it be nice if there was a specific function to create a circle,
appropriately named circle
,
which only requires one diameter as a parameter?
Ha! Whenever you think "wouldn't it be nice if there was a specific function to...", you should feel happy! Why? Because you can make that wish come true! How? Python allows us to define our own functions!
The two lines starting with def
and return
in the following code cell show how to define a function.
In the line after that, replace _
with a call to your newly defined circle
function,
to create a blue circle with diameter 100.
The keyword def
says that what follows is the definition of a function.
Right after that word, you specify the name of the function
(we named our function circle
).
Then you provide the parameters, within parentheses
(our circle
function has two parameters: diameter
and color
).
The line ends with a colon.
Below that line, you write the body of the function.
The body consists of one or more lines,
each line containing a statement.
In our example, the body consists of a single line, a return
-statement.
Where does the body end?
Good question!
Do you see that the body is indented a bit?
By four space characters.
In Python, indentation matters. A lot!
The body of the function goes on as long as the indentation remains.
When a line is not indented anymore, that line is not part of the body anymore.
In the code cell above,
the line blue_circle = circle(100, blue)
is not part of the body anymore.
What does the body do?
Whenever the function is called, the body gets executed.
The body of the circle
function will call ellipse
, to produce a circle graphic,
and then it will return
that circle graphic.
So, our code cell does four things:
ellipse
, blue
, and show_graphic
circle
, so that whenever it gets called, it will create a circular ellipse with the given diameter and colorcircle
to produce a graphic, and assign that graphic to the name blue_circle
blue_circle
In the following code cell, create and show a green circle with diameter 150, and show it.
First import the definition of the name green
,
and then replace the _
with the appropriate circle
call.
Now create and show a black circle with diameter 80.
This time, don't define a name,
but simply call the circle
function where the _
is.
You practiced creating circles, but much more importantly, you had your first encounter with a very important idea in programming:
def
)Defining a function is a form of abstraction:
you can give a name to some code (to the "body" of the function),
and you can then use that name (to "call" the function)
wherever you need to run that code.
Functions can have parameters (like diameter
).
You can call the same function many times, passing different values to its parameters.
Over time, you will define many, many functions. Defining new functions will become one of your most common activities as a programmer!
This activity has been created by LuCE Research Lab and is licensed under CC BY-SA 4.0.
Circles
PyTamaro is a project created by the Lugano Computing Education Research Lab at the Software Institute of USI
Privacy Policy ⢠Platform Version 1cd5229 (Tue, 05 Nov 2024 16:55:57 GMT)