In Python, calling a function means executing the code inside it to perform its defined task. When we call a function, we "activate" it, asking it to perform its job.
Basic syntax of a function call
To call a function, write its name followed by parentheses ()
def greet():
print("Hello!")
greet() # This is calling the function
Explanation:
calls thegreet() greet
function, so Python executes the code inside it.- The output is:
Hello!
Calling a function with arguments
When a function has parameters, you must provide arguments to match those parameters during the function call. These arguments are the actual values the function uses to perform its task.
def greet(name):
print(f"Hello, {name}!")
greet("Alice") # Output: Hello, Alice!
greet("Bob") # Output: Hello, Bob!
Explanation:
- The
function has one parameter,greet
name
. - When we call
, the valuegreet("Alice")
is passed as an argument toAlice
name
, resulting in the output:Hello, Alice!
Using return values from function calls
A function can return a value that can be stored in a variable or used in an expression.
def add(a, b):
return a + b
result = add(5, 3) # Calls the function and stores the result in 'result'
print(result) # Output: 8
Explanation:
calls the function with argumentsadd(5, 3) 5
and3
.- The function returns the sum (
8
), which is stored in the variable.result
- Printing
outputs:result
8
Calling functions with default parameters
A function can have default parameter values. If no argument is provided for a parameter, the default value is used.
def greet(name="stranger"):
print(f"Hello, {name}!")
greet() # Output: Hello, stranger!
greet("Charlie") # Output: Hello, Charlie!
Explanation:
- When
is called without arguments, the default valuegreet()
is used.stranger
- When
is called, the default is overridden withgreet("Charlie")
.Charlie
Calling functions with multiple arguments
For functions with multiple parameters, provide arguments in the correct order or use keyword arguments to specify them explicitly.
Positional arguments example:
def subtract(a, b):
return a - b
result1 = subtract(10, 5) # Output: 5 (10 - 5)
result2 = subtract(5, 10) # Output: -5 (5 - 10)
print(result1, result2)
Keyword arguments example:
def subtract(a, b):
return a - b
result = subtract(b=5, a=10) # Output: 5
print(result)
Explanation:
- Positional arguments must follow the order of parameters.
- Keyword arguments let you specify which value corresponds to which parameter, regardless of order.
Function calls within expressions
Since function calls can return values, you can use those values in expressions or pass them to other functions.
def square(x):
return x * x
def add_squares(a, b):
return square(a) + square(b)
result = add_squares(3, 4) # Output: 25 (9 + 16)
print(result)
Explanation:
callsadd_squares(3, 4) andsquare(3)
.square(4)
- Their results (
9
and16
) are added together,returning .25
Summary
- Calling a function executes its code using
.function_name(arguments)
- Arguments must match the parameters in number and order unless you use keyword arguments.
- Functions can return results, which you can store or use directly in expressions.
- Default parameters allow flexibility, letting you omit arguments when calling the function.
- Functions are reusable building blocks that make complex programs simpler and more efficient.