Beginner
Methods are reusable subprograms. A method has a name, a fixed number of inputs, and a fixed number of outputs. You define a method once and call it from anywhere — including from inside itself (recursion).
Every method is a dataflow graph with two special nodes:
A method-call node invokes another method. It looks like a regular node but in a purple color. Its name is the method being called. The node’s inputs map to the called method’s input bar, and its outputs come from the called method’s output bar.
Load the Fibonacci example (Cmd+Shift+E > Math).
This project has two methods: fib and main.
main calls fib(10):
fib has two cases (more on cases in Lesson 5).
The key idea: fib calls itself twice (fib(n-1) and
fib(n-2)) and adds the results. This is recursion.
When you define a method, you specify how many inputs and outputs it has. The input bar gets that many output pins, and the output bar gets that many input pins. Every call site must match this arity.
Examples:
Method "square": 1 input, 1 output (x → x*x) Method "add": 2 inputs, 1 output (a, b → a+b) Method "swap": 2 inputs, 2 outputs (a, b → b, a) Method "greet": 0 inputs, 0 outputs (side effect only)
square with 1 input and 1 output.square: wire the input bar’s output to both inputs of a * node (fan-out). Wire the product to the output bar.main method (0 inputs, 1 output).main: add constant 7, wire it to a square method-call node, wire the result to the output bar.49.double (1 input, 1 output) that multiplies its input by 2.main: call double, then pass the result to square.3. The chain is: 3 → double → 6 → square → 36.36.divmod (2 inputs, 2 outputs) that returns
the quotient and remainder of a / b./ node for the quotient,
and to a mod node for the remainder.divmod(17, 5) from main.log. You should see 3 and 2.