Functions - Reading Assignment

  1. How can you create a function in Javascript?
    Using the keyword function and defining it’s name, like:

const square = function(x) {
return x * x;
}

  1. 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, so if you create one of those inside of a loop, the code before and after the loop cannot “see” it. 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.

  2. What is a pure function?
    Specific kind of value-producing function that has no side effects and also doesn’t rely on side effects from other code. It doesn’t read global bindings whose value might change. When it’s called with the same arguments, it always produces the same value (and doesn’t do anything else)

  1. functions are created by: function = functionName(any parameters) { what the function does }
  2. ‘let’ and ‘const’ are local in regards to scope so they can only be used in the block of code where they are created. ‘var’ is global in regards to scope so it can be used anywhere in the program.
  3. a pure function is one that doesn’t read global variables and only produces one output regardless of the input.
  1. How can you create a function in Javascript?
    with a variable binding, instead of the value, function will be inserted. Short cut would be:
    function abc(argument1, argument2,…) {
    statement 1
    statement 2
    return result 1
    }
  2. What is the difference between var and let in regards to scope?
    var is global and can be called outside of the function block, let cant
  3. What is a pure function?
    A function without side effects, It returns a value and uses only local bindings.
  1. functionName = function(parameter) {returned value;}
  2. let creates local binding, that program can’t see outside of the function, while var helps to make global bindings.
  3. Pure function is the one, that doesn’t have side effects, and reads only local bindings.
  1. const hello = function (param1, param2) {
    function body
    }
  2. let allows us to declare a variable in a scope but var declares a variable in global scope except a function.
  3. In pure function output of a function depends on the input parameters provided to it.
  1. How can you create a function in Javascript?
    function test (){
    console.log(“Hello, World”);
    };

  2. What is the difference between var and let in regards to scope?
    var is for global scope

  3. What is a pure function?
    A pure function has no side effects.

1. How can you create a function in Javascript?
function name(parameters){
code to be executed
}

2. What is the difference between var and let in regards to scope?
Let creates a variable localized to the block or statement in which it’s used. Var is used to define a variable globally or within all blocks within a function in which its used

3. What is a pure function?
Pure functions are functions that act independently from the rest of the program. They always create the same output from a given input and produce no side-effects.

  1. HOW CAN YOU CREATE A FUNCTION IN JAVASCRIPT?

A: A function is created with an expression that starts with the keywordfunction. Functions have a set ofparameters(in this case, onlyx) and abody, whichcontains 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 inbraces, even when it consists of only a single statement.

  1. WHAT IS THE DIFFERENCE BETWEEN VAR AND LET IN REGARDS TO SCOPE?

A:Parameters and bindings declared in a given scope are local and not visible from the outside. Bindings declared with var behave differently—they end up in the nearest function scope or the global scope.

  1. WHAT IS A PURE FUNCTION?

A: 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 using the starting with keyword ‘function’ then follow by a name then follow by the arguments separated by , . E.g. function name(arg1,arg2…)
  2. What is the difference between var and let in regards to scope?
    let is limited by the block, statement or expression whereas var is globally or locally to the entire functions.
  3. What is a pure function?
    Pure function does not have any side effects and doesnt rely on side effects.
1. How can you create a function in Javascript?

They are created using the keyword function. They have parameters that we set within parentheses, and a body, which contains the expression to be executed.

A function may look like the following:

const power = function(base, exponent) {
let result = 1;
for (let count = 0; count < exponent; count++) {
result *= base;
}
return result;
};

In the parentheses following function, we set the parameters. Parameters may be empty, or have one or several values. When calling the function, should we use too many parameters, like below:

function square(x) { return x * x; }
console.log(square(4, true, "hedgehog"));
// → 16

The program will ignore the extra parameters. Should we use too few, the program will assign undefined to the forgotten parameters. This can be bad, because we can create mistakes without the system telling us about it, but could also prove useful such as below:

function minus(a, b) {
if (b === undefined) return -a;
else return a - b;
}
console.log(minus(10));
// → -10
console.log(minus(10, 5));
// → 5

We can assign default values to parameters in order to circumvent those situations, or simply if needed, by writing an equal sign after the parameter. For example:

function power(base, exponent = 2) {
let result = 1;
for (let count = 0; count < exponent; count++) {
result *= base;
}
return result;

Here the default value for exponent is 2.

Therefore:

console.log(power(4));
// → 16

In braces, we have the body of the function, containing the statement to be executed when the function is called. The return statement determines the value to be returned by the function. The code will be read and when it hits the return statement, jumps out of the function to give back the result. Failure to define a return statement will give undefined.

In the first function above, the binding, const was declared. Doing so submits the function to the normal control flow of the program, i.e. top to bottom and left to right:

console.log("The future says:", future());
const future = function () {
return "You'll never have flying cars";
}

We get an error because we are trying to use a function called future, that we define after calling it with console.log. There is another way to define functions, that doesn’t submit it to the control flow:

console.log("The future says:", future());
function future() {
return "You'll never have flying cars";
}

simply using the keyword function, and then naming it will allow you to create a function and use it anywhere in the code.

A third and final way to declare functions is using the “implies” sign:

const power = (base, exponent) => {
let result = 1;
for (let count = 0; count < exponent; count++) {
result *= base;
}
return result;
};

The differences compared to the function declaration are minor.

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

The scope of a binding is the part of the program in which is the binding is visible. Global bindings are defined outside the scope of any function and can be called at any point in the program. When defined inside a function though, those bindings are called local and can only be used within that function. The bindings are therefore created again every time the function is called. This provides a quarantine and ensure that each function can operate separately. As a result, it is possible to read the function and understand it without knowing the global program.

Var is the historical way to create a binding and is visible throughout the program. It used to be that only bindings within a function were local. Const and let were introduced to enable local bindings only visible in the block they are in, i.e. only inside the curly brackets. Here is an example of the side effects:

let x = 10;
if (true) {
let y = 20;
var z = 30;
console.log(x + y + z);

console.log(y);
//Syntax error

console.log(x+z);
//40

Here, X is out of the block, therefore when called outside of it, even when using let, a result back is given. Y is in the block, and let is used to bind it, so when called, no result is given. Z is in the block, but var is used to bind it. So when called it works perfectly.

When using different bindings with the same name, should a function be called, it will use its inner binding, not the “global” one:

const halve = function(n) {
return n / 2;
};
let n = 10;
console.log(halve(100));
// → 50
console.log(n);
// → 10

When nesting functions, the inner function may see the outer bindings as well as its own, but the outter function cannot:

const hummus = function(factor) {
const ingredient = function(amount, unit, name) {
let ingredientAmount = amount * factor;
if (ingredientAmount > 1) {
unit += "s";
}
console.log(`${ingredientAmount} ${unit} ${name}`);
};
ingredient(1, "can", "chickpeas");
ingredient(0.25, "cup", "tahini");
ingredient(0.25, "cup", "lemon juice");
ingredient(1, "clove", "garlic");
ingredient(2, "tablespoon", "olive oil");
ingredient(0.5, "teaspoon", "cumin");
};

Here the function ingredient can see the bindings of function hummus, but hummus cannot see the bindings of ingredient. Therefore the visibility is “upward”, I can see what is up there, not “downward”, those that are up cannot see what is down there. This approach to binding is called lexical scoping.

  1. What is a pure function?

A pure function is a value producing function, therefore has no side effects, and that does not rely on side effects from other lines of code. It does not read global bindings and will always return the same results when input with the same arguments and does not do anything else. They are easier to test than non pure functions. They need not be an obsession though, as side effects might be useful to optimize execution time.

  1. There are several syntaxes that can be used, for example you can create a binding
    let a = function(x,y){return x+y;};
    OR you can just straight up define the function using the function keyword
    function a(x,y) {return x+y;}

  2. When defining bindings using var they are valid globally, throughout the whole code, however let bindings are valid only for the code block where they were defined. Using let instead of var is useful to keep the bindings under control, not having them influence code statements we don’t intend to, as well as being able to use the same variables in different parts of the code repeatedly (eg. generic bindings such as n, x etc)

  3. It is a function that does not create nor depend on any side effects generated anywhere else in the code. It is the most predictable and versatile type of function.

  1. How can you create a function in Javascript?
    A function is created with an expression that starts with the keyword function
    Then we define parameters to the function between () and a body between {} which contains the statements that are to be executed when the function is called.

  2. What is the difference between var and let in regards to scope?
    var is a global binding, which means that if it is defined outside a function, we can refer to this binding wherever we are in the program.
    let is a local binding, which means we can only refer to this binding in the block it has been created in.

  3. What is a pure function?
    A pure function is a function that when called with the same arguments will always produce the same result. It has no side effects and doesn’t rely on side effects from other code

  1. How can you create a function in Javascript?
    By creating an expression that starts with the function keyword (x) {}

  2. What is the difference between var and let in regards to scope?
    Let allows you to declare variables inside of a specific block on which it is used. Where Var can define global variables

  3. What is a pure function?
    A pure function is a value producing a function with no side effects and has no reliance on other code with side effects. When called with the same arg, it will always return the same value.

  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.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.A function can have multiple parameters or no parameters at all.

  2. What is the difference between var and let in regards to scope?
    var and let are both used for function declaration in javascript but the difference between them is that var is function scoped
    and let is block scoped. It can be said that a variable declared with var is defined throughout the program as compared to let.

3.What is a pure function?
Pure functions are functions that accept an input and returns a value without modifying any data outside its scope(Side Effects).
Its output or return value must depend on the input/arguments and pure functions must return a value

  1. How can you create a function in Javascript?
    A function is created by using an expression that begins with the word “function” followed by a set pf parameters and a body text.
  2. What is the difference between var and let in regards to scope?
    “let” is local and “var” is global.
  3. What is a pure function?
    A pure function is a function that does not rely on global effects and will produce the same results each time given its inputs.
  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. For example, I can define var a = “Hello”; or I can define function a() { return console.log(“Hello”); }
    1. What is the difference between var and let in regards to scope?: bindings created with ‘let’ are local to the block of code they are created in (ie: a function or loop). bindings created with ‘var’ can be seen throughout the program, or are considered global.
    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—for
      example, it doesn’t read global bindings whose value might change.
  1. How can you create a function in Javascript? I can create a function in 2 ways:
    declaring a binding and make it equal to a function or writing directly the function. `

function nameFunction ( argument1, argument2, …) { //code of function};`

  1. What is the difference between var and let in regards to scope? Let defines a variable with it’s scope limited to the block on which it is used. Var defines a variable with a larger scope.

  2. 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. How can you create a function in Javascript?
  • a function is defined using the keyword “function”, followed by the name of the function, then a set of brackets containing the parameters, then a set of curly brackets containing the code to be executed, eg:
  • function nameOfFunction(parameters) {code to be executed}
  1. What is the difference between var and let in regards to scope?
  • ‘let’ allows you to declare variables that are limited in scope to the block, statement or expression.
  • ‘var’ allows you to define a variable globally, regardless of block scope.
  1. What is a pure function?
  • a value producing function which has no side effects and which does not rely on other code (which may change). Therefore, when called with the same arguments, it always produces the same result.

  1. A function is created using the function keyword. It is followed by () with option parameters included within. function congratulateIvan(reason) { console.log(“Congratulations Ivan on " +reason +”!"); }

  2. let declares variable that can be limited to the scope of the block in which they are created. (local)

  3. a Pure function can be passed the same parameters every time and will always give the same result. This means the function and its parts are not influenced by other parts of the program.