LEARN VOID / 04
Functions
Name an idea. Use it again.
Complete program for the synchronous entry adapter. The result below is the returned integer.
func subtotal(unit_price: i32, quantity: i32) -> i32:
return unit_price * quantity
func entry(quantity: i32) -> i32:
return subtotal(12, quantity)Name a reusable calculation
subtotal takes an integer unit price and quantity, then returns their product. The entry function calls it positionally: five items at twelve each return 60. Parameters and result types make the boundary explicit.
Give recursion a stopping point
A recursive function calls itself with a smaller problem. The separate function fragment below computes factorial for non-negative integers. Its base case is 0! = 1; input 5 gives 120. Add it to a source file and call factorial(5) from a function. It is not a standalone CLI program.
func factorial(number: i32) -> i32:
if number == 0:
return 1
else:
return factorial(number - 1) * numberKeep the domain clear
Use small non-negative inputs for this factorial fragment. Values through 12! fit in i32; larger products wrap. Negative inputs do not reach this fragment’s base case.