The Four Shapes
Two questions decide the shape of every function you will ever write. Does it need anything from the caller? That is parameters. Does it hand anything back? That is return. Two questions, two answers each — so four shapes, and every function is one of them.
| No parameters | With parameters | |
|---|---|---|
| No return | Shape 1 def line():
print('-' * 30)Does the same thing every time. | Shape 2 def greet(name):
print('Hello,', name)Does a different thing each time. |
| Returns a value | Shape 3 def roll():
return random.randint(1, 6)Needs nothing, produces something. | Shape 4 def area(l, b):
return l * bThe workhorse. Takes input, gives an answer. |
1Shape 1 — no parameters, no return
The simplest kind. It needs nothing from you and hands nothing back; it just does something, and it does the same thing every time.
def line():
print('-' * 30)
line()------------------------------
Printing a header, printing a menu, clearing the screen — jobs with no variation and no answer.
2Shape 2 — parameters, no return
Now the caller supplies something, and the function does a slightly different thing each time. It still hands nothing back — the result of its work is what appears on the screen.
def greet(name):
print('Hello,', name + '!')
greet('Riya')
greet('Amit')Hello, Riya! Hello, Amit!
3Shape 3 — no parameters, returns a value
This one needs nothing from you but produces an answer for you to use. Anything that fetches or generates something falls here.
import random
def roll():
return random.randint(1, 6)
face = roll()
print('You rolled', face)You rolled 4
random — and it is exactly why this function must return rather than print: the caller needs to keep the value to compare it, add it up, or check it against a guess.4Shape 4 — parameters and a return
The workhorse, and the shape most exam questions want. Give it what it needs, get an answer back.
def area(length, breadth):
return length * breadth
a = area(12, 5)
print('Area is', a)
print('Two rooms need', area(12, 5) + area(9, 4), 'square metres of carpet')Area is 60 Two rooms need 96 square metres of carpet
Look at the last line. Because area() hands a number back, two calls can be added together in the middle of an expression. That is something none of the printing shapes can do.
5The difference that actually matters
print and return are the two most commonly confused words in this chapter, because on screen they can look identical. Here are two functions that add two numbers:
def add_show(a, b):
print(a + b) # shows it
def add_give(a, b):
return a + b # hands it back
add_show(3, 4)
x = add_show(3, 4)
print('add_show handed back:', x)
y = add_give(3, 4)
print('add_give handed back:', y)
print('and it can be used:', add_give(3, 4) * 10)7 7 add_show handed back: None add_give handed back: 7 and it can be used: 70
print shows a value to the person. return gives a value to the program. A function that only prints hands back None, so its answer cannot be stored, added, compared or passed on. The screen is a dead end; the return value is not.Try to use a printing function's “answer” and the None shows up immediately:
def add_show(a, b):
print(a + b)
total = add_show(3, 4) * 107
Traceback (most recent call last):
File "print_cannot_be_used.py", line 4, in <module>
total = add_show(3, 4) * 10
~~~~~~~~~~~~~~~^~~~
TypeError: unsupported operand type(s) for *: 'NoneType' and 'int'The 7 is printed, because the function ran. Then the multiplication fails, because what the function gave back was nothing at all.
The same rule read the other way round is just as important. A function that does return must have its value caught — put on the right of an =, or inside a print, or used in a sum. Write add_give(3, 4) on a line by itself and the 7 is worked out and then quietly thrown away, with no error and nothing on the screen to tell you. The Returning Values lesson comes back to this.
6Choosing the shape
1. Would this function do the same thing every single time? If yes, it needs no parameters. If it should behave differently for different data, that data comes in as parameters.
2. Does the caller need the answer, or just need to see it? If the program has to do anything at all with the result — store it, add it, test it — the function must return. Only print when the screen really is the destination.
print(area(12, 5)) — but a function that prints can never be un-printed. Returning keeps both options open, which is why almost every exam answer wants one.7Recap
None, if it does the same job every time. One or more, if it should work on different data.
Nothing, if the work ends on the screen. A value, if the program has to use the result.
A function that only prints hands back None, and None cannot be stored, added or compared.
Parameters in, a value out. It is the shape almost every exam question is asking for.
- 1
Write one function of each of the four shapes, and call each one.
Hint · A banner, a greeting, something from
random, and a calculation. Four functions, about ten lines. - 2
Write
cube(n)two ways — one printing, one returning — then try to add two cubes together with each.Hint · The printing one gives
TypeError: unsupported operand type(s) for +, because both halves areNone. - 3
Write
is_pass(mark)that returnsTrueorFalse, and use it inside anif.Hint ·
return mark >= 33is the whole body. A function returning a Boolean fits straight into a condition. - 4
Look at three functions you wrote in Class 11 and name the shape of each.
Hint · Most will be shape 2 or shape 4. If a shape-2 function's result is ever needed later, it wanted to be a shape 4.
What does a function that only prints hand back to its caller?
Which shape is `def roll(): return random.randint(1, 6)`?
Why is returning usually safer than printing?