Exercises Files – Section 10

In learning functions, we work to understand how to declare functions, pass information to functions and to get information back from functions.  In declaring functions we use the return keyword and the parentheses to declare a function and we use brackets to enclose the function code.

function name() {
     function code   
}

If our function code needs information passed to it from outside the function, we pass information in with an argument placed between the parenthesis.

function name(argument) {
     function code
     use the "argument" in your code
     }

To return the result from our function we use the return keyword to return the information from our function.

function name(argument) {
    function code
    use the "argument" in your code
    use the 'return keyword' to return your code from your function
    }

In order to use our function, we have to call it and we can save the results by placing them in a variable.

name(argument); //Calling the function:
var save_return_value = name(argument); //Saving the information returned from the function to a variable:

In defining variable scope; variables defined outside the function are global.  To define a variable inside a function, we use the ‘var’ keyword or we pass it in through an argument.  If we don’t use the ‘var’ keyword to declare a local variable, it will be recognized as a global variable if one exists.

var scope = "global";

console.log(scopeTest());
console.log(scopeTest1("scopePassedIn"));
console.log(scopeTest2());

//Global scope changed in local scope because var keyword not used locally
function scopeTest() {
   return scope = "local";
}

function scopeTest1(argument) {
   return scope = argument;
}

//Global scope not changed because the 'var' keyword is used locally.

function scopeTest2() {
     var scope = "doesNotChangeGlobalScope";
     return scope;
      }

RGB Color

Game

isEven

Factorial

KebabToSnake

Higher Order Function

Leave a Reply

Your email address will not be published. Required fields are marked *

css.php