Stop 04 · FunctionsTopics · 2/8

Parameters & Return Values

Part of the Python Roadmap

In short

A function becomes truly useful when you can hand it different inputs and get an answer back. Parameters are how data goes in, and return is how the result comes out.

Parameters are the names a function uses for its inputs, arguments are the values you pass in, and return sends a result back to the caller. A function without return gives back None.

The jaadu feeds sugarcane sticks into a roadside juice machine and catches a frothy glass of juice pouring out the other side.

Think of it like this

The sugarcane juice stall

You hand the vendor two things: sugarcane and how much ginger you want. The machine crushes them and hands back a glass of juice. The vendor's recipe has slots called "cane" and "ginger" (parameters), what you actually hand over are the arguments, and the glass you get back is the return value.

Watch it happen

Calling total = add_gst(1000, 18)

Caller
add_gst

1000, 18

1/4The arguments are matched by position: price = 1000 and rate = 18.

The big ideas

What you need to know

Parameters vs arguments

Parameters are the names in the def line (price, rate). Arguments are the actual values in the call (1000, 18).

return hands back a value

return ends the function and gives a result to the caller, who can store it, print it or pass it on.

No return means None

A function that only prints still returns None. Printing shows a value; returning lets other code use it.

Return several values

return low, high actually returns a tuple, which the caller can unpack: low, high = min_max(data).

See it in code

Walk through the code

python
1def add_gst(price, rate):2    tax = price * rate / 1003    return price + tax4 5 6def greet(name):7    print(f"Hello, {name}!")      # prints, returns nothing8 9 10def min_max(numbers):11    return min(numbers), max(numbers)12 13 14total = add_gst(1000, 18)         # 1000 and 18 are arguments15print(total)                      # 1180.016 17result = greet("Divya")           # Hello, Divya!18print(result)                     # None19 20low, high = min_max([42, 7, 19])21print(low, high)                  # 7 42

1/5price and rate are parameters. return sends the final amount back to whoever called it.

Three functions: one that returns, one that only prints, and one that returns two values.

Save yourself the pain

Common mistakes

Oops

Using print() inside a function and then trying to use the "result" in a calculation.

Do this instead

return the value instead. The caller can always print it, but can't calculate with something only printed.

Oops

Putting code after return and wondering why it never runs.

Do this instead

return ends the function immediately. Move any final steps before it.

Oops

Passing arguments in the wrong order, like add_gst(18, 1000).

Do this instead

Positional arguments match parameters left to right. Use keyword arguments (rate=18) when the order is easy to mix up.

Your mission

Write three small functions

~25 min

Quick check

def f(): print(5). What does x = f() store in x?

Go deeper

  1. 1Write a function that returns a value instead of printing it, then use the result.
  2. 2Return several values as a tuple and unpack them at the call site.
  3. 3Notice the difference between printing inside a function and returning from it.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge