A function is a named, reusable block of code that performs a specific task and can be called from different places in a program. Functions allow you to package behavior, hide implementation details, and avoid repeating the same code each time you need that behavior. The function mean(), for example, takes the arithmetic mean of whatever values you put in the parentheses.
Functions receive data through parameters or arguments and send results back using return values. Parameters define the expected inputs and often include default values, optional parameters, or variadic arguments that accept an arbitrary number of values. A clear return contract (what type or shape the output will be) makes functions safer to compose and easier to make sense of.
So, what does this look like? Well, in Python it could be:
sum([1, 2, 3])
First, we have sum, which is the function name, then we have parentheses () which contain the input, which is [1, 2, 3] in this case, surrounded by square brackets [] so that Python knows they’re all to be used as a single argument by the function. This function “expects” a list of numbers to be entered so it can add them together and give you the result, so it won’t work if you try to put something else in there.