Welcome to Our Tutorials! Let`s continue.
<What You'll Get Here
We continue with the next lesson.
We study some more python. Ready to dive in? 🚀
Conditional statements
Python Tutorial 5: Introduction to Functions
Program Description
As your programs get bigger, you'll find yourself writing the same code multiple times. Functions help you avoid repetition by bundling a block of code that performs a specific task. You can then "call" this function whenever you need that task done. Functions make your code organized, reusable, and easier to understand.
Illustration
As your programs get bigger, you'll find yourself writing the same code multiple times. Functions help you avoid repetition by bundling a block of code that performs a specific task. You can then "call" this function whenever you need that task done. Functions make your code organized, reusable, and easier to understand.
Tutorial Sections: Building Reusable Code!
Section 1: Defining a Function
Goal: Learn how to create your own functions.
Explanation: You define a function using the `def` keyword, followed by the function name, parentheses `()`, and a colon `:`. The indented block of code after the colon is the function's body.
Code:
# Defining a simple function
def greet():
print("Hello, world!")
What it means: We've defined a function named `greet`. It doesn't do anything yet when you run this code; we've only told Python what the function should do when it's called.
Section 2: Calling a Function
Goal: Execute the code inside a function.
Explanation: To run the code inside a function, you "call" it by using its name followed by parentheses `()`.
Code:
# Defining the function (from Section 1)
def greet():
print("Hello, world!")
# Calling the function
greet()
greet() # You can call it multiple times
What it means: The `greet()` lines execute the code within the `greet` function, printing "Hello, world!" twice.
Section 3: Functions with Parameters
Goal: Pass information into a function.
Explanation: Functions can accept inputs called "parameters" (or arguments). These are listed inside the parentheses in the function definition and act like variables within the function's body.
Code:
# Function with a parameter
def greet_person(name):
print(f"Hello, {name}!")
# Calling the function with arguments
greet_person("Alice")
greet_person("Bob")
What it means: The `greet_person` function takes one parameter, `name`. When we call it, we provide a value (like "Alice" or "Bob") inside the parentheses, which gets assigned to the `name` variable inside the function.
Section 4: Functions with Return Values
Goal: Get a result back from a function.
Explanation: Functions can perform calculations or operations and return a value using the `return` keyword. This returned value can then be used in the rest of your code.
Code:
# Function that returns a value
def add_numbers(a, b):
sum_result = a + b
return sum_result
# Calling the function and using the return value
result = add_numbers(5, 3)
print(f"The sum is: {result}")
print(f"Another sum: {add_numbers(10, 20)}")
What it means: The `add_numbers` function takes two parameters, adds them, and uses `return` to send the `sum_result` back. We store this returned value in the `result` variable and also use the function call directly within a `print` statement.
Awesome! You've learned the basics of creating and using functions in Python. Functions are essential for writing clean, modular, and efficient code. Start thinking about how you can use functions to organize your own programs!