Skip to content

Instantly share code, notes, and snippets.

@cbrandolino
Last active August 29, 2015 14:18
Show Gist options
  • Select an option

  • Save cbrandolino/3c0892f809525d58a8c3 to your computer and use it in GitHub Desktop.

Select an option

Save cbrandolino/3c0892f809525d58a8c3 to your computer and use it in GitHub Desktop.

Revisions

  1. cbrandolino revised this gist Apr 9, 2015. 1 changed file with 0 additions and 6 deletions.
    6 changes: 0 additions & 6 deletions gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,3 @@
    The two Roberts' answers were definitely to the point.

    I am about to start teaching some people who never programmed before and this looks like a question I might encounter, though - so I'll try an extended, non-programmer-oriented answer. Sorry if it's too long-winded and slow.

    A decently formatted version, with syntax highlighting, is here: https://gist.github.com/cbrandolino/3c0892f809525d58a8c3

    A function is a machine. In most of your future work, you'll be working with functions you didn't write and probably won't have time to read. You'll put something in it; it will you something else back. What happens inside the machine should not be of your concern, as long as you get back what you want.

    Let's pretend we have a function that does long drinks, defined as follows:
  2. cbrandolino revised this gist Apr 9, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@ The two Roberts' answers were definitely to the point.

    I am about to start teaching some people who never programmed before and this looks like a question I might encounter, though - so I'll try an extended, non-programmer-oriented answer. Sorry if it's too long-winded and slow.

    A decently formatted version is here:
    A decently formatted version, with syntax highlighting, is here: https://gist.github.com/cbrandolino/3c0892f809525d58a8c3

    A function is a machine. In most of your future work, you'll be working with functions you didn't write and probably won't have time to read. You'll put something in it; it will you something else back. What happens inside the machine should not be of your concern, as long as you get back what you want.

  3. cbrandolino revised this gist Apr 9, 2015. 1 changed file with 6 additions and 4 deletions.
    10 changes: 6 additions & 4 deletions gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -38,7 +38,7 @@ This makes total sense: the fact that I give the machine `"vodka"` as a liquour
    ```javascript
    // Part one
    var liqour = 'gin';
    console.log('Outside the function! console.log(liquour) will output "gin");
    console.log('Outside the function! console.log(liquour) will output "gin"');
    console.log(liquour);
    // Part two
    @@ -87,9 +87,11 @@ Nobody assigned `width` or `height`! This is just the description of a process.

    A javascript rectangle area calculator could be:

    function rectangleArea(width, height) {
    return width * height;
    }
    ```javascript
    function rectangleArea(width, height) {
    return width * height;
    }
    ```

    ... and in fact, if we tried to call it:

  4. cbrandolino created this gist Apr 9, 2015.
    98 changes: 98 additions & 0 deletions gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,98 @@
    The two Roberts' answers were definitely to the point.

    I am about to start teaching some people who never programmed before and this looks like a question I might encounter, though - so I'll try an extended, non-programmer-oriented answer. Sorry if it's too long-winded and slow.

    A decently formatted version is here:

    A function is a machine. In most of your future work, you'll be working with functions you didn't write and probably won't have time to read. You'll put something in it; it will you something else back. What happens inside the machine should not be of your concern, as long as you get back what you want.

    Let's pretend we have a function that does long drinks, defined as follows:

    ```javascript
    function makeLongDrink(liquour, mixer, garnish) {
    var result = "here's a glass of " + liquour;
    result += " and " + mixer;
    result += " garnished with a slice of " + garnish;
    return glass;
    }
    ```

    ... make for quite a sloppy bartender, but let's forgive it for now.

    The function just defines the process. It is a machine, and it's not activated yet. `liquour`, `mixer`and `garnish`- the function *parameters* - are just placeholders which will be used inside the machine.

    To get a Moscow Mule you would call it like this:

    ```javascript
    var drink = makeLongDrink('vodka', 'ginger beer', 'cucumber');
    ````

    This way you tell the machine to take `'vodka'` as a liquour, `'ginger beer'` as a mixer, `"cucumber"` as garnish. The machine won't need to know the specific of these ingredients; it will always apply the same process.
    In this case you'll have the string `"here's a glass of vodka and ginger beer garnished with a slice of cucumber"` as a result - the `return` value, now assigned to the variable `drink`. Copy the two snippets in a javascript console and see the result if you have a minute to spare; it helps with memorising things.

    Other things to notice: those values are only valid in the machine itself (it's not always so clear but for now it's enough).

    This makes total sense: the fact that I give the machine `"vodka"` as a liquour does not mean vodka is the only liquour! Look (and maybe copy and paste each part in sequence in a javascript console):

    ```javascript
    // Part one
    var liqour = 'gin';
    console.log('Outside the function! console.log(liquour) will output "gin");
    console.log(liquour);
    // Part two
    console.log('This function definition won\'t output anything! To prove it I\'ll write something right after');
    function makeLongDrink(liquour, mixer, garnish) {
    var result = "here's a glass of " + liquour;
    result += " and " + mixer;
    result += " garnished with a slice of " + garnish;
    // This is the line that would print
    console.log('liquour is now:'); // the long drink description
    console.log(liquour); // once the machine is activated
    console.log('... but just inside our function, in order for us to make this:');
    console.log(result)
    return glass;
    }
    // Part three
    console.log('See? We just prepared a machine; didn\'t activate it yet.');
    console.log('Let's do it, with a cuba libre');
    // Part four
    var drink = makeLongDrink('rum', 'cola', 'lime');
    console.log('This up here ^ is the result of us calling the function.');
    // Part five
    console.log('Now, despite `liquour` being "rum" within the machine...');
    // Part six
    console.log('It is still "gin" outside:');
    console.log('liquour: ' + liquour);
    ```

    So:

    - The names of the parameters are just placeholders used *within* the function;
    - Once the function is called, the value passed in the spot of the parameter names get assigned to variables of the same name;
    - This assignment has no effect outside the function.

    Now it should be easy to recognise functions you met before: for instance, the formula to calculate the area of a rectangle is:

    width * height

    Nobody assigned `width` or `height`! This is just the description of a process.

    A javascript rectangle area calculator could be:

    function rectangleArea(width, height) {
    return width * height;
    }

    ... and in fact, if we tried to call it:

    rectangleArea(5,6);

    ... we'd have the expected `30`, without assigning the `width` and `height` variables outside.