Want to make creations as awesome as this one?

Transcript

Following def is the function name. The rules for function names are the same as for variable names

Following def is the function name. The rules for function names are the same as for variable names

The main body of the function can contain any Python code, including aithmetic operations and the creation of new variables. Commonly, it will use the parameters of the function to perform some kind of calculation.

The word def tells Python we’re about to start defining a function

Following the function name is a set of parentheses. These may contain a series of one or more variable names. These are the parameters of the function and will receive values each time the function is called. Parameters in the list are separated by commas. A function may have no parameters and, in this case, the set of parentheses would be empty.

The colon at the end of the line tells Python we’re ready to start defining the code that will be run when the function is called. This code is indented to indicate which code is part of the function and which code follows it

If the execution of the code reaches the word return, if there is an expression which follows it, it will be evaluated and its value will be returned. If there is no expression after it, the special value None will be returned to indicate the lack of a value.

Lines of code after a return statement will never be executed as the return will cause the function to finish executing. As a result, you would not normally include code after a return statement.

The first piece of code which is not indented marks the end of the function. This unindented code is not part of the function and will not be executed when the function is called. If the function is finished by the end of the indented block without a return statement being reached, the function will return None.