At My Fingertips

Rapid Playground

Documentation

Circles

In this activity you learn how to define your own functions.

Asking for a Graphic

Assume you have a friend who does not see the figure below:

Alt text

Which of the following descriptions is better?

  1. A blue ellipse
  2. A blue circle

It seems to be circular, so let's call it a circle.

Create 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 PyTamaro iconellipse function to create a red circle with diameter 100:

Loading...

Click on the "RUN" button to check. Does it look like a circle?

Defining a Function to Conveniently Create Circles

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.

Loading...

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.

Python Function Definition

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:

  1. Import the names ellipse, blue, and show_graphic
  2. Define the function circle, so that whenever it gets called, it will create a circular ellipse with the given diameter and color
  3. Call the function circle to produce a graphic, and assign that graphic to the name blue_circle
  4. Show the graphic known by the name blue_circle

Different Circles

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.

Loading...

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.

Loading...

What You Learned

You practiced creating circles, but much more importantly, you had your first encounter with a very important idea in programming:

  • creating your own function (using 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

Logo of PyTamaro

PyTamaro is a project created by the Lugano Computing Education Research Lab at the Software Institute of USI

Privacy PolicyPlatform Version d693e64 (Sun, 05 May 2024 13:57:26 GMT)