Goal

You’ll be able to write functions with parameters and return values, and understand the genuinely important difference between a function that returns a value and one that just prints it.

Learn

A function packages reusable code under a name, so you don’t repeat yourself every time you need that logic:

def greet(name):
    return f"Hello, {name}!"

message = greet("Ahmed")
print(message)  # "Hello, Ahmed!"

name here is a parameter — a placeholder for whatever value gets passed in when the function is called. "Ahmed" is the argument — the actual value provided at call time.

The genuinely important distinction: return sends a value back to whatever called the function, so it can be stored, used in further calculations, or passed elsewhere. print() just displays something on screen — it doesn’t give the calling code anything usable:

def add_v1(a, b):
    print(a + b)  # only displays, returns nothing usable

def add_v2(a, b):
    return a + b  # actually gives back a usable value

result1 = add_v1(3, 4)  # prints "7", but result1 is actually None!
result2 = add_v2(3, 4)  # result2 is actually 7, usable in further code

A function with no explicit return statement automatically returns None — this is exactly why result1 above is None, not 7, even though “7” was visibly printed to the screen.

Decision Task

A function calculates a total and uses print(total) instead of return total. Before reading on: what happens if you try to use that function’s result in a further calculation, like final = calculate_total(items) * 1.1?

Show Answer

This will fail or produce a nonsensical result, since a function using print() instead of return actually returns None by default. final = calculate_total(items) * 1.1 would attempt None * 1.1, which raises a TypeError — you can’t multiply None by a number, since print() never actually hands back a usable value to the calling code, regardless of what got displayed on screen.

Common Mistake

Using print() inside a function when the calling code actually needs to use the result further, confusing “displaying a value to a human” with “giving the calling code a usable value.” A function’s job is very often to compute and return something usable, not to directly display output — that’s frequently a separate concern, often handled by whatever code calls the function.

Practice Questions

1. Write a function called square that takes one parameter and returns its square.

Show Answer

def square(x):\n return x ** 2

2. What does a function return by default if it has no explicit return statement?

Show Answer

None

3. Given def multiply(a, b): print(a * b), what does result = multiply(3, 4) actually store in result?

Show Answer

None — even though “12” gets printed to the screen, the function never actually returns anything, so result is None, not 12.

4. What’s the difference between a parameter and an argument?

Show Answer

A parameter is the placeholder name defined in the function itself; an argument is the actual value passed in when the function is called.

5. True or False: a function can only return one value.

Show Answer

False, functionally — Python allows returning multiple values at once as a tuple, e.g. return a, b, though this is a slightly more advanced pattern than covered directly in this lesson’s core examples.

Try It Yourself

Without looking back, write a function called is_even that takes a number and returns True if it’s even, False otherwise, using the modulo operator from Part 1.2.

Show Answer

def is_even(n):\n return n % 2 == 0 — n % 2 gives the remainder when dividing by 2; if that remainder is 0, the number is even.

Quick Check

1. What does the return statement do?

Show Answer

Sends a value back to whatever code called the function, making it usable elsewhere.

2. What does a function return if it has no explicit return statement?

Show Answer

None

3. Why is packaging repeated logic into a function generally better than copy-pasting the same code multiple times?

Show Answer

A function centralizes that logic in one place — if it needs fixing or changing later, you update it once instead of finding and editing every copy-pasted instance separately, reducing the chance of missing one and introducing inconsistent behavior.

4. Why can’t you use the result of a function that only uses print() in further calculations?

Show Answer

Because print() only displays output; it doesn’t return a usable value, so the function actually returns None regardless of what was printed.

5. What keyword defines a function in Python?

Show Answer

def

تحميل هذا الباب / Download this Chapterنسخة كاملة للدراسة بدون إنترنت، مع الأسئلة والإجابات والصور المتاحة.