Codepath

Functions

What is a Function

A function is a group of lines of code that completes a task. Programmers use functions all the time because they make it easier and faster to code and also makes code more readable to other users. Programmers use functions to define procedures in the code (i.e measure distance, print to the console, find a value, etc.).

A function's purpose is to execute a set of commands (or lines of code) to complete some procedure. In many cases, we can think of a function takes in some input or information and does something and finally send a response or output.

Image depiction of a function taking in input and returning output Source: Wikipedia


Defining a Function

Let's quickly review the syntax of defining a function.

function functionName(param1, param2){
  //lines of code
} 
  • function: This keyword is used to define a function in JavaScript.
  • functionName: This is the name of the function. You should check that the name of the function does not overlap with any variable names to avoid confusion.
  • param1, param2: Functions may take in parameters. If a function takes in multiple parameters, they must be separated by commas.
  • {}: Curly brackets are used to define the block of code associated with a function.
Fork me on GitHub