A function is a group of statements that together do a particular task. Functions help to organize, reuse, and clarify our code.
Syntax of a function
def double(number):
doubled = number * 2
return doubled
the def keyword marks the beginning of a function definition
double is the name of the function, which must be unique within the program
number is the function’s parameter; functions can have any number of parameters (including none)
the indented statements are the body of the function
Calling a function
After you define a function, you can use it by calling it, which requires the function name with the correct number of parameters.
x = 10
y = double(x)
Return statement
If a return statement is reached when evaluating a function, then the function ends. Optionally, the return statement can specify an expression.
In the example above, return doubled is specifying the variable doubled as its return expression. When the double function is called, it will evaluate to that return expression.
If there’s no return expression, then the function call will return None.