Python - Functions
In Python, parameters and arguments are terms associated with functions. Let's clarify the difference between them.
Parameters: These are the variables defined in the function signature. They act as placeholders for the values that will be passed into the function when it is called.
Arguments: These are the actual values that are passed into a function when it is called. They correspond to the parameters in the function definition.
Now, let's look at an example:
def multiply(x, y):
result = x * y
return result
# x and y are parameters in the function definition
# 5 and 3 are arguments passed into the function
product = multiply(5, 3)
print(f"The product is: {product}")
In this example, x
and y
are parameters of the multiply
function. When we call the function with multiply(5, 3)
, 5 and 3 are the arguments. The values of 5 and 3 get assigned to the parameters x
and y
inside the function.
Output:
The product is: 15
return
statement in a function?
The return
statement in a function is used to exit the function and return a value to the caller. It marks the end of the function's execution, and the specified value is sent back to the point in the program where the function was called.
Let's illustrate this with an example:
def add_numbers(x, y):
sum_result = x + y
return sum_result
# Call the function and store the result in the variable 'result'
result = add_numbers(3, 7)
# Print the result
print(f"The sum is: {result}")
In this example, the add_numbers
function takes two parameters x
and y
, calculates their sum, and returns the result using the return
statement. The returned value (sum) is then assigned to the variable result
when the function is called.
The sum is: 10
In Python, you can provide default values to function parameters by specifying the default values in the function definition. This allows you to call the function without providing values for those parameters, and the default values will be used.
Let's demonstrate this with an example:
def greet(name, greeting="Hello"):
print(f"{greeting}, {name}!")
# Call the function without providing 'greeting'
greet("Alice")
# Call the function with a custom 'greeting'
greet("Bob", "Good morning")
In this example, the greet
function has a default value of "Hello" for the parameter greeting
. When the function is called without providing a value for greeting
, it uses the default value. If a custom value is provided, it overrides the default.
Output:
Hello, Alice! Good morning, Bob!
In Python, variable-length argument lists in functions allow you to pass a variable number of arguments to a function. This is achieved by using the special syntax of *args
for variable positional arguments and **kwargs
for variable keyword arguments.
Let's illustrate this with an example:
def print_args(*args, **kwargs):
print("Positional arguments:", args)
print("Keyword arguments:", kwargs)
# Call the function with different arguments
print_args(1, 2, 3, name="Alice", age=30)
print_args("Hello", "world", greeting="Hi")
In this example, the print_args
function accepts any number of positional arguments using *args
and any number of keyword arguments using **kwargs
. The function then prints the received arguments.
Output:
Positional arguments: (1, 2, 3) Keyword arguments: {'name': 'Alice', 'age': 30} Positional arguments: ('Hello', 'world') Keyword arguments: {'greeting': 'Hi'}