Python lesson
Functions, step 2
profit(140, 90) works out 140 - 90 and hands back 50, which is stored in p. A function can take more than one input: separate them with commas.
Try this lesson free →Example
return sends a value back to wherever the function was called, so you can store it or use it:
def profit(revenue, cost):
return revenue - cost
p = profit(140, 90)
print(p)
print(profit(70, 45))50
25Key points
returnSends a value back to where the function was called.
p = profit(140, 90)Store what comes back, or use it straight away.
print vs returnprint only shows a value. return hands it back so you can use it.
Put together: def profit(revenue, cost): then, indented, return revenue - cost.
Practice
1. Build the line that sends back the VAT: amount times 0.075.
return amount * 0.0752. Build a line that stores the result of profit(140, 90) in p.
p = profit(140, 90)3. Write a function called double that returns a number times 2. Then print double(21).
def double(n):
return n * 2
print(double(21))4. Write a function margin(revenue, cost) that returns revenue minus cost. Then print margin(140, 90).
def margin(revenue, cost):
return revenue - cost
print(margin(140, 90))5. Write a function label(revenue) that returns "Big" if revenue is over 50, otherwise "Small". Then print label(70).
def label(revenue):
if revenue > 50:
return "Big"
return "Small"
print(label(70))Learn it by doing. This lesson is a short hands-on level on Bampaa: see it work, then build and write it yourself, with instant feedback.
Try this lesson free →