Arguments and Return Values
Functions become much more powerful when they can receive information and send information back.
Think about a vending machine.
You put something into it.
The machine processes your input.
Then something comes out.
Coins → Vending Machine → Soda
Functions can work in the same way.
Arguments → Function → Return Value
What Are Arguments?
An argument is information you give to a function when you call it.
For example:
print("Hello")The string "Hello" is an argument passed to the print function.
You can create your own function that accepts information.
def greet(name): print("Hello", name)Then:
greet("Aarav")Output:
Hello Aarav
The value "Aarav" is an argument.
Parameters vs Arguments
Beginners often hear both words.
Consider:
def greet(name): print("Hello", name)Here, name is called a parameter.
When you call:
greet("Aarav")"Aarav" is the argument.
A simple way to remember:
Parameter → Placeholder in the function definition
Argument → Actual value supplied during the call
The Vending Machine Analogy
Imagine a vending machine.
You insert:
₹20
The machine uses that information to determine what to give you.
In programming:
def buy_soda(money): print("Money received:", money)Calling:
buy_soda(20)is like putting ₹20 into the machine.
The function receives that value through the parameter money.
Multiple Arguments
A function can accept multiple pieces of information.
For example:
def add(a, b): print(a + b)Calling:
add(10, 20)Output:
30
Here:
a → 10
b → 20
The function uses both values.
What Is return?
So far, our functions have printed results.
But printing something and returning something are not the same.
Consider:
def add(a, b): return a + bNow:
result = add(10, 20)The function calculates:
10 + 20 = 30
and sends 30 back to the caller.
The value can then be stored:
result = add(10, 20)print(result)Output:
30
print vs return
This difference is extremely important.
def add(a, b): print(a + b)This displays the result.
return
def add(a, b): return a + bThis sends the result back to the code that called the function.
Think:
print → "Show this to the user."
return → "Give this value back to the caller."
The Vending Machine Analogy Continued
bill = calculate_total(100, 3)When a variable is looked up inside calculate_total(100, 3), Python searches the scopes from left to right. Once it finds the name, it stops searching immediately!
Why Functions Isolate Scope: When a function returns, its frame is popped from the stack and its local variables vanish. This prevents functions from accidentally overwriting variables in the rest of your program!
Imagine:
You insert money
↓
Vending Machine
↓
Machine processes request
↓
Soda comes out
A function works similarly:
Argument
↓
Function
↓
Processing
↓
Return Value
For example:
def add(a, b): return a + bCalling:
result = add(10, 20)means:
10 and 20
↓
add function
↓
calculate 10 + 20
↓
return 30
↓
result receives 30
Data Flow Diagram
flowchart LR
A["Argument: 10"] --> C["add function"]
B["Argument: 20"] --> C
C --> D["Calculate 10 + 20"]
D --> E["return 30"]
E --> F["result"]
This is one of the most important ideas in functions.
Data can flow into a function and a result can flow out.
Functions Can Return Different Types
A function can return a string:
def get_message(): return "Hello"A function can return a number:
def get_age(): return 21A function can return a boolean:
def is_adult(age): return age >= 18For example:
answer = is_adult(20)print(answer)Output:
True
Returning Early
When Python reaches return, the function immediately finishes.
For example:
def check_age(age): if age < 18: return "Not allowed" return "Allowed"If we call:
print(check_age(16))we get:
Not allowed
The function stops at the first return.
A Function Can Accept and Return Data
This is a very common pattern:
def calculate_total(price, quantity): total = price * quantity return totalThen:
bill = calculate_total(100, 3)print(bill)Output:
300
The complete data flow is:
price + quantity
↓
function
↓
calculation
↓
total
↓
return
↓
bill
Real-World Example: Shopping Cart
Imagine an online store.
You have:
price = 500quantity = 2A function can calculate the total:
def calculate_total(price, quantity): return price * quantityThen:
total = calculate_total(price, quantity)The function receives the data and gives the answer back.
This makes the function reusable.
For example:
total1 = calculate_total(500, 2)total2 = calculate_total(100, 5)total3 = calculate_total(250, 4)One function can handle many different inputs.
Why Return Values Matter
Suppose a function calculates a customer's total bill.
If it only prints:
def calculate_total(price, quantity): print(price * quantity)you can see the result, but other parts of the program cannot easily use that result as a value.
With:
def calculate_total(price, quantity): return price * quantityyou can do:
total = calculate_total(100, 3)discount = total * 0.10final_price = total - discountThe returned value becomes useful to the rest of the program.
The Function Mental Model
When you see:
result = function_name(data)think:
Data
↓
Function
↓
Processing
↓
Return value
↓
Result
This mental model will help you understand much larger programs.
Key Takeaways
- Arguments are values supplied to a function.
- Parameters are placeholders used in the function definition.
- A function can accept one or multiple arguments.
returnsends a value back to the caller.printdisplays something;returngives something back.- A returned value can be stored in a variable.
- Functions can transform input data into useful output.
- The vending machine is a useful mental model: input goes in, processing happens, output comes out.
Beginner rule: Think of a function as a machine: give it input, let it work, and use the value it returns.