Functions - Reading Assignment

  1. With word functions then (input parameters) then {some code;} and bind it to a name:
    let myFunc = function(a, b){some code;}; or function myFunc(a, b) {some code;}
  2. var can be visible outside of its scope, let can be visible only within its scope
  3. Pure function does not produce side effects and does not rely on any variables that are declared outside of it

How can you create a function in Javascript?

Through using the function keyword as an expression we can create a function value. Or function can be used to create a binding and assign a value. The final way to create a function is to use => this is the same as calling the term function, but is less verbose.

What is the difference between var and let in regards to scope?

Var allows for a greater scope in that a binding created with var will be seen throughout a function. Let creates a narrow scope in that only that function will be able to see it; in other words, if there is a nested function within another function and you bind a statement with let, only that inner function will ‘see’ it.

What is a pure function?

A pure function is a specific kind of value-producing function that not only has no side effects but also doesn’t rely on side effects from other code.

  1. You can define the function:

const fairy = function() {
console.log(“tail”);
};

You can declare the function:

function multiply(a, b) {
return a * b;
}

Or, you can use arrow notation:

var add = x => x + x;

  1. The keyword let is only visible within the local scope of the function in which it is defined. It will not be “seen”, or defined, outside of the function environment. However, the keyword var can be seen on a global scope even when it is defined within a function. It will fall under the global scope or the nearest function scope.

  2. A pure function is a function with no side effects that is not influenced by any other global bindings.

  1. How can you create a function in Javascript?
    A function is created with an expression, that starts with the keyword “function”, following by parameters in() and the body in{}.
    e.g.: const MyName=function(Name,Surname){code}
    function MyName(Name,Surname){code}

  2. What is the difference between var and let in regards to scope? The main difference is that “var” is visible throughout the whole function, so the whole program after the declaration can see it, whereas “let” is visible only inside the loop and it´s not visible after the loop.

  3. What is a pure function?
    A pure function doesn´t produce side-effects and also doesn´t rely on side effects from other code. It always produce the same value, if called with the same arguments.

  1. How can you create a function in Javascript?

A function consists of a regular binding where the value of the binding is a function.

You create a function by starting with the word function that expresses a set of parameters () and a body – the body must always be wrapped in braces {}.

  1. What is the difference between var and let in regards to scope?

Bindings, created with the var keyword, are visible throughout the whole function that they appear in—or throughout the global scope, if they are not in a function. Let can be visible only within its scope of its function.

  1. What is a pure function?

A pure function is a specific kind of value-producing function that not only has no side effects but also doesn’t rely on side effects from other code

  1. “A function is created with an expression that starts with the keyword function.”
  2. Bindings declared with let are local to the block that they are declared in whereas var are in the nearest function scope or the global scope.
  3. A function that does not produce side effects nor rely on side effects from other code.
  1. How can you create a function in Javascript?
    function()
  2. What is the difference between var and let in regards to scope?
    var is accessible throughout the program while let is local to the code block
  3. What is a pure function?
    A pure function is a specific kind of value-producing function that not only
    has no side effects but also doesn’t rely on side effects from other code. A pure
    function has the pleasant property that, when called with the same arguments,
    it always produces the same value (and doesn’t do anything else).
  1. How can you create a function in Javascript?

A function expression in JavaScript is defined: function funtionName (Argument1, Argument2, …) {

set of instructions and even functions

}

function(param1, param2) {

// do something meaningful here…

}

The function can be given a name ( function definition ) by simply assigning the function expression to a binding, e.g.:

const add = function(x, y) { return x + y; };

Ex. function Using prompt alert to show a pop up to the user, this uses a predetermined function to make it show up.

  1. What is the difference between var and let in regards to scope?

Bindings declared with let (and const ) are local to the block they are declared in, while var bindings are visible in the whole function they are declared in (or global, if they aren’t in any function). So a var remains throughout the whole program and let can be used to insert another value instead of var for the same parameter. Const can`t be changed.

  1. What is a pure function?

A pure function is a function that doesn’t have any side-effects and doesn’t rely on side-effects from other code. It has the property that it always produces the same value if the same inputs are passed.

  1. By typing function a() { }.
  2. var is global and is seen throughout whole function global, let is more local and usually seen in its block.
  3. Pure functions are function that accept an input and returns a value without modifying any data outside its scope.

How can you create a function in Javascript?
-You express the function name and write the parameters inside brackets.
Then you make the body which contains statements to be executed when function is called.

What is the difference between var and let in regards to scope?
-let works inde a block. If you make one inside of a loop the code
before or after it can not “see” it. bindings made with var are visible
throughout the whole function or global scope

What is a pure function?
-It has no side effects and does not rely on side effects from other code(doesn’t read global bindings).
It always produces the same value.

  1. How can you create a function in Javascript?
    -> A function is created with an expression that starts with the keyword function. Functions have a set of parameters (in this case, only x) and a body, which contains the statements that are to be executed when the function is called. The function body of a function created this way must always be wrapped in braces, even when it consists of only a single statement.

  2. What is the difference between var and let in regards to scope?
    -> Bindings declared with let are in fact local to the block that they are declared in. Functions created with the var keyword, are visible throughout the whole function that they appear in—or throughout the global scope, if they are not in a function.

  3. What is a pure function?
    -> A pure function is a specific kind of value-producing function that not only
    has no side effects but also doesn’t rely on side effects from other code—for
    example, it doesn’t read global bindings whose value might change. A pure
    function has the pleasant property that, when called with the same arguments,
    it always produces the same value (and doesn’t do anything else).

How can you create a function in Javascript?
A function is created by defining a variable then the function parameters
const [Variable] = function([Object]){[Function Parameters]}

What is the difference between var and let in regards to scope?
variables defined with “let” (or const) are “…local to the block that they
are declared in…”
variables defined with “var” “…are visible
throughout the whole function that they appear in—or throughout the global
scope…”

What is a pure function?
“A pure function is a specific kind of value-producing function that not only
has no side effects but also doesn’t rely on side effects from other code…”
For this reason pure functions can reliably produce the same output when given the same input (regardless of how the code outside the function is changed.

  1. a) Expression: const f = function(x) { return x+x; };
    b) statement: function g () = { return ‘hello’; };
    c) arrow: const h = x => x * 5;

  2. each loop and function forms its own scope in which local variables can be defined and used.
    “var” is obsolete and does not know the loopscope -> so it is also valid outside the loop. For functions var is only valid within the scope.

  3. a pure function has no side effects and didn’t use any global variable.It is a “stand alone” function

  1. You crate a function with an expression that starts with the keyword function.

  2. The difference is that when var is declared it is viewed as a global scope while let is viewed as a local scope.

  3. A pure function is a specific kind value producing function that not only has no side effects from code.

  1. By creating an expression with the keyword “function”. It has a set of parameters, and a body which contains the statements to be executed.

  2. A binding declared with “let” is only visible within the block in which is declared. A binding declared with “var” is visible within that block and outside it.

  3. A pure function is a function that does not produce side effects. It also does not rely on side effects from other code.

image How can you create a function in Javascript? A function is created with an expression that starts with the keyword function. Functions have a set of parameters and a body, which
contains the statements that are to be executed when the function is called.
The function body of a function created this way must always be wrapped in braces, even when it consists of only a single statement.
What is the difference between var and let in regards to scope? Bindings declared with let which are local to the block that they are declared inside of a loop, the code before and after the loop cannot “see” it.
Var keyword, are visible throughout the whole function that they appear in—or throughout the global scope, if they are not in a function.
What is a pure function? A pure function is a specific kind of value-producing function that not only has no side effects but also doesn’t rely on side effects from other code—for example, it doesn’t read global bindings whose value might change.

1.A function is JavaScript vocabulary that is created by typing in a valid word to produce a value

2.var assigns a binding that is global to the scope of the programme, while let is local

3.A pure function is one which has no side effects, not rely on global bindings

  1. function nameOfFunction (Parameter1, Parameter2, etc) {instructions}
  2. let has a local scope and can not be called outside the block it has been used in, var has global purpose and can be called if outside the block as well when declared outside that block.
  3. A ‘pure function’ is a function that does not create any side effects and doesn’t rely on side-effects from other code. It produces the same value if the same inputs are used.
  1. How can you create a function in Javascript?
    Declare the keyword function followed by the name of the function. The function may receive parameters, which are passed inside brackets. The body of the function is wrapped in curly braces and contains any code to be executed when the function is called. If the function produces a value, rather than a side effect, then a ‘return’ statement is used to return the value…
    function myFunction(param1 param2){
    var rtn = 0;
    rtn = param1 * param2;
    return rtn;
    {

  2. What is the difference between var and let in regards to scope?
    A var binding created anywhere within a function exists within the scope of, and is visible throughout the function, whereas a let binding only exists within the scope of the block that created it. An example would be a let binding that was created within an ‘if’ block, which would not be visible before or after the block.

  3. What is a pure function?
    A pure function is one that produces a value and does not produce side effects or is reliant on side effects from other code.

Question 1

Functions can be created in JavaScript in the following 2 ways:

  1. The  function  keyword can be used to create a function expression.
    A variable is defined in the usual way and the function expression is assigned (using the  =  operator) to the variable’s name as its value. The function expression itself starts with the  function  keyword followed by parentheses, which contain the function’s parameters (or are left empty if there are no parameters). The function body — a code block wrapped in curly braces — comes after the parentheses and contains the statements to be executed when the function is called. These statements may or may not include code that executes side effects, such as  console.log() . If the function needs to return a value, then this is achieved by creating a statement with the  return  keyword followed by an expression which evaluates to the value to be returned. A semicolon is placed after the closing curly brace.
    const name = function(parameter1, parameter2) {
        // function body
        return parameter1 * parameter2;
    };
  1. Alternateively, the  function  keyword can be used to create a function declaration.
    Instead of defining a variable with the keyword  const  or  let , the  function  keyword opens a statement which contains the name of the variable the function is assigned to, followed by parentheses (containing any parameters) and finally the function body. The function body has the same features and characteristics as the one in the function expression, except no semicolon is required after the closing curly brace.
    function name(parameter1, parameter2) {
       // function body
        return parameter1 * parameter2;
    }

With both methods, the function is called using the variable name it has been assigned to, followed by parentheses. The parentheses contain any arguments fed into the function (to serve as its parameters) or are left empty if the function has no parameters.

    name(argument1, argument2);

Question 2

A variable defined with  var  has function scope. This means that if the variable is defined within a function, then it can be accessed from anywhere within that same function, but not from outside the function. If the variable is defined outside all of the program’s functions, then it has global scope, meaning that it can be accessed from anywhere within the program.

A variable defined with  let  has block scope. This means that if the variable is defined within a code block, then it can only be accessed from anywhere within that same code block, but not from outside the code block. If the variable is defined outside all of the program’s code blocks, then it will have global scope, meaning that it can be accessed from anywhere within the program.

It therefore follows that:

  1. A variable defined outside all of the program’s functions and code blocks has the same global scope whether it is defined with  var  or  let . It can be accessed from anywhere else within the program.

  2. A variable defined outside all of the program’s functions, but within the curly braces of an if statement, or loop, can be accessed from anywhere within the program if it is defined with  var . However, if it is defined with  let  it can only be accessed from within the same code block (including from within any functions and other code blocks nested within it).

  3. A variable defined within a function body (but not also within any other functions or code blocks nested within that same function body) has the same local scope whether it is defined with  var  or  let . It can be accessed from anywhere else within that same function (including from within any other functions and/or code blocks nested within it).

  4. A variable defined within a function body (and also within the curly braces of an if statement, or loop, nested within that same function body) can be accessed from anywhere within the function if it is defined with  var . However, if it is defined with  let  it can only be accessed from within the same code block (including from within any functions and/or other code blocks nested within it).

Question 3

A pure function has two main characteristics:

  1. It is deterministic, meaning that it will always return the same value when fed the same arguments. This is because it is a closed system, and does not depend on any side effects or other external variables.

  2. It does not produce any side effects, and only returns a value.