Functions

A function is a named sequence of instructions, packaged as a unit, that performs a specific task.

function makeHamburger() {
  Add bread
  Add burger patty
  Add pickles
  Add bread
}

Functions for Flexibility

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

We’ve renamed the function makeSandwich() and given it two inputs, or parameters. Each time we call the function, we’ll give actual values for each input, called arguments.

For example, 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.

// Define the 'makeSandwich()' function
function makeSandwich(topping1) {
  sandwich = 'bread' + ','
  sandwich += topping1 + ','
  sandwich += 'bread'
  return sandwich
}

// Call the function and store the returned value in 'result'
result = makeSandwich('jelly', 'peanut butter')
// Define the 'makeSandwich()' function
function makeSandwich(topping1, topping2) {
  sandwich = 'bread' + ','
  sandwich += topping1 + ','
  sandwich += topping2 + ','
  sandwich += 'bread'
  return sandwich
}

// Call the function and store the returned value in 'result'
result = makeSandwich('burger patty', 'pickles')
  • function is a sequence of instructions that performs a specific task, packaged as a unit.
  • When we define a function, we specify the instructions, inputs, and name of the function.
  • When we call a function, all of its instructions are executed.
  • Functions can be executed many times, making its instructions reusable.
  • Functions can have parameters, which accept input values, making its instructions flexible.
  • Functions organize a program into distinct units, making interchanging and editing them easier. This makes your entire program organized and modular.
  • Parameters and arguments are both inputs to a function. Parameters refer abstractly to a function’s inputs and arguments are the actual values inputted.

Control Flow

  • Web surfing: Open the browser, load the text, load the images.
  • Social media: If the user’s profile is private, hide their posts. Otherwise, make them visible.
  • Electronic music: Repeat a note four times a measure.
  • Shopping: Collect shipping and contact information. Reject a purchase if either is incorrect.

Each of these programs is a set of instructions, executed in a certain order. This order, called control flow, is just as important as the instructions themselves.

Each of these programs is a set of instructions, executed in a certain order. This order, called control flow, is just as important as the instructions themselves.

Conditional

If the user’s profile is private, hide their posts. Otherwise, make them visible.

“if X, then do instruction set 1. Otherwise do instruction set 2.” This structure is called a conditional control structure because the computer’s instructions depend on some condition(s).

Loop

A loop is a sequence of instructions which is specified once but which may be carried out several times in a row. 

Loop structures can also specify that the instructions are repeated until a certain condition is met. For example, “Repeat a sound until you are booed off stage”.

Exception

Collect shipping and email address. Reject a purchase if either is incorrect.

In exception handling, two sets of instructions are defined. The first set is executed, and if an exception occurs, the first set is stopped and the second set of instructions is executed.

In this example,

  • The first set is “collect shipping address”, “collect email address”
  • The second set is “reject the purchase”

If any errors occur in executing the first set—maybe the address is incorrectly formatted, or fake—it is stopped and the second set is executed.

These statements are useful if you know an exception may occur in a set of instructions but you don’t know in which one, or you expect the same exception might occur in multiple, sequential instructions.

if (condition) {
  DO SOMETHING
} else {
  DO SOMETHING DIFFERENT
}

If the condition evaluates to true, the computer will execute the body of that statement. In this case, it’s DO SOMETHING.

If the condition is false, the computer will execute the body of the else statement. In this case, it’s DO SOMETHING DIFFERENT.

Let’s adapt this general template to our example:

if (mode === 'public') {
  // Call a function here to show the post details

} else {
  // Call a function here to hide the post details

}
if (mode === 'public') {
  showDetails();

} else {
  hideDetails();

}
  • Control flow is the order in which instructions are executed. It is the order or execution in a program.
  • Control structures alter control flow in a program. You learned three structures, each suited for a different scenario:Conditional: “if some condition is met, then do X. Otherwise, do Y”.
  • Loop: “do something Z number of times” or “do something repeatedly until some condition is met”.
  • Exception: “do steps A, B, C. If an error occurs, stop, and do steps J, K, L”.
This entry was posted in Study Notes and tagged . Bookmark the permalink.

Leave a Reply