Python Practice › Function Programs › Program 1
Function to add two numbers
Write a Python function that adds two numbers and returns the result.
Input
12, 30
Output
42 3.0
Approach
- Define
add(a, b)that returnsa + b.
Solution
def add(a, b): return a + b print(add(12, 30))print(add(2.5, 0.5))Output
42 3.0
Line by line
Line 1
a and b are parameters; their values come from the call.
Line 2
return hands the result back so the caller can use it.
Learn the concept
New to this topic? Read the lesson, then come back.