One of the most important in mathematics concept is to use functions. The executing function produces one or more results, dependent on the parameters passed to it and provides a re-usable piece of code, still somehow flexible, depending on the parameters.
In general, a function is a structuring element in the programming language which groups a set of statements so they can be called more than once in a program. Programming without functions will need to reuse code by copying it and changing its different context. Using functions enhances the comprehensibility and quality of the program. It also lowers the software's memory usage, development cost and maintenance.
Different naming is used for programming language functions, e.g., subroutines, procedures or methods.
Python language defines function by a def
statement. The function syntax looks as follows:
def function-name(Parameter list): statements, e.g. the function body
Function bodies can contain one or more return statements. It can be situated anywhere in the function body. A return statement ends the function execution and returns the result, e.g. to the caller. If the return statement does not contain an expression, the value None
is returned.
def Fahrenheit(T_in_celsius): """ returns the temperature in degrees Fahrenheit """ return (T_in_celsius * 9 / 5) + 32 for t in (22.6, 25.8, 27.3, 29.8): print(t, ": ", fahrenheit(t))
Output:
>>> 22.6 : 72.68 25.8 : 78.44 27.3 : 81.14 29.8 : 85.64 >>>
Optional Parameters
Functions can be called with optional parameters, also named default parameters. If the function is called without parameters, the default values are used. The following code greets a person. If no person's name is defined, it greets everybody:
def Hello(name="everybody"): """ Say hello to the person """ print("Hello " + name + "!") Hello("George") Hello()
Output:
>>> Hello George! Hello everybody! >>>
Docstrings
The string is usually the first statement in the function body, which can be accessed with function_name.doc.
This is a Docstring statement.
def Hello(name="everybody"): """ Say hello """ print("Hello " + name + "!") print("The docstring of the function Hello: " + Hello.__doc__)
Output:
>>> The function Hello docstring: Say hello >>>
Keyword Parameters
The alternative way to make function calls is to use keyword parameters. The function definition remains unchanged.
def sumsub(a, b, c=0, d=0): return a - b + c - d print(sumsub(12,4)) print(sumsub(42,15,d=10))
Only keyword parameters are valid, which are not used as positional arguments. If keyword parameters don't exist, the next call to the function will need all four arguments, even if the c
needs just the default value:
print(sumsub(42,15,0,10))
Return Values
In the above examples, the return statement exists in sumsub
but not in the Hello
function. The return statement is not mandatory. If an explicit return statement doesn't exist in the sample code, it will not show any result:
def no_return(x,y): c = x + y res = no_return(4,5) print(res)
Any result will not be displayed in:
>>>
Executing this script, the None
will be printed. If a function doesn't contain an expression, the None
will also be returned:
def empty_return(x,y): c = x + y return res = empty_return(4,5) print(res)
Otherwise, the expression value following return will be returned. In this example, 11 will be printed:
def return_sum(x,y): c = x + y return c res = return_sum(6,5) print(res)
Output:
>>> 9 >>>
Multiple Values Returning
Any function can return only one object. An object can be a numerical value – integer, float, list or a dictionary. To return, e.g. three integer values, one can return a list or a tuple with these three integer values. It means that the function can indirectly return multiple values. The following example calculates the Fibonacci boundary for a positive number returns a 2-tuple. The Largest Fibonacci Number smaller than x
is the first, and the Smallest Fibonacci Number larger than x
is next. The return value is stored via unpacking into the variables lub and sup:
def fib_intervall(x): """ returns the largest Fibonacci number, smaller than x and the lowest Fibonacci number, higher than x""" if x < 0: return -1 (old, new, lub) = (0,1,0) while True: if new < x: lub = new (old,new) = (new,old+new) else: return (lub, new) while True: x = int(input("Your number: ")) if x <= 0: break (lub, sup) = fib_intervall(x) print("Largest Fibonacci Number < than x: " + str(lub)) print("Smallest Fibonacci Number > than x: " + str(sup))