Parameters and Arguments
Parameters and Arguments
Some functions always do the same thing.
But many times, we want a function to work with different values.
For example:
- Greet different people
- Add different numbers
- Show different messages
Parameters and arguments make this possible.
What Are Parameters?
A parameter is a variable written inside a function.
It is used to receive a value when the function runs.
def greet(name):
print("Hello", name)
Here:
greetis the function namenameis the parameter
The function is ready to receive a name.
What Are Arguments?
An argument is the real value you send into the function.
greet("Ali")
Here:
"Ali"is the argument
The value "Ali" is placed into name.
Full Example
def greet(name):
print("Hello", name)
greet("Ali")
greet("Sara")
greet("John")
Output
Hello Ali
Hello Sara
Hello John
Why This Is Useful
Without parameters, you would need many separate functions.
With parameters, one function can work with many values.
More Than One Parameter
A function can receive more than one value.
def add(num1, num2):
print(num1 + num2)
add(5, 3)
Output
8
How Values Match
Python matches values by order.
def show(name, age):
print(name, age)
show("Maya", 20)
Here:
namegets"Maya"agegets20
Output
Maya 20
Another Example
def country(name):
print("Country:", name)
country("Canada")
country("USA")
Output
Country: Canada
Country: USA
Common Beginner Mistakes
Forgetting Required Values
def greet(name):
print("Hello", name)
greet()
This gives an error because the function expected one value.
Wrong Number of Values
def add(num1, num2):
print(num1 + num2)
add(5)
This gives an error because the function expected two values.
Code Along
def city(name):
print("City:", name)
city("New York")
city("Toronto")
Output
City: New York
City: Toronto
Mini Challenge
Build a student greeter.
Steps:
- Create a function called
student - Add one parameter called
name - Print:
Welcomeand the name - Call the function with two different names
Expected output:
Welcome Ali
Welcome Sara
Real World Use Case
Programs use parameters in login systems, calculators, games, reports, and apps that work with changing data.
Quiz
- What is a parameter?
- What is an argument?
- Where is a parameter written?
- Can a function have two parameters?
Assignment
Create a function called multiply with two parameters. Print the answer when called with 4 and 5.
Summary
You learned that parameters receive values, arguments send values, and functions become more powerful when they can work with different data.