--Originally published at 101 For Tec
Functions
Syntax
The concept of a function is one of the most important ones in mathematics. A common usage of functions in computer languages is to implement mathematical functions. Such a function is computing one or more results, which are entirely determined by the parameters passed to it.
In the most general sense, a function is a structuring element in programming languages to group a set of statements so they can be utilized more than once in a program. The only way to accomplish this without functions would be to reuse code by copying it and adapt it to its different context. Using functions usually enhances the comprehensibility and quality of the program. It also lowers the cost for development and maintenance of the software.
Functions are known under various names in programming languages, e.g. as subroutines, routines, procedures, methods, or subprograms.
A function in Python is defined by a def statement. The general syntax looks like this:
def function-name(Parameter list):
statements, i.e. the function body
The parameter list consists of none or more parameters. Parameters are called arguments, if the function is called. The function body consists of indented statements. The function body gets executed every time the function is called.
Parameter can be mandatory or optional. The optional parameters (zero or more) must follow the mandatory parameters.
Function bodies can contain one or more return statement. They can be situated anywhere in the function body. A return statement ends the execution of the function call and “returns” the result, i.e. the value of the expression following the return keyword, to the caller. If the return statement is without an expression, the special value None is returned. If there is no return statement in the function code, the function ends, when the control flow reaches the
Continue reading "This doesn´t function :(" →