Skip to content

Functions

Functions: Reusable Code Blocks

A function is a block of code which only runs when it is called. You can pass data, known as parameters, into a function. A function can return data as a result.

Why use Functions?

  1. Reusability: Define code once, and use it many times.
  2. Organization: Break down large programs into smaller, manageable pieces.
  3. Abstraction: You don't need to know how the function works, just what it does (like a remote control).

Components of a Function

  • Definition: Creating the function.
  • Arguments/Parameters: The information you send into the function.
  • Body: The code that performs the task.
  • Return Value: The result the function sends back.

Logic First

Think of a function like a recipe. The recipe is the definition; the ingredients are the parameters; the cooking is the body; and the finished dish is the return value.