Quick Overview
A function with no `return` hands back `None`, so a forgotten one surfaces as a `TypeError` at the call site rather than at the line that actually caused it.
Create a Python function addition(a, b) that takes two numbers as parameters and returns their total sum.
Function parameters and return values form the foundation of Python programming. Mastering function inputs allows you to process dynamic data cleanly.
In Python, functions are declared using the def keyword. The + operator computes the arithmetic sum:
def addition(a, b):
return a + b
# Testing our function
print(addition(3, 2)) # Output: 5
print(addition(-3, -6)) # Output: -9
print(addition(7, 3)) # Output: 10
addition(3, 2) is invoked, parameter a receives value 3 and parameter b receives value 2.3 + 2 to compute integer 5.return keyword exits the function and sends 5 back to the caller."3" and "2"), Python performs string concatenation ("3" + "2" == "32"). Use int(a) + int(b) if parsing user text inputs!print(a + b) inside the function displays the value on the console, but returns None to the caller.Always use return to output values from functions. Next, let's convert minutes into seconds!
`minutes * 60` returns an `int` when given an `int`, while `/` always produces a float regardless of the operands. Multiplication preserves the type; division does not.
`base * height / 2` always yields a float in Python 3 because `/` never returns an int, even for whole numbers. `//` floors instead, which is almost never what an area wants.
Python integers have no fixed width, so `n + 1` never overflows the way a 32-bit `int` would. The value simply grows to whatever memory allows.