Functions - Reading Assignment

  1. To create a function, just declare it in a statement,
  • e.g. function name(parameters…) = algorithm
    or, more explicitly, defined in an expression,
  • e.g. const name = function(parameters) algorithm
    OR using arrows that we don’t cover here but is reminiscent of R,
  • e.g. const name = parameters => algorithm
  1. variables defined using var have a scope that includes the entire function (or global space) whereas let variables are local to the block of code in which they’re defined (i guess block means loop/sub-context of some sort…)

  2. Pure functions can take a certain input and give the same output every time, i.e. they do not depend on contextually defined variables, are context-independent and thus can be checked independetly for whether they’re working correctly.

1 Like
  1. A function can be created in Javascript by using an expression named “function”. Functions can also be created using the symbols “=>” after the list of parameters for that function, but before the body o the function.
  2. The difference between var and let scope is whether the variable has local vs global scope. In other words, bindings (or variables) created inside a particular function using “let” can only be used within that function; it is local to that function. Bindings created using “var” will be be visible throughout a broader function or environment that they appear in, and hence, are global.
  3. A pure function is one that is deterministic and will not cause any side effects. In other words, when called with the same inputs it will produce the same value. It does not rely open global variables whose value may change.
1 Like

1. How can you create a function in Javascript?
By using a function declaration where the statement begins with the function keyword, otherwise it’s defined by a function expression. To clarify:

  1. function goCrazy() {}; //This is a function declaration
  2. const goCrazy = function() {} //This is a function expression

2. What is the difference between var and let in regards to scope?
Var is function scoped and let is block scoped, meaning any bindings declared by let will only be seen as defined values within the block and will not have an associated definition outside that block.

3. What is a pure function?
A pure function will always give the same output if given the same input, causing no observable side effects, such as changing a variable outside of the function itself.

1 Like
  1. How can you create a function in Javascript?
    A function is created with an expression that starts with the keyword function.Functions contain parameters and a body, which contain statements that are to be executed when the function is called. The body of a function is enclosed in braces.

  2. What is the difference between var and let in regards to scope?
    Bindings declared with the word let are local in scope, meaning that they can only be seen within the code block that they are declared in. Bindings declared with the word var have a global scope, so they can be seen throughout the entire program.

  3. What is a pure function?
    A pure function is a value-producing function that not only has no side effects, but also doesn’t rely on side effects from other code. A pure function will always produce the same value when called with the same arguments.

2 Likes
  1. You create a function by using the keyword “function” and give it parameters in () and a body in {}
  2. Let has local scope and var has global scope.
  3. Pure functions produce values and no side effects. They will always produce the same output for the same arguments given
2 Likes
  1. By using the keyword function
  2. var define global or local variables regardless of the scope of the block of code. Let defines local variables. with a limited scope.
  3. A function without side effects that return some value that depends on the input the function gets.
1 Like

How can you create a function in Javascript?

  • By defining an expression that starts with the ‘function’ keyword then followed by the functions’ parameters (wrapped inside a couple of parenthesis) and the body containing the statements to be executed (wrapped in braces).

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

  • When using ‘let’ the bindings defined are only local to the block where they are declared while using ‘var’ makes a binding visible in the function they appear in or throughout the global scope if they are not inside a function.

What is a pure function?

  • A value-producing function that not only has no side effects but also doesn’t rely on side effects from other code. In other words, always produces the same value when called with the same parameters.
2 Likes
  1. A function is created with an expression that starts with “function”.
  2. var makes a variable global whereas let assigns a variable to a limited scope or block.
  3. A pure function doesn’t produce side effects and returns results within the given parameters.
1 Like
  1. by creating a statement with body.
  2. Let lets you call a variable. Var is a global variable used within the entire code.
  3. pure only rely on itself and doest read global binds
1 Like
  1. Like so>
function foo(args){
    Stuff;
}

2.var is global, while let is limited to its block
3. A function that is deterministic (ie. produces the same output with the same inputs every time), depends only on its own arguments and doesn’t change any variables outside its scope.

1 Like
  1. How can you create a function in Javascript?
    To create a fuction, use the keyword fuction, give it parameters and a body

  2. What is the difference between var and let in regards to scope?
    let is only visible to the local block it is declared in, while var is seen by every block

  3. What is a pure function?
    A pure function is a fuction that returns a value that is determined by only the input values, with no side effects

1 Like

can anyone explain what the => in the function?

function wrapValue(n) {
let local = n;
return () => local;
}
1 Like

a function is created with an expression that starts with the keyword function.
functions have a set of parameters () and a body which contains statements to be executed when the function is called.

let is local the the block it is in code before and after cannot see it.
var is visible throughout the whole function it appears in or throughout the global scope if it is not in a function.

A pure function is a function where the return value is only determined by its input values, without visible side effects.

1 Like
  1. You can give a variable or constant, then use the function syntax : var add = function(x) {
    return x + x
    };

  2. The difference between them is that var is function scoped and let is block scoped.

  3. This function has no side effects and doesn’t rely on side effects of other code. When called upon the outcome will always be the same.

1 Like
  1. A function is created by using the keyword function, followed by a name, then parentheses which may or may not contain a set of parameters. And the last part is the function body which must be wrapped in braces.

  2. Bindings created with the var keyword are global in scope if they are not in a function. Bindings created with let have a local scope to the block they were declared in.

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

2 Likes
  1. A function can be created by using the keyword function and giving it parameters and a body.

  2. Variables created using let bindings are local to block they are declared in, whereas those created with var bindings are visible throughout the whole or the whole program, they have a global scope.

  3. A pure function is a value producing function that has no side effects and doesn’t rely on side effects from other code. When called with the same arguments it always returns the same value, and doesn’t do anything else.

1 Like

A JavaScript function is created by writing code containing a keyword defining the function, naming it, including any parameters, and body describing how it should execute

Var is a keyword that defines general or global variables that could also apply locally and let is a keyword that describes a specific local function

A pure function is an expression that neither depends on the side effects of other code nor produces its own side effects

1 Like
  1. utilizing the keyword function and providing parameters and a body.
  2. bindings created with the var keyword are global in scope if they are not function but bindings created with let have a local scope directly to the block they were called in.
  3. pure function has no side effects nor rely on side effects from other code.
1 Like

Q1)

  • SIMPLE DEFINITION; Its instructions that are formed in a coding format which provide us with a specific outcome.
    This outcome is achieved by adding inputs to these instructions.

HOW TO CREATE A FUNCTION;

  1. You need to start an expression with the keyword ‘function’

  2. the keyword ‘function’ would then have an input which you would need to give (also called ‘Parameter’)
    FOR EXAMPLE; function(a) - ‘a’ would be the input i.e. the Parameter
    You can have more than 1 parameter or no parameter.
    e.g. function(a,b) or function()

  3. You would also need to give the function a name i.e. define the function

e.g. const ‘name’ = function(x)

  1. Then, must add a ‘body’ i.e. has the statements which are executed when the function is set.
    e.g.
    const ‘name’ = function(x) {
    return x + x;
    };

let x = 2
console.log(name(2));
// 4 = answer

The body i.e. the code inside the block brackets, would be ‘return x+x’.

Q2)
Scope basically means where variables are available for use. var declarations are globally scoped or function/locally scoped.

The scope is global when a var variable is declared (i.e. shown) outside a function. This means that any variable that is declared with var outside a function block (i.e. outside of {} ) is available for use in the whole window.

var is function scoped when it is declared (i.e. shown) within a function. This means that it is available and can be accessed only within that function.

Example;

var greeter = "hey hi";
    
    function newFunction() {
        var hello = "hello";
    }
  • Here, greeter is globally scoped because it exists outside a function while hello is function scoped. So we cannot access the variable hello outside of a function. So if we do this:
var tester = "hey hi";
    
    function newFunction() {
        var hello = "hello";
    }
    console.log(hello); // error: hello is not defined

We’ll get an error which is as a result of hello not being available outside the function.

Let is block scoped

A block is a chunk of code bounded by {}. A block lives in curly braces. Anything within curly braces is a block.

So a variable declared in a block with let is only available for use within that block.
Example;

let greeting = "say Hi";
   let times = 4;

   if (times > 3) {
        let hello = "say Hello instead";
        console.log(hello);// "say Hello instead"
    }
   console.log(hello) // hello is not defined

We see that using hello outside its block (the curly braces where it was defined) returns an error. This is because let variables are block scoped .

Q3)
A pure function is a function which:

  • Given the same input, will always return the same output.
  • Produces no side effects.

So, console.log( double(5) ); is the same as console.log(10);

This is true because double() is a pure function, but if double() had side-effects, such as saving the value to disk or logging to the console, you couldn’t simply replace double(5) with 10 without changing the meaning.

2 Likes
  1. How can you create a function in Javascript?
    Using an expression that starts with function.
  2. What is the difference between var and let in regards to scope?
    var is global and let is local
  3. What is a pure function?
    Doesn’t produce any side effects
1 Like