Python tutorials > Core Python Fundamentals > Functions > How to call functions?
How to call functions?
Basic Function Call
greet("World")
calls the greet
function, passing the string "World" as the argument which is assigned to the name
parameter within the function.
def greet(name):
print(f"Hello, {name}!")
greet("World") # Calling the function with the argument "World"
Functions with Multiple Arguments
add(5, 3)
passes 5
to the x
parameter and 3
to the y
parameter. The function returns their sum, which is assigned to the result
variable.
def add(x, y):
return x + y
result = add(5, 3) # Calling the function with two arguments
print(result) # Output: 8
Keyword Arguments
describe_person(age=30, name="Alice", city="New York")
, we explicitly specify the parameter names, allowing us to pass the arguments out of order.
def describe_person(name, age, city):
print(f"Name: {name}, Age: {age}, City: {city}")
describe_person(age=30, name="Alice", city="New York") # Calling with keyword arguments
Default Argument Values
power
function, exponent
has a default value of 2
. When we call power(5)
, exponent
defaults to 2
. When we call power(5, 3)
, the provided value 3
overrides the default.
def power(base, exponent=2):
return base ** exponent
print(power(5)) # Uses the default exponent value of 2. Output: 25
print(power(5, 3)) # Overrides the default exponent value. Output: 125
Variable-Length Arguments (*args)
*args
syntax allows a function to accept a variable number of positional arguments. These arguments are passed as a tuple. Inside the function, you can iterate over the args
tuple to access each argument. In the sum_all
example, any number of arguments can be passed, and the function calculates their sum.
def sum_all(*args):
total = 0
for num in args:
total += num
return total
print(sum_all(1, 2, 3, 4, 5)) # Output: 15
Keyword Arguments (**kwargs)
**kwargs
syntax allows a function to accept a variable number of keyword arguments. These arguments are passed as a dictionary. Inside the function, you can iterate over the kwargs
dictionary to access each key-value pair. The print_details
function can accept any number of keyword arguments and prints them.
def print_details(**kwargs):
for key, value in kwargs.items():
print(f"{key}: {value}")
print_details(name="Bob", age=40, city="Chicago")
Concepts Behind the Snippet
Real-Life Use Case Section
clean_data(data)
function might be called first, followed by transform_data(cleaned_data)
and finally visualize_data(transformed_data)
.
Best Practices
Interview Tip
*args
and **kwargs
. Also, be ready to discuss the importance of function documentation and the benefits of using functions to improve code readability and maintainability. A common question is how to call a function that takes both positional and keyword arguments; remember the order matters.
When to Use Them
*args
and **kwargs
when you need to create functions that can handle a variable number of arguments.
Memory Footprint
Alternatives
Pros
Cons
FAQ
-
What happens if I call a function with the wrong number of arguments?
Python will raise aTypeError
. The error message will indicate the expected number of arguments and the number of arguments that were actually provided. -
Can I call a function within another function?
Yes, you can definitely call a function within another function. This is a common practice and is essential for building complex and modular programs. -
What is the difference between positional and keyword arguments?
Positional arguments are passed to the function based on their order, while keyword arguments are passed by explicitly specifying the parameter name along with the value. Keyword arguments allow you to pass arguments in any order and improve code readability.