First Function
Create a function called 'say_hi' that prints "Hi!". Then call it.
Functions are reusable blocks of code.
Create a function with def:
def say_hello():
print("Hello!")
# Call the function
say_hello() # Prints: Hello!
Functions with parameters:
def greet(name):
print("Hello, " + name + "!")
greet("Alice") # Prints: Hello, Alice!
Functions that return values:
def add(a, b):
return a + b
result = add(3, 5) # result is 8
Create a function called 'say_hi' that prints "Hi!". Then call it.
Create a function 'greet' that takes a name and prints: "Hello, [name]!" Then call: greet("World") Expected output: Hello, World!
Create a function 'double' that takes a number and returns it multiplied by 2. Call it with 5 and print the result. Expected output: 10
Create a function 'add' that takes two numbers and returns their sum. Call: add(10, 20) and print the result. Expected output: 30
Create a function 'multiply' that takes two numbers and returns their product. Test it with: multiply(6, 7) Expected output: 42