05-01: Doing Stuff: functions

Matt Price

A Function is a variable(!)

  • functions are also a box into which you put stuff
  • but when you look into the box, the "stuff" has changed
  • we use them to change the state: to do things
  • use whenever you're repeating an action or process

Defining a function


function square(number) {
  return number * number;
}

let cube = function(number) { 
      return number * number * number;
  };

console.log(square(2));
console.log(cube(3));
  • we'll use the first method:
    • keyword function followed by name then (parameters, if, you, need, them), then { , then statements, then }

Remember this


let historians= ["Edward Gibbon", "Leopold von Ranke", "Edward Said", "Joan Scott"];
let i = 0;

while (i < historians.length) {
    console.log(historians[i] + " was a historian.");
    i+=1;
  }

As a function


function makeSentences(historians) {
  let i = 0,
      output = "";

  while (i < historians.length) {
    output += historians[i] + " was a historian.\n";
    i+=1;
  }
  return output;
}

let h1 = ["Edward Gibbon", "Leopold von Ranke", "Edward Said", "Joan Scott"],
    h2 = ["Orlando Patterson", "Michel Foucault", "Natalie Zeemon Davis", "Howard Zinn"];

makeSentences(h1);
//makeSentences(h2);

Finally we can talk about scope!


let a = 'just some string',
    b = 'some other string';

function scopeExample (anyString) {
  let a = 'I set this value inside the function';

  return ('inside the function, a="' + a + '", not ' + anyString);
}

// a
// scopeExample(a);
//anyString
  • outside the function, a = "just some string"
  • inside, we redeclare it for the function scope and now a has new value
  • anyString is only declared inside the function scope

Advanced scope


let a = "global scope a";

console.log(a);

for (i=0; i<6; i++) {
  let a = "local scope a on iteration: " + (i + 1) ;
  console.log(a)
}

console.log(a);
  • we can also redeclare let variables in an if/for/while "block"
  • scope scales: global → function → block → nested block → etc etc
  • any time you see { .. } you're in a new scope context!

Function Hints for Assignments

  • remember the return values!
  • debug by setting a variable to the function output, and using quokka to look at the value:

    
      function returnArray (first, second, third) {
      // you can define the array using "new Array ()" or just "[ , , ]"
      // don't forget to return it
      // return ; // add the value here!
    }
    
    let a = returnArray (1, 3,5);
    a  // quokka will display the value
    

Function note 1: Name vs. Return Value

A Function is a variable Robot


function robotCleaner () {
  let output =  "I cleaned your room";
  return output;
}

let r = robotCleaner();
r
  • build a function to do something
  • name it whatever you want
  • the return value is what the function "give you back*
  • call it whenever you need it done

Name and Return Value are independent


function robotCleaner () {
  let output =  `Ha, ha! I have replaced your robot cleaner! 
Now your room is even messier! Bwa ha ha ha ha!`
  return output;
}

let r = robotCleaner();
r
  • To pass the assignment, always modify the function
  • don't get confused and try to somehow modify the function call
  • no intrinsic relationship between name and return value

Functions note 2: Parameter and Output

Your Return Value should Make use of the Parameter


function greatWriter (name) {
let output = "Margaret Atwood was a great writer."
return output
}

console.log(greatWriter("Margaret Atwood"))
// console.log(greatWriter("Toni Morrison"))

Solution: don't use a STATICALLY DEFINED STRING as your output


function evenGreaterWriter (name) {
let output = name + " was a great writer."
return output
}

console.log(evenGreaterWriter("Margaret Atwood"))
// console.log(evenGreaterWriter("Toni Morrison"))

Parts of things: elements, methods, properties


let a=["name", 0, "otherinfo"];
console.log(a[2]);
console.log(a.length);
console.log(a[length]);
a.pop;
  • note strange syntax:
    • a[1]: second element of array
    • a.length: a special way of finding the length of any array. Length is a property of a.
  • console.log(a)): log is a part of console. it takes a parameter , in this case a
  • not that a.pop is not the same as a.pop()