Functions are central in Scheme. We’ll learn about functions with a few different examples.
To run this Scheme program, save it to a file (e.g., functions.scm) and use your Scheme interpreter. For example, if you’re using Guile:
In Scheme:
Functions are defined using the define keyword.
Function parameters are listed in parentheses after the function name.
The function body is the expression(s) following the parameter list.
Scheme automatically returns the value of the last expression in a function, so explicit return statements are not needed.
Function calls are written in prefix notation: (function-name arg1 arg2 ...).
Scheme doesn’t have a separate main function. Instead, code at the top level is executed when the file is run.
Scheme’s functional nature allows for concise and expressive function definitions and calls. The language’s simplicity is evident in how easily we can define and use functions with varying numbers of parameters.
There are several other features to Scheme functions. One is the ability to create higher-order functions, which we’ll look at in future examples.