DEV Community

Saravanan Cn
Saravanan Cn

Posted on

#Python Functions: Parameters, Arguments, Default Arguments, and Keyword Arguments

As part of my Python learning journey, today I explored different concepts related to functions. I learned how parameters and arguments work, how default values are assigned, and the difference between positional and keyword arguments.

Let's look at what I learned with simple examples.

1. What is a Function?

A function is a reusable block of code that performs a particular task.

We define a function using the def keyword.

def greet():
    print("Hello World")

greet()
Enter fullscreen mode Exit fullscreen mode

Output:

Hello World
Enter fullscreen mode Exit fullscreen mode

Here:

  • def is the keyword used to define a function.
  • greet is the function name.
  • greet() calls the function.
  • print() displays the output.

2. Parameters vs Arguments

This was one of the important concepts I learned today.

  • Parameters: Variables written inside the parentheses when defining a function.
  • Arguments: Actual values passed when calling a function.

Example:

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

greet("Sam")
Enter fullscreen mode Exit fullscreen mode

Here:

  • name is a parameter.
  • "Sam" is an argument.

Output:

Hello Sam
Enter fullscreen mode Exit fullscreen mode

3. Positional Arguments

Positional arguments are assigned to parameters based on their position.

def employee(name, department):
    print(name, department)

employee("Sam", "IT")
Enter fullscreen mode Exit fullscreen mode

Output:

Sam IT
Enter fullscreen mode Exit fullscreen mode

Python assigns the first argument to the first parameter and the second argument to the second parameter.

4. Default Arguments

A default argument is a value assigned to a parameter while defining a function.

If we don't provide a value when calling the function, Python uses the default value.

def greet(name, message="Happy Birthday"):
    print(name, message)

greet("Sam")
Enter fullscreen mode Exit fullscreen mode

Output:

Sam Happy Birthday
Enter fullscreen mode Exit fullscreen mode

We can also override the default value:

greet("Sam", "Many more happy returns of the day")
Enter fullscreen mode Exit fullscreen mode

Output:

Sam Many more happy returns of the day
Enter fullscreen mode Exit fullscreen mode

5. Required Parameters and Default Parameters Together

We can use both required parameters and default parameters in the same function.

However, required parameters must come before parameters with default values.

Correct example:

def employee(name, salary, department="IT"):
    print(name, salary, department)

employee("Sam", 25000)
Enter fullscreen mode Exit fullscreen mode

Output:

Sam 25000 IT
Enter fullscreen mode Exit fullscreen mode

Incorrect example:

def employee(name="Sam", salary, department="IT"):
    print(name, salary, department)
Enter fullscreen mode Exit fullscreen mode

This produces a SyntaxError because a required parameter cannot follow a parameter with a default value.

6. Keyword Arguments

Keyword arguments are arguments passed using parameter names.

def greet(name, message):
    print(message, name)

greet(name="Narmatha", message="Happy Birthday")
Enter fullscreen mode Exit fullscreen mode

Output:

Happy Birthday Narmatha
Enter fullscreen mode Exit fullscreen mode

One interesting thing I learned is that we can change the order of keyword arguments.

greet(message="Happy Birthday", name="Narmatha")
Enter fullscreen mode Exit fullscreen mode

Output:

Happy Birthday Narmatha
Enter fullscreen mode Exit fullscreen mode

Why is the output the same?

Because Python identifies the values using parameter names rather than their positions.

The order of variables inside the print() statement determines the display order.

7. What is pass?

The pass statement is a placeholder. It does nothing when executed.

It is useful when we want to define a function but haven't written its implementation yet.

def greet():
    pass
Enter fullscreen mode Exit fullscreen mode

Python accepts this function definition without an error.

We can also write statements after pass:

def greet():
    pass
    print("Hello")

greet()
Enter fullscreen mode Exit fullscreen mode

Output:

Hello
Enter fullscreen mode Exit fullscreen mode

pass does not stop the execution of the function.

8. Difference Between print() and return

This was another important concept I learned.

  • print() displays a value on the screen.
  • return sends a value back to the caller.

Example:

def calculate_sum():
    return 20 - 30

result = calculate_sum()
print(result)
Enter fullscreen mode Exit fullscreen mode

Output:

-10
Enter fullscreen mode Exit fullscreen mode

The function calculates the result and returns it. The variable result stores the returned value.

Without print(result), the value is stored but isn't displayed when running a normal Python script.

What I Learned Today

Today I learned:

  1. What functions are and how to call them.
  2. Difference between parameters and arguments.
  3. Positional arguments.
  4. Default arguments.
  5. Required and default parameters together.
  6. Keyword arguments.
  7. The purpose of the pass statement.
  8. Difference between print() and return.

My Learning Takeaway

Initially, I was confused about how Python assigns values to parameters, especially when changing the order of keyword arguments.

After practising different examples, I understood that positional arguments depend on position, while keyword arguments depend on parameter names.

I'm continuing my Python learning journey, one concept at a time.

Top comments (0)