CodingNic

Functions and Reusability

Parameters and Arguments

Functions and Reusability 22 min read

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.

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

Here:

  • greet is the function name
  • name is the parameter

The function is ready to receive a name.

What Are Arguments?

An argument is the real value you send into the function.

python
greet("Ali")

Here:

  • "Ali" is the argument

The value "Ali" is placed into name.

Full Example

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

greet("Ali")
greet("Sara")
greet("John")

Output

text
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.

python
def add(num1, num2):
    print(num1 + num2)

add(5, 3)

Output

text
8

How Values Match

Python matches values by order.

python
def show(name, age):
    print(name, age)

show("Maya", 20)

Here:

  • name gets "Maya"
  • age gets 20

Output

text
Maya 20

Another Example

python
def country(name):
    print("Country:", name)

country("Canada")
country("USA")

Output

text
Country: Canada
Country: USA

Common Beginner Mistakes

Forgetting Required Values

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

greet()

This gives an error because the function expected one value.

Wrong Number of Values

python
def add(num1, num2):
    print(num1 + num2)

add(5)

This gives an error because the function expected two values.

Code Along

python
def city(name):
    print("City:", name)

city("New York")
city("Toronto")

Output

text
City: New York
City: Toronto

Mini Challenge

Build a student greeter.

Steps:

  • Create a function called student
  • Add one parameter called name
  • Print:
    Welcome and the name
  • Call the function with two different names

Expected output:

text
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

  1. What is a parameter?
  2. What is an argument?
  3. Where is a parameter written?
  4. 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.