PythonII - Function
A function is a section of code that runs whenever it’s called. It can return a value as a result or not.
🦗⬇️⬇️⬇️ Download the gh script start file here ⬇️⬇️⬇️🦗 –> 🦗⬇️⬇️⬇️ Download the gh script end file here ⬇️⬇️⬇️🦗
Creating a function
Ok, let’s consider the following code:
a = 1
b = 2
c = a + b
print(c)
It is a program that does two things: first a sum and then it prints the result. This program can be containarized into a function like so:
def sum_and_print(param1, param2):
result_sum = param1 + param2
print(result_sum)
return result_sum
Where,
def
: it’s the keyword we use to indicate a function. It stands for definition, just another word for function.sum
: it’s the name of your function. In python you should always use the style snakecase to name functions, e.g.this_is_my_function_name()
.(param1, param2)
: these are called parameters. These are the names of the variables you will feed and use into your function.:
: the column points will signify the start of the function’s implementationreturn
: this keyword return values out of the function. A function can work also without it.- an indent: beware that all the function’s body has to be indented. Otherwise your python interpreter will throw an error.
GHPython function
Remember that a function needs to be called to have an effect. If you consider this code and you click on Test
, nothing will happen.
While here the function is called (and so the code runs):
Why we need functions?
We might ask ourself: why do we need functions?
Functions are useful for two major things in programming:
- (i) ♻️ Functions are a good tool to avoid repetitions: functions are a nice tool to store a code that you would need to use multiple times. This is fundamental since every time your rewrite the same code, you double the chances of creating a bug or an error. If well thought and written, functions can avoid that. Consider the following scenario:
As you can see we can actually call the same function twice but with different parameters.
- (ii) 📦 Functions allow to organize your code: Consider the following code:
# my script ## A) my first part of code: it does something and produce result a ### line of code 1 ### line of code 2 ### line of code 3 ### ... ## B) my second part of code: it does something and produce result b ### line of code 16 ### line of code 17 ### line of code 18 ### ... ## C) my second part of code: it does something and produce result c ### line of code 36 ### line of code 37 ### line of code 38 ### ...
By using functions it can be reworked to be much more organized and readable like so:
def funcA():
### line of code 1
### line of code 2
### line of code 3
### ...
def funcB():
### line of code 16
### line of code 17
### line of code 18
### ...
def funcC():
### line of code 36
### line of code 37
### line of code 38
### ...
def funcABC():
funcA()
funcB()
funcC()
funcABC() # <-- this is the called function
Good practices in functions
In order to become a better computational design programmer it exists a set of good practices that you can follow to improve the robustness and readability of your code:
- (a) be tight: keep your function small. If it’s getting to big, split it in multiple functions.
- (b) use meaningfull names: instead of write long comments of what a function does, take a moment to give a meaningfull and informative name to your function. For example:
def p_0_or_1(bool p)
pass
This is not a good name for the function since it does not inform us about its content, it does not clearly describe what the function does or what it is used for. A good function name should be descriptive, easy to understand, and should clearly convey the purpose of the function.
def print_on_intersection(bool isIntersected)
pass
Instead, this function will give us some hints about its content.
- (b) one function should do one thing only (Single Responsibility Principle or SRP): do not hide actions or code that modify or produce effects in addition to what announced in the function name. Let always one function do one thing. Considering the following:
def process_geometries():
data = get_geometry()
data = clean_geometry(data)
data = analyze_sanity(data)
save_geometry(data)
process_geometries(data)
This function is called process_geometries()
but in reality it does more than that. It process the geometry and save it. These are two different tasks not stated in the function’s name. In this case it is raccomended to break it down into single-purpose functions like:
def get_geometry():
# code to get the geometry
# ..
return data
def clean_geometry(data):
# code to clean the geometry
# ..
return data
def analyze_geometry(data):
# code to analyze geometry
# ..
return data
def process_geometry():
data = get_geometry()
data = clean_geometry(data)
data = analyze_sanity(data)
return data
def save_geometries(data):
# code to save geometry
data = process_geometries()
save_geometries(data)