Functions - Reading Assignment

  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.

  1. How can you create a function in Javascript?
    A function is created with the keyword “function”. They have parameters and a body with statements (to be executed, wrapped in braces).

  2. What is the difference between var and let in regards to scope?
    “let” is a local for the block they are declared in. “var” is global and can be used on the whole function it is declared in (or globally).

  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.

  1. The syntax to define functions in Javascript is as follows:
    function functionname(var1, var2, etc)
    {
    code for action to be taken
    }

  2. var == function scoped, defined throughout the program
    let == block scoped

  3. 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. When called with the same arguments, it produces the same value (and doesn’t do anything else).

  1. By using " function()" or “>=” like normal binding
  2. Var is pre 2015 Javascript, it is global binding; Let is post 2015 Javascript, it is local binding.
  3. Pure function has no side effect and does not rely on side effect from other part of the code.
  1. How can you create a function in Javascript?
    A function definition is a regular binding where the value of the binding is a function.
    const square = function(x) {
    return x * x;
    };
    2. What is the difference between var and let in regards to scope?
    Bindings declared with let and const are in fact local to the block that they are declared in

    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
  1. How can you create a function in Javascript?
    A function is created in JavaScript with an expression using the function keyword.

  2. What is the difference between var and let in regards to scope?
    They differ because let is visible only within the local scope of the function block it was declared in while var is visible throughout the entire function.

  3. What is a pure function?
    A pure function only produces a value, has no side effects and does not rely on side effects from other code.

1.How can you create a function in Javascript?
function LoveSteak ();{
}
2.What is the difference between var and let in regards to scope?
var - defines Globaly
let - limited to the scope

3.What is a pure function?
It has no side effects and doesnt rely on side effects from other code.

1a. const name = function(a){ ;};
b. function name(a){ ;}
c. let name = a => ;

  1. var applies globally even if it was created inside of a function, while let will only be defined inside the block it was created.

  2. A pure function gives a value with no side effects and doesn’t rely on other side effects to function.

1 A function is created with an expression that starts with the keyword function.
2 let allows you to declare variables that are limited in scope to the block, statement, or expression on which it is used. var keyword defines a variable globally, or locally to an entire function regardless of block scope.
3 A pure function is a function that not only has no side effects, but it also doesn’t rely on side effects from other code. When it is called with the same arguments, it will always return the same result.

  1. How can you create a function in Javascript?

An expression that starts with function, following by parameters (or not), with instructions between the curly brackets that follow

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

Let can only be accessed inside the scope it was declared. Var can be used anywhere outside of the scope as well

  1. What is a pure function?

A function that has no side effects and is also deterministic

How can you create a function in Javascript?

Function is created with an expression that starts with the keyword function

function type_name(parameter1, parameter2){
//code to be executed
}
document.write(type_name);

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

Var defines a variable globally, or locally to an entire function regardless of block scope.
Let allows you to declare variables that are limited in scope to the block, statement, or expression on which it is used

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

  1. How can you create a function in Javascript?
    With an expression that starts with the keyword function. Then parameters(x,y)
    and body {instructions}

  2. What is the difference between var and let in regards to scope?
    Var is function scoped and let is block scoped.

  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.

  1. How can you create a function in Javascript?
    By writing var myvariable = funct(x){some code here}

  2. What is the difference between var and let in regards to scope?
    Let will only be visible to code within it’s bindings whereas var will be visible globally.

  3. What is a pure function?
    One with no side effects.

  1. How can you create a function in Javascript?

Typing the keyword function followed by parenthesis with multiple parameters or none of them inside. After that and always inside braces comes the function body.

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

var is always visible throughout a function or global when is outside a function. However, let or const are only local so their scope is the block they are declared in.

  1. What is a pure function?

It is a kind of function that produces a value and has no side effects. It doesn’t rely on side effects from other code either. Moreover, when called with the same arguments, it always throw the same value and doesn’t do anything else.

  1. How can you create a function in Javascript?
    by using the word “function” and naming the function then setting the parameters then the body of statements to be executed.
  2. What is the difference between var and let in regards to scope?
    var is set globally in the code so anything after it is set will see and use it where as a let is only visibly used in a local scope inside a loop and will not be used after the loop.
  3. What is a pure function?
    It has no side effects and will not rely on side effects from other code, it will always produce the same value.