2
Lesson 2
Anatomy of a Function
1. def Keyword:
def is a keyword in Python that stands for "define." It tells Python that you are creating a function.
When Python encounters the def keyword, it expects that a new function is being defined.
def is a keyword in Python that stands for "define." It tells Python that you are creating a function.
When Python encounters the def keyword, it expects that a new function is being defined.
def greet(): print("Hello!")
2. Function name:
The function name is how we refer to and call the function in the rest of our code.
Function names should follow Python's naming conventions:
- They should start with a lowercase letter or underscore and use underscores to separate words (snake_case).
- They should be descriptive of the function's purpose, so it's clear what the function does.
def calculate_area(): # "calculate_area" is the function name pass # "pass" is a placeholder if we haven't written code yet
3. Parameters (or Arguments):
Parameters are optional inputs that you define within the parentheses after the function name.
They allow the function to accept values that affect its behavior.
Parameters act as placeholders for values passed into the function when it's called.
A function can have zero, one, or multiple parameters.
If parameters are specified, they must be given values (called arguments) when calling the function.
def greet(name): # "name" is a parameter print(f"Hello, {name}!")
In this example, name is a parameter. When we call greet("Alice"), "Alice" is passed to the function as an argument, and name is assigned this value inside the function.
Multiple parameters:
def add_numbers(a, b): # "a" and "b" are parameters return a + b
Here, a and b allow the function to add any two numbers that we pass when calling it.
4. Function body:
The body of the function is the indented code block that comes after the function definition line.
The body contains the code that will be executed each time the function is called.
def greet(name): print(f"Hello, {name}!") # This line is the body of the function
5. Return statement:
The return statement is used to send a result back to where the function was called.
When Python encounters a return statement, it exits the function and optionally passes a value back to the caller.
If a function has no return statement, it will return None by default.
Using return allows you to use the result of a function in further calculations or assignments.
def add_numbers(a, b): return a + b # Returns the result of "a + b"
In this example, calling add_numbers(3, 5) will return 8, which can then be used in further expressions.
6. No return statement:
def print_greeting(): print("Hello, world!")
Here, print_greeting() prints a message but doesn’t return any value, so it implicitly returns None.
7. Putting It all together:
Here's a complete example of a function that includes all these parts:
def calculate_area_of_rectangle(length, width): # 'def' keyword, function name, parameters area = length * width # Body of the function return area # Return statement
When we call this function as calculate_area_of_rectangle(5, 10), it:
1. Multiplies length and width (5 and 10) to calculate the area.
2. Returns 50 as the result, which we can then print or use further in the code.
Example of calling and using the result:
result = calculate_area_of_rectangle(5, 10) print(f"The area of the rectangle is: {result}")
Output:
The area of the rectangle is: 50
Summary
Each function in Python has:
- The def keyword to start the definition.
- A function name to identify it.
- Optional parameters that allow it to accept inputs.
- A body where the main logic is written.
- A return statement (if necessary) to pass back a result.
Together, these components make functions powerful tools for creating reusable, organized, and modular code in Python.