Functions

A named recipe

A function is a named chunk of code you can run whenever you want. You give it inputs, it hands back an output. Define once, use everywhere β€” the opposite of copy-paste.

The anatomy, left to right:

  • func β€” the keyword that starts every function definition.
  • the name β€” camelCase, usually a verb-ish phrase: walkPrice.
  • parameters in parentheses β€” the inputs, each written name: Type, separated by commas.
  • -> and a return type β€” the arrow announces what type the function hands back.
  • the body in { } β€” the code that runs. return followed by a value ends the function and hands that value back.

Calling a function means writing its name with real values in the parentheses. The call is an expression β€” it becomes the returned value, so you can assign it straight to a let.