What Are Functions?
A function is a special part of our code that has a name and can be used again and again, just like a magic spell! Functions help us keep our code neat and tidy, making it easier for us to understand what we wrote. In Python, we can create a function by using the word 'def', which stands for 'define'. Here’s how we can create a simple greeting function:
def greet(name):
print('Hello, ' + name + '!')
When we want to use this function, we just call it with a name. For example, if we call greet('Alex'), it will say:
Hello, Alex!
And if we call greet('Sam'), it will say:
Hello, Sam!
This means we only need to write the greeting code once, but we can use it many times with different names. Isn’t that cool? Functions make our coding life much easier and more fun!