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,



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:

As you can see we can actually call the same function twice but with different parameters.


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:


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.

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)