Python Functions with Parameters

Let’s look at functions with parameters

  1. Notice

    Parameters are names inside a function. A call binds them to the argument objects before the body runs.

  2. Try

    Follow the example, change its sample, and compare what happens.

  3. Check

    Explain the result and test your understanding.

Watch how it works

Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.

Explore the idea

Follow the code and watch its state change. Guided simulation.

Code
Python
def greet(name):
    return "Hello, " + name
message = greet('Ada')
print(message)
Live state

Top-level scope

greet→Object 1
message→Object 3
Object 1 · functionfunction greetObject 3 · str'Hello, Ada'
This step

Parameters are names inside a function. A call binds them to the argument objects before the body runs.

Picture functions with parameters

Parameters are names inside a function. A call binds them to the argument objects before the body runs.

Image brief

Alt text
A diagram for Python Functions with Parameters. Instructions: def greet(name): return "Hello, " + name message = greet('Ada') print(message). Displayed result: Hello, Ada. The final example bindings are greet --> object 1 (function); message --> object 3 (str). object 1: function greet; object 3: 'Hello, Ada'
Image generation prompt
Create a clean educational illustration for a Python programming lesson titled "Python Functions with Parameters". Teaching focus: Parameters are names inside a function. A call binds them to the argument objects before the body runs. Depict exactly the relationships and labels in this reference diagram: EXAMPLE INSTRUCTIONS def greet(name): return "Hello, " + name message = greet('Ada') print(message) | v DISPLAYED RESULT Hello, Ada FINAL BINDINGS greet --> object 1 (function) message --> object 3 (str) object 1: function greet object 3: 'Hello, Ada' Use a flat vector style, generous spacing, clear arrows and legible labels, with a restrained blue/teal palette on a plain background. The diagram must explain the programming idea accurately. Do not depict variables as boxes containing copied objects when they share a reference. Avoid mascots, decorative screenshots, logos, gradients and extra text. Landscape composition, approximately 3:2, suitable for a course card. Accessible description: A diagram for Python Functions with Parameters. Instructions: def greet(name): return "Hello, " + name message = greet('Ada') print(message). Displayed result: Hello, Ada. The final example bindings are greet --> object 1 (function); message --> object 3 (str). object 1: function greet; object 3: 'Hello, Ada'

Follow the execution

Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.

Execution workspace

Choose a sample and follow its execution. Guided simulation.

Code
Python
def greet(name):
    return "Hello, " + name
message = greet('Ada')
print(message)
Live state

Top-level scope

greet→Object 1
message→Object 3
Object 1 · functionfunction greetObject 3 · str'Hello, Ada'
This step

The example has finished. Compare the final output with the values and references above.

Terminal
Output
Hello, Ada

What do you expect this program to print?

What does this example display?

def greet(name):
    return "Hello, " + name
message = greet('Ada')
print(message)
What does this example display?
Choose one answer. You can try again.

Answer and explanation

Hello, Ada

Follow the printed values in order. The output is: Hello, Ada

Which explanation matches what you learned?

Which explanation matches what you learned?
Choose one answer. You can try again.

Answer and explanation

Parameters are names inside a function. A call binds them to the argument objects before the body runs.

Parameters are names inside a function. A call binds them to the argument objects before the body runs.

More examples and reference

In Python, functions allow you to group a block of code under a name and reuse it whenever needed. But functions become truly powerful when they can accept input in the form of parameters. These inputs allow functions to behave dynamically, depending on the data passed to them.

Why Parameters Matter

Without parameters, a function is just a repetitive block of code. With parameters, it becomes reusable, flexible, and adaptable. Think of parameters like ingredients in a recipe—you can create the same dish with different flavors, depending on what you add.

Defining a Function with Parameters

You define parameters inside the parentheses when writing a function. These are placeholders for values (called arguments) you'll pass when calling the function.

def greet(name):
    print("Hello, " + name + "!")

Explanation: The function greet takes one parameter called name. When you call it, you must pass a value for name.

Calling the Function with Arguments

def greet(name):
    print("Hello, " + name + "!")
    
greet("Arjun")
greet("Bob")
Hello, Arjun!
Hello, Bob!

Note: The values "Arjun" and "Bob" are the actual arguments passed to the function. They replace the name parameter temporarily when the function runs.

Multiple Parameters in a Function

You can define more than one parameter by separating them with commas.

def add(a, b):
    result = a + b
    print("The sum is:", result)
add(10, 5) add(-3, 7)
The sum is: 15
The sum is: 4

Check: Ensure that you pass two values when calling this function. If you forget or pass the wrong number of arguments, Python will throw a TypeError.

Parameters vs Arguments

  • Parameter: The variable name in the function definition (like name, a, b).
  • Argument: The actual value passed when calling the function (like "Arjun", 10).

Default Parameter Values

Sometimes, you want to make a parameter optional. You can do this by assigning it a default value.

def greet(name="User"):
    print("Hello, " + name + "!")
greet("Arjun")
greet()
Hello, Arjun!
Hello, User!

Verification: Default values are used only when no argument is provided. You can override them by passing your own value.

Best Practices and Checks

  • Always pass the correct number of arguments unless defaults are provided.
  • Use descriptive names for parameters (avoid vague names like x, y unless in math contexts).
  • Use print() or return to observe and verify behavior.

Returning Value from Function

In many real-world scenarios, you may want the function to send back a value instead of printing it directly.

def multiply(x, y):
    return x * y
result = multiply(4, 5)
print("Result:", result)
Result: 20

Why return? Returning a value gives you flexibility. You can store it, use it in calculations, or pass it to another function.

What to remember

Connect what you noticed in the example with the idea below.

Remember

Parameters are names inside a function. A call binds them to the argument objects before the body runs.

Watch out for this idea

A parameter and an argument are always the same name.

This is the misconception to avoid.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Python main() Function →