A function is a reusable block of code that performs a specific task. Functions help organize your code and make it easier to maintain.
You define a function using the def
keyword:
def greet():
print("Hello!")
You call the function by its name followed by parentheses:
greet() # This will print "Hello!"
Functions allow you to avoid repeating code. You can write code once and run it multiple times whenever needed.
Functions can take parameters (inputs) to make them more flexible.
Example:
def greet(name):
print("Hello, " + name + "!")
# Call it with an argument:
greet("Alice") # Prints "Hello, Alice!"