Call a function

Category:Functions

Call a function

Category: Functions

Calls a user defined function that takes no parameters, and optionally generates a return value.

When you define a function you give a name to a set of actions you want the computer to perform. When you call a function you are telling the computer to run (or execute) that set of actions.

A function definition can be provided anywhere in your code - in some ways the function definition lives independently of the code around it. It actually doesn't matter where you put a function definition. And you can call it from anywhere, either before or after the function definition. We will follow the convention of always putting function definitions at the bottom of our program, and the code for calling functions at the top of our program.

Examples


// Call function to draw a long line.
longLine();

function longLine(){ // Define a function to draw a long line.
  penDown();
  moveForward();
  moveForward();
  moveForward(); 
}

Example: Big Box Call functions to draw a big box.

// Call functions to draw a big box.
longLine();
turnLeft();
longLine();
turnLeft();
longLine();
turnLeft();
longLine();
turnLeft();

function longLine(){ // Define a function to draw a long line.
  penDown();
  moveForward();
  moveForward();
  moveForward(); 
}

Example: Big Box (Improved) Call functions to draw a big box. This is improved because it abstracts the box as two "left angles" and creates a new function that reduces repeated statements.

// Call functions to draw a big box.
leftAngle();
leftAngle();

function leftAngle(){ // Define a function to draw a left angle with long lines.
  longLine();
  turnLeft();
  longLine();
  turnLeft();
}

function longLine(){ // Define a function to draw a long line.
  penDown();
  moveForward();
  moveForward();
  moveForward(); 
}

Example: Roll Two Die Call functions that use randomNumber(1,6) to simulate rolling two die and summing the results.

// Call functions to generate two die rolls and sum the result. Display the value on the console.
console.log(rollDie() + rollDie());

function rollDie() { 
// Define a function that uses randomNumber(1,6) to randomly generate a die roll, 1 to 6, and return the value.
  var roll = randomNumber(1,6);
  return roll;
}

Syntax

myFunction();  // No value returned.  
// OR
var returnValue = myFunction();  // Value returned and saved.

Parameters

Some functions do not any parameters.

Returns

If the function returns a value, you must assign the returned value to a variable or use the value as a parameter in another function call.

Tips

  • The purpose of a function is to help you organize your code and to avoid writing the same code twice.
  • You can call a function within another function.
  • A function that does not explicitly return a value returns the JavaScript value undefined.

Found a bug in the documentation? Let us know at documentation@code.org

Found a bug in the documentation? Let us know at documentation@code.org