0
0
Pythonprogramming~15 mins

Return values in Python - Mini Project: Build & Apply

Choose your learning style9 modes available
Return values
๐Ÿ“– Scenario: You are creating a simple calculator function that adds two numbers. This is like using a calculator to get the sum of two amounts you have in your wallet.
๐ŸŽฏ Goal: Build a function called add_numbers that takes two numbers and returns their sum. Then, use this function to add two numbers and print the result.
๐Ÿ“‹ What You'll Learn
Create a function named add_numbers with two parameters: a and b
Inside the function, return the sum of a and b
Call the function add_numbers with the numbers 5 and 7
Store the result in a variable called result
Print the value of result
๐Ÿ’ก Why This Matters
๐ŸŒ Real World
Functions that return values are used everywhere, like calculating totals in shopping carts or scores in games.
๐Ÿ’ผ Career
Understanding return values is essential for writing reusable code and building programs that process and return data.
Progress0 / 4 steps
1
Create the function add_numbers
Write a function called add_numbers that takes two parameters named a and b. Inside the function, return the sum of a and b.
Python
Need a hint?

Use the def keyword to create a function. Use return to send back the sum.

2
Call the function and store the result
Call the function add_numbers with the numbers 5 and 7. Store the returned value in a variable called result.
Python
Need a hint?

Use the function name with parentheses and pass the numbers inside. Assign the call to result.

3
Print the result
Write a print statement to display the value stored in the variable result.
Python
Need a hint?

Use print(result) to show the sum on the screen.

4
Try with different numbers
Call the function add_numbers again with the numbers 10 and 15. Store the returned value in a variable called new_result. Then print new_result.
Python
Need a hint?

Repeat the call with new numbers and print the new result.