Function Parameters

Think of a function like a juice blender. The blender is the function, but the parameters are the fruits you put inside. If you put in strawberries, you get strawberry juice. If you put in mangoes, you get mango juice! Parameters let your function work with different data every time you use it.

What are Parameters?

Parameters are values that you pass into a function so it can use them to do a job.

  • Inside the Parentheses: They are always written inside the ( ) after the function name.

  • Dynamic Variables: They act like variables that only exist inside that specific function.

  • Flexible: Instead of writing ten different functions to add different numbers, you write one function with parameters!

Real-World Parameter Scenarios

  • Calculator: When you type 5 + 10, the numbers 5 and 10 are parameters sent to the "Add" function.

  • Online Forms: When you type your name into a website, your name is a parameter sent to the "Save User" function.

  • Banking App: When you check your balance, your Account Number is the parameter used to find your money.

  • Video Games: When you pick up a "Power-Up," the amount of extra strength you get is passed as a parameter to your character.

How it Looks (Syntax & Examples)

The Pattern:

def function_name(parameter):
    # The code uses the parameter here


Example 1: A Personalized Greeting

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

# Passing "Ram" as the input
greet("Ram")


Output: Hello Ram


Example 2: Adding Two Numbers

def add(a, b):
    print(a + b)

# Passing 10 and 20 as parameters
add(10, 20)


Output: 30

Summary:

  • Multi-Tasking: A single function can take multiple parameters at once (just separate them with a comma!).

  • Logic Power: Parameters make your code logical and stop you from writing the same code over and over.

  • Efficiency: They allow your program to handle thousands of different inputs using just one small block of code.

Check your knowledge

Quickly verify what you've learned from this tutorial.

Question 1

Where are parameters written when defining a function?

Parameters are always placed inside the round parentheses immediately following the function name.

Question 2

What is the main benefit of using parameters?

Parameters make functions flexible so they can process different data each time they are called.

Question 3

If you define def play(game, level):, how many parameters does this function have?

There are two labels inside the parentheses (game and level), meaning it accepts two inputs

Question 4

What happens if you define a function with one parameter but try to call it with zero parameters?

You must pass the correct number of values that the function is expecting, or it won't know how to run.

Question 5

In Example 2 (add(a, b)), what are 'a' and 'b' called?

In this context, a and b are the parameters that act as placeholders for the numbers you want to add.

Congratulations!

You've successfully mastered the knowledge check for "Function Parameters."

For more questions and practice, click the link below:

Practice More Questions
Previous Topic Functions (def) Next Topic Return Statement