MPDA’22
December 23, 2021
A function is a block of code which only runs when it is called.
Calling a function just means to use a function in your code.
We’ve been using funcions during the entire course:
You can pass data, known as parameters, into a function.
A function can return data as a result.
You can create a function by using the def
keyword, followed by the name and the (optional) inputs surrounded by parenthesis.
Inside the indented block you can add any code you wish.
And use it like this:
A function can optionally have input parameters (variables)
These parameters can be of any type.
A function can also return data
The return value can be saved into a variable for later use.
Functions are useful in many ways:
Essentially, a python function is the equivalent of a grasshopper node:
There’s only one main difference between them:
A python function can only have one return
value
It means that the result of any function has to be a single item (or no item at all).
If you need to output more than one item, you can output a list/tupple or dictionary that collects the multiple items you want to send.
According to wikipedia, recursive is something that involves the repeated application of a rule, definition, or procedure to successive results.
In other words:
Recursive is any operation that you repeat over and over using the result from the previous iteration.
In programming, a function is recursive if it calls itself.
Meaning, that inside the function’s code you are using the same function you are creating.