Functions can have multiple parameters:
def multiply(a, b):
return a * b
print(multiply(4, 5)) # 20
You can also set default parameter values:
def power(base, exp=2):
return base ** exp
print(power(3)) # 9 (uses default exp=2)
print(power(3, 3)) # 27
📋 Instructions
Create four functions: `add(a, b)`, `subtract(a, b)`, `multiply(a, b)`, and `divide(a, b)`.
Each should return the result of the operation.
For `divide`, handle division by zero (return `'Error'`).
Test them:
```python
print(add(10, 5))
print(subtract(10, 5))
print(multiply(10, 5))
print(divide(10, 5))
print(divide(10, 0))
```
Expected output:
```
15
5
50
2.0
Error
```
Create 4 functions: add(a, b), subtract(a, b), multiply(a, b), divide(a, b). Each returns the result. For divide, be careful about dividing by zero!
⚠️ Try solving it yourself first — you'll learn more!
def power(base, exp=2):
return base ** exp
print(power(3)) # 9 (uses default exp=2)
print(power(3, 3)) # 27