3
Lesson 3
Calling functions
Calling a function in Python means using the function to perform its defined task. When we call a function, we ask it to execute the code inside it.
1. Basic Syntax of a Function call:
To call a function, write the function’s name followed by parentheses (). If the function takes parameters, pass the necessary arguments inside the parentheses.
1. Basic Syntax of a Function call:
To call a function, write the function’s name followed by parentheses (). If the function takes parameters, pass the necessary arguments inside the parentheses.
def greet(): print("Hello!") greet() # This is calling the function
In this example:
- greet() calls the greet function, so Python runs the code inside it.
- The output is "Hello!".
2. Calling a Function with arguments:
When a function has parameters, we need to provide the corresponding arguments when calling it. The arguments are the actual values passed to the function's parameters, which it uses to perform its task.
def greet(name): print(f"Hello, {name}!") greet("Alice") # Output: Hello, Alice! greet("Bob") # Output: Hello, Bob!
- The greet function has one parameter, name.
- When we call greet("Alice"), "Alice" is passed as the argument for name, and the function outputs "Hello, Alice!".
- Similarly, greet("Bob") outputs "Hello, Bob!".
3. Using return values from Function calls:
When a function returns a value, we can capture that value and use it elsewhere in our code. To do this, assign the function call to a variable or use it directly in expressions.
def add(a, b): return a + b result = add(5, 3) # Calling the function and storing the result in 'result' print(result) # Output: 8
- add(5, 3) calls the add function, with 5 and 3 as arguments.
- The function returns the sum (8), which is stored in the result variable.
- We then print result, which outputs 8.
4. Calling Functions with default parameters:
If a function has default parameters, you can call it without providing arguments for those parameters. In this case, the function will use the default values. However, if you pass values, they override the defaults.
def greet(name="stranger"): print(f"Hello, {name}!") greet() # Output: Hello, stranger! greet("Charlie") # Output: Hello, Charlie!
- greet() without arguments uses the default value, "stranger".
- greet("Charlie") overrides the default, using "Charlie" as name.
5. Calling Functions with multiple arguments:
When a function has multiple parameters, you must provide arguments in the correct order or use keyword arguments.
Example with positional arguments:
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)
Example with keyword arguments:
def subtract(a, b): return a - b result = subtract(b=5, a=10) # Output: 5 print(result)
In the keyword arguments example, we specify which argument goes to each parameter, regardless of their order.
6. Function calls within expressions:
Since function calls can return values, you can use them directly in expressions or pass their results 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)
add_squares(3, 4) calls square(3) and square(4), adds their results, and returns 25.
Summary
- Calling a function means executing it using function_name(arguments).
- Arguments must match the parameters in number and order unless you use keyword arguments.
- A function call may return a result, which you can store or use directly.
- Functions can be called with or without arguments, depending on how they’re defined.
This flexibility makes functions reusable and essential for building complex programs.