Functions: Reusable Code Blocks
Functions in Python
Functions are like mini-programs within your code. They package instructions that you can reuse whenever you need them.
Defining a Function
def greet(name):
message = f"Hello, {name}!"
return message
Calling a Function
result = greet("Alice")
print(result) # Prints: Hello, Alice!
Function with Multiple Parameters
def add_numbers(a, b):
return a + b
sum_result = add_numbers(3, 5) # Returns 8
Function without Return
def print_greeting(name):
print(f"Hello, {name}!")
print_greeting("Bob") # Prints directly
Remember
def function_name(parameters):defines the function- Parameters are inputs to the function
returnsends a value back (optional)- Functions make code organized and reusable
- Call functions by name with parentheses
Next, practice writing your own functions!
Functions: Reusable Code Blocks
Functions in Python
Functions are like mini-programs within your code. They package instructions that you can reuse whenever you need them.
Defining a Function
def greet(name):
message = f"Hello, {name}!"
return message
Calling a Function
result = greet("Alice")
print(result) # Prints: Hello, Alice!
Function with Multiple Parameters
def add_numbers(a, b):
return a + b
sum_result = add_numbers(3, 5) # Returns 8
Function without Return
def print_greeting(name):
print(f"Hello, {name}!")
print_greeting("Bob") # Prints directly
Remember
def function_name(parameters):defines the function- Parameters are inputs to the function
returnsends a value back (optional)- Functions make code organized and reusable
- Call functions by name with parentheses
Next, practice writing your own functions!
main.py
Loading...
Click "Run Code" when you are ready to test your solution.
