7. Functions
The loops of the last chapter removed repeated actions; this chapter removes repeated stretches of code. Pack a piece of work into a named bundle, and afterwards the name alone does the job — that is a function.
In this chapter you will learn:
- to define and call functions;
- parameters, arguments, results and
-> Unit; - early
return, and why every path must return; - scope: which names are visible where;
- the order of top-level code;
- why
mainis not special; - recursion and call traces;
- the naming conventions.
7.1 Defining and calling
func double(n: Int) -> Int:
return n * 2
print(double(4))
print(double(10))8
20The definition first:
func double(n: Int) -> Int:defines a function;funcis the keyword anddoubleis the name.- The
n: Intin the parentheses is a parameter: a name and a type, through which the function receives the value passed in from outside. -> Intis the result type: the function hands back anInt.- In the indented body,
return n * 2computesn * 2, hands it back to the place that called, and ends the function there.
Now the call:
double(4)is a call: write the name and put the argument 4 in the parentheses. The program jumps into the body withnequal to 4, computes 8 and hands it back.double(4)is itself anIntexpression, so it can go intoprint(...)or intolet x = ....- The same function can be called any number of times, each from the top; in
double(10)thenis 10 and the result is 20.
With a function, the "copy the same if three times" code of the last chapter writes the branches once and calls them with different arguments.
7.2 The signature must be complete
Every type in the function's signature (its parameters and result) is required. Local variables may be inferred, as in let x = 3, but the signature is the contract the caller reads, so it is written out.
Deliberate mistake: a missing result type
func double(n: Int):
return n * 2
print(double(4))SPR-SYNTAX-ERROR [SYNTAX] main.spr:1:20: 'func double(n: Int)' has no result type
hint: Write 'func double(n: Int) -> Unit:' when it returns nothing, or put the result type after '->', for example 'func double(n: Int) -> Int:'.1:20 points at the colon at the end of the line: the function header stopped there, with no -> or result type. The hint offers the two possibilities: a result means -> Int, no result means -> Unit (the next section). A missing parameter type will not compile either; add it at the definition.
7.3 Returning nothing: Unit
Not every function hands a value back. A function that just does something, such as printing, writes -> Unit:
func greet(name: String) -> Unit:
print("Hello, " + name + "!")
greet("Ada")
greet("Grace")Hello, Ada!
Hello, Grace!Unit means "no value": the function finishes its work and ends without a result. A call to it is a statement by itself; there is nothing to use.
Deliberate mistake: storing a Unit result
With no value, there is nothing to bind a name to:
func greet(name: String) -> Unit:
print("Hello, " + name + "!")
let message = greet("Ada")SPR-TYPE-UNIT [TYPE] main.spr:4:1: Cannot bind a Unit result to a variableThe compiler stops you here because "save what the printing function returned" is almost always a mistake. To make a function hand something back, change its result type to a real type and put the value after return.
7.4 Early return
The moment return runs, the function ends and no later line runs. That makes it the tool for "leave as soon as something is wrong", which reads much better than a stack of nested ifs:
func label(age: Int) -> String:
if age < 0:
return "invalid"
if age < 18:
return "minor"
return "adult"
func show(n: Int) -> Unit:
if n < 0:
print("negative, skipping")
return
print("value " + n)
print(label(-1))
print(label(10))
print(label(30))
show(-3)
show(5)invalid
minor
adult
negative, skipping
value 5label(-1): the firstifholds,return "invalid"ends the function at once and the two laterifs are never checked.label(10)reaches the secondreturn, andlabel(30)fails bothifs and falls to the finalreturn "adult".show(-3):n < 0holds, so it printsnegative, skippingand then a valuelessreturnends the function.show(5): the condition fails and it printsvalue 5. A function returningUnitneeds noreturnat the end; to leave early, a barereturndoes it.
Note print("value " + n): when one side of + is a String, the Int is turned into text and joined on.
7.5 Deliberate mistake: a path that returns nothing
When a function's result type is not Unit, every path of execution must end in return:
func sign(n: Int) -> String:
if n > 0:
return "positive"
elif n < 0:
return "negative"
print(sign(1))SPR-FLOW-MISSING-RETURN [FLOW] main.spr:1:1: Function 'sign' must return String on every pathn > 0 and n < 0 both return, but when n == 0 neither branch runs and the function reaches its end with nothing to hand back. The compiler performs flow analysis (the category is FLOW) over every possible path before it finds this. The fix: add an else branch, or a final return at the end.
7.6 Scope: where names are visible
Each function is its own small world. Its parameters and the names declared in its body mean something only inside it:
Deliberate mistake: a local name that escaped
func setup() -> Unit:
let hidden = 1
print("set " + hidden)
setup()
print(hidden)SPR-NAME-UNRESOLVED [NAME] main.spr:6:7: Unresolved name 'hidden'hidden exists only inside setup. The name is gone when the function ends, so the outer print(hidden) cannot find it. The other direction works: a function body can see top-level names:
let rate = 3
func triple(n: Int) -> Int:
return n * rate
print(triple(5))15triple uses the top-level rate. A function definition only "writes the code down"; it runs when it is called, and by then rate exists.
7.7 Top-level order: functions may be called early, variables may not
A function definition does not take part in the running order. Calling a function before its definition is fine:
print(twice(3))
func twice(n: Int) -> Int:
return n * 26Top-level statements (code outside every function) run exactly once, strictly in the order they appear. Using a top-level variable before it is declared is a compile error:
print(n)
let n = 3SPR-NAME-FORWARD-REFERENCE [NAME] main.spr:1:7: Top-level 'n' is used before its declaration; top-level statements run once, in source order
hint: Move the declaration of 'n' above this statement.
Declared here at 3:1Note the last line, Declared here at 3:1: the compiler marks where n is declared for comparison.
There is a subtler case too: where the function is defined does not matter, but any top-level variable it uses at run time must already be initialized. This compiles, and fails only when value() runs:
func value() -> Int:
return n
print(value())
let n = 7SPR-RUNTIME-EXCEPTION [RUNTIME] main.spr:2:5: Top-level 'n' was used before its initializer ran
hint: Top-level statements run in source order; declare the binding above the first top-level statement that calls code using it. Run with --stacktrace to see the JVM stack.The RUNTIME in SPR-RUNTIME-EXCEPTION says it happens at run time, not at compile time. Move let n = 7 above the call.
7.8 main is not special
Some languages start a program at a main function. Sprig has no such rule: top-level statements are the program, run from top to bottom; sprig init merely writes a main function by habit and calls it:
# There is no special main: top-level statements are the entry point. A main function is fine, but you must call it yourself.
func main() -> Unit:
print("from main")
main()from mainmain here is an ordinary function name. Delete the main() call and the definition stays but nothing prints — defining is not running; sprig run will even remind you that no top-level statement calls main.
7.9 Recursion: a function that calls itself
A function body may call the function itself; that is recursion. The factorial of 4 (4×3×2×1) is the example:
func factorial(n: Int) -> Int:
if n <= 1:
return 1
return n * factorial(n - 1)
print(factorial(4))24factorial(4) wants 4 times factorial(3), which wants 3 times factorial(2), and so on down to factorial(1). The branch n <= 1 returns 1 directly and asks nothing further — that is the base case. The results then come back up:
| Step | Being computed | Result |
|---|---|---|
| 1 | factorial(4) | 4 × factorial(3), waiting |
| 2 | factorial(3) | 3 × factorial(2), waiting |
| 3 | factorial(2) | 2 × factorial(1), waiting |
| 4 | factorial(1) | base case, 1 |
| 5 | factorial(2) receives | 2 × 1 = 2 |
| 6 | factorial(3) receives | 3 × 2 = 6 |
| 7 | factorial(4) receives | 4 × 6 = 24 |
A recursive function must have a branch that stops it. Without one the calls pile up and the run ends with StackOverflowError.
7.10 How to name things
The naming rules for Sprig (the full text is in sprig help language):
- Names you declare — functions, methods, parameters, variables, fields, top-level
lets — uselower_snake_case:read_utf8,max_by. - Types — classes, contracts, enums, variants, their cases and type parameters — use
UpperCamelCase:Light,Expense. - Built-in members (
toString,divTrunc) and Java APIs keep theircamelCasenames.
The compiler does not complain about a name like myFunc, but the book and the standard library follow the convention, and following it saves trouble.
Summary
func name(parameter: Type, ...) -> ResultType:, with the body indented.- Signature types are required; write
-> Unitwhen nothing is returned. - Arguments are passed by position; a call is the name plus parentheses.
returnends the function at once; a non-Unitfunction must return on every path.- Parameters and locals live only inside the function; top-level names are visible inside it.
- Functions may be called before they are defined; top-level statements run in order, so top-level variables must be declared before use.
- There is no special
main: top-level statements are the program. - Recursion needs a base case; a variable trace table works for function calls too.
- Declared names use
lower_snake_case, types useUpperCamelCase.
Exercises
Exercise 1: a square function
Write square(n: Int) -> Int returning n * n, and print square(6).
Hint: the body is the single line return n * n.
Answer
func square(n: Int) -> Int:
return n * n
print(square(6))36Exercise 2: the largest of three
Write max3(a: Int, b: Int, c: Int) -> Int returning the largest of the three, and print max3(3, 9, 5).
Hint: keep var best starting at a, challenge it with b and then c in two ifs, and return best.
Answer
func max3(a: Int, b: Int, c: Int) -> Int:
var best = a
if b > best:
best = b
if c > best:
best = c
return best
print(max3(3, 9, 5))9Exercise 3: even or not
Write is_even(n: Int) -> Bool returning true for an even n and false otherwise; print is_even(10) and is_even(7).
Hint: return a Bool expression: n % 2 == 0.
Answer
func is_even(n: Int) -> Bool:
return n % 2 == 0
print(is_even(10))
print(is_even(7))true
falseExercise 4: a countdown function
Write countdown(start: Int) -> Unit that prints from start down to 1 and then liftoff; call countdown(3).
Hint: parameters cannot be assigned (they are like let), so start the function with var n = start and use the while from the last chapter.
Answer
func countdown(start: Int) -> Unit:
var n = start
while n > 0:
print(n)
n -= 1
print("liftoff")
countdown(3)3
2
1
liftoffExercise 5: recursive sum
Write sum_to(n: Int) -> Int recursively, returning 1+2+…+n; print sum_to(5).
Hint: the base case returns 0 when n <= 0; otherwise return n + sum_to(n - 1).
Answer
func sum_to(n: Int) -> Int:
if n <= 0:
return 0
return n + sum_to(n - 1)
print(sum_to(5))15Coming from another language?
- Python:
defbecomesfunc, and colons and indentation are the same; but the types must be written in the signature, and there are no default arguments, no*argsand no nested functions. - Java / JavaScript: no
functionkeyword; functions cannot share a name (no overloading); there is no "last line is the result", so every path needs an explicitreturn. - Parameters are immutable like
let; for a mutable copy, declare avarinside the function. - When you need a "small local function", use the lambdas of chapter 15.
Next chapter: Chapter 8: Lists.
