Codecademy Logo

Learn to Code with Blockly: Functions

Define Function Definition

A function is defined by specifying its instructions, inputs, and name.

For example, a sandwich-making function takes in two inputs representing two ingredients. The definition in psuedocode would look like:

function makeSandwich(topping1, topping2) {
  Add bread
  Add topping1
  Add topping2
  Add bread
}

The name is makeSandwich, the inputs are topping1 and topping2, and the instructions are the four lines each starting with Add.

Function Inputs Definitions

“In programming, inputs to functions are known as parameters when a function is declared or defined. Parameters are variables that can be used inside the function body. When the function is called, these parameters will have the value of whatever is passed in as arguments.

For example, a sandwich-making function has two parameters:

function makeSandwich(topping1, topping2) {
  Add bread
  Add topping1
  Add topping2
  Add bread
}

We make a ham-and-cheese sandwich with makeSandwich("ham", "cheese"). We call the function with the arguments "ham" and "cheese". Those will be the values for the topping1 and topping2 parameters.”

Learn More on Codecademy