Python Functions
Functions are a major tool for structuring programs. We already used a number
of functions, such as input
, int
, print
,
and math.sin
.
Functions can return a value and they can have any number of arguments (a.k.a. parameters). If they do not return a value, they usually do something such as printing something out.
The syntax of the function definition statement is given above. We have the
keyword def
, followed by a function name, followed by a pair
of parentheses, in which there can be any number of parameters.
The parameters are just variables that can be used in the body of the function.
If you come from other programming languages, you will be disoriented because
we do not specify types. This is seen as a feature (you can write the same
function for an integer argument and for a floating point argument), but
can be considered a detriment (you cannot find errors where the programmer
calls a function with the wrong argument).