Functions
9. Functions
Functions are reusable blocks of code that perform a specific task.
Use def to create a function.
def say_hello():
print("Hello Robot!")
say_hello()Functions can also receive information called parameters.
def greet(name):
print("Hello", name)
greet("Clu-Bot")
Functions can return a result using return.
def add(a, b):
return a + b
result = add(5, 3)
print(result)
Why use functions?
- Avoid repeating code.
- Organize your programs.
- Make programs easier to understand.
- Reuse instructions in different projects.
