Python - 15
Python - 15: Returning Values From Functions
Functions can send a result back to the rest of your program using return.
Your challenge: Return a number from one function, return text from another, then store and print the results.
Return a result
This function calculates a value and sends it back: return a + b
The returned value can be stored in a variable: result = add_numbers(5, 3)
return is different from print()
print() displays something on the screen. return sends a value back so your program can store it or use it somewhere else.
Return text too
Functions can return strings, numbers, and many other kinds of values.
Try it: Create a function called multiply that returns the result of multiplying two numbers.
Good Python habit: Use return when a function needs to produce a value that other parts of your program can reuse.
👀 Show Example Solution
def add_numbers(a, b):
return a + b
result = add_numbers(5, 3)
print(result)
def full_name(first, last):
return first + " " + last
name = full_name("Alex", "Smith")
print(name)
def multiply(a, b):
return a * b
answer = multiply(4, 6)
print(answer)Each function returns a value, and that value can then be stored in a variable and used later.
