A function (also called a routine) is reusable code that provides instructions for performing an action, or creating an output based on input.
To define a function, you must provide three key parts: a unique name, the type of information to expect as its result, and what the function will do when called.
The following is the basic syntax for a function:
name() : type = codeblockThe name() and type separated by a colon: This is the function signature, which is how you call and use the function, and the value that must be returned by the function is of the type you provide. This format is similar to how you create constants, except for the () after the name, which mimics how you call the function in your code.
The function code block: You define what the function will do when it's called by providing
=codeblock, wherecodeblockis any sequence of one or more expressions. Whenever you call the function, the expressions in the code block are executed.