After we learn the most basic and essential data types and structures commonly used in Python, we want to look at the so-called user-defined function (UDF). The usage and the general concept of user-defined functions are identical to the built-in functions of Python that have been introduced before. UDFs allow you to externalise and encapsulate a logical sequence of code statements that marks a specific task repeatedly used under similar conditions. For instance, that could be an often-used calculation or a code fragment used multiple times within your script in more or less the same way.
UDFs are defined as their own self-contained and executable block. Well-programmed UDFs are flexible to varying executing condition over preconfigured parametrisations within the functiondeclaration itself. Thereover UDFs allow you to:
Note: functions defined within an object's scope are called methods. Methods differ distinctively in their concept and usage from UDFs since user-defined functions are universally callable, while methods can only be called under the specific object context!
In Python, a user-defined function is declared by the keyword def
followed by the <function_name>
. A complete and empty UDF body looks like this:
def function_name():
return
Every UDF in Python consists of 3 main components:
def
and followed by the individual name of the function.()
brackets after the function declaration. In the above example, the function is non-parameterized. Hence the brackets are empty. In the following also, examples of parameterized functions are shown.:
. The function body must at least contain the return
statement. Every function in Python needs to return a value. After the function is defined, it can be used anywhere within the same programming context by just typing/calling the function's name.
As a first and simple example for UDFs, we want to declare and develop a function that calculates the value of a cube step by step. Initially, the function should be pretty simple and return the volume of a cube with a fixed edge length. Since this provides no flexibility, we want to adapt and parametrise the function afterwards so that the volume of any given edge length is calculated and returned.
But one after the other. Generally, the volume $V$ of a cube with the edge length $a$ is calculated by:
$$V = a^3$$Now we want to implement the above equation within a Python UDF. As it should be as simple as possible at first, the function should return the volume of the cube with a fixed edge length of 3
:
def cubes_volume():
return (3**3)
After successfully declaring and implementing the function, we can directly use it by typing the name of the function followed by ()
. The function should return the value $27$ since $3^3 = 27$:
cubes_volume()
27
Awesome, that has worked quite well, and you have defined your first UDF! Now we want to adapt our function so that it calculates the cube's volume to any given edge length. For those purposes, we make use of the concept of parametrisation. Hence we add a parameter a
within the ()
brackets of the functions declaration. Within the function's body, we use this parameter for the volume calculation:
def cubes_volume(a):
return (a**3)
After we re-declared the function cubes_volume()
, we can check it out. Just try a few different edge lengths on your own. The following are some examples:
cubes_volume(3)
27
cubes_volume(100)
1000000
cubes_volume(5)
125
Great! As an additional add-on, we can assign a so-called default value to the function's parameter a
. This ensures that the function is still operable even though no value for a
is passed:
def cubes_volume(a=3):
return (a**3)
cubes_volume()
27
cubes_volume(5)
125
$$K = (°F - 32.0) \times \frac{5}{9} + 273.15$$Exercise: Write a function that computes Kelvin from Fahrenheit! The function shall be named as
fahrenheit_to_kelvin
and shall also be parameterised. Kelvin is converted to Fahrenheit by:
### your solution
def fahrenheit_to_kelvin(F):
"""
Function to compute Fahrenheit from Kelvin
"""
K = (F - 32.0) * 5/9 + 273.15
return K
Test your function:
$-50\ F$ should yield approximately $228\ K$.
fahrenheit_to_kelvin(-50)
227.59444444444443
$$°C = K - 273.15$$Exercise: Write a function that computes Celsius from Kelvin! The function shall be named as
kelvin_to_celsius
. This function should be parameterised too. Celsuis is converted to Kelvin by:
### your solution
def kelvin_to_celsius(K):
'''
Function to compute Celsius from Kelvin
'''
C = K - 273.15
return C
Test your function:
$0\ K$ should yield approximately $-273.15 °C$.
kelvin_to_celsius(0)
-273.15
Exercise: Write a function that computes Celsius form Fahrenheit! The function shall be named as
fahrenheit_to_celsius
. This function should be parameterised too. Reuse your above declared functions for this calculation:
### your solution
def fahrenheit_to_celsius(F):
'''
Function to compute Celsius from Fahrenheit
'''
K = fahrenheit_to_kelvin(F)
C = kelvin_to_celsius(K)
return C
Test your function:
$451\ F$ should yield approximately $232\ °C$.
fahrenheit_to_celsius(451)
232.77777777777777
Citation
The E-Learning project SOGA-Py was developed at the Department of Earth Sciences by Annette Rudolph, Joachim Krois and Kai Hartmann. You can reach us via mail by soga[at]zedat.fu-berlin.de.
Please cite as follow: Rudolph, A., Krois, J., Hartmann, K. (2023): Statistics and Geodata Analysis using Python (SOGA-Py). Department of Earth Sciences, Freie Universitaet Berlin.