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()
Output:
Hello World
Here:
-
defis the keyword used to define a function. -
greetis 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")
Here:
-
nameis a parameter. -
"Sam"is an argument.
Output:
Hello Sam
3. Positional Arguments
Positional arguments are assigned to parameters based on their position.
def employee(name, department):
print(name, department)
employee("Sam", "IT")
Output:
Sam IT
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")
Output:
Sam Happy Birthday
We can also override the default value:
greet("Sam", "Many more happy returns of the day")
Output:
Sam Many more happy returns of the day
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)
Output:
Sam 25000 IT
Incorrect example:
def employee(name="Sam", salary, department="IT"):
print(name, salary, department)
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")
Output:
Happy Birthday Narmatha
One interesting thing I learned is that we can change the order of keyword arguments.
greet(message="Happy Birthday", name="Narmatha")
Output:
Happy Birthday Narmatha
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
Python accepts this function definition without an error.
We can also write statements after pass:
def greet():
pass
print("Hello")
greet()
Output:
Hello
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. -
returnsends a value back to the caller.
Example:
def calculate_sum():
return 20 - 30
result = calculate_sum()
print(result)
Output:
-10
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:
- What functions are and how to call them.
- Difference between parameters and arguments.
- Positional arguments.
- Default arguments.
- Required and default parameters together.
- Keyword arguments.
- The purpose of the
passstatement. - Difference between
print()andreturn.
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)