Functions - Reading Assignment

  1. A function is a parametric block of code defined once and called multiple times later. In JavaScript a function is composed and influenced by many components:

a) JavaScript code that forms the function body.
b) The list of parameters.
c) The variables accessible from the lexical scope.
d) The returned value.
e) The context this when the function is invoked.
f) Named or an anonymous function.
g) The variable that holds the function object.
h) arguments object (or missing in an arrow function).

  1. var and let are both used for variable declaration in javascript but the difference between them is that var is function scoped and let is block scoped.
    A variable declared with var is defined throughout the program as compared to let.
  1. In programming, a pure function is a function that has the following properties:
  1. The function always returns the same value for the same inputs.
  2. Evaluation of the function has no side effects. Side effects refer to changing other attributes of the program not contained within the function, such as changing global variable values or using I/O streams.
1 Like
  1. How can you create a function in Javascript?
    A function is created with an expression that starts with the keyword function.
    (It’s possible define a binding to a function)

  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. Bindings, created with the var keyword, are visible
    throughout the whole function that they appear 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. A function can be created with an expression calling the keyword function.

  2. let is used locally and var is used globally and locally.

  3. A pure function is a function where the returned value is determined by it’s own input, not by any outside variables.

1 Like

1 - There are different ways:
a. let myFunction = function (arg1, arg2) { /* function body here / };
b. function myFunction (arg1, arg2) { /
function body here / };
c. const myFunction = (arg1, arg2) -> ) { /
function body here */ };

2 - Binding declared with var behave differently, they end up in the nearest function scope or the global 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 a loop, the code before and after the loop can not see it.

3 - A pure function is a specific kind of value - producing function that not only has no side effects from other code - for example, it does not 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.

1 Like
  1. A function in JS:
  • we can create with an expression that starts with the keyword function,
  • “function” is followed by parameters like (x) - wrapped in braces (always, even when it consists of only a single statement)
  • can have multiple parameters or no parameters - ()
  • a body, which contains the statements that are to be executed when the function is called,
  • can produce a value: power, square,
  • does not produce a value: makeNoise - no parameters - only result is a side effect
  • a return statement determines the value the function returns,
  • return - expression = undefined: makeNoise

Parameters behave like regular bindings:

  • their initial values are given by the caller of the f. not the code in the f.
  1. Each binding has a scope, defined outside of any function or block, is global (the whole program).
    Var and let are local bindings (to the block where they are declared, e.g inside a loop), created for function parameters or declared inside a function.

  2. A pure function is a function:

  • where given the same input is always going to return the same output,
  • produces no side effects,
  • avoids shared state & bugs associated.
1 Like
  1. a function can be created using the keyword function followed by the function name, then - if needed - the parameters inside (), then followed by curly brackets where there’ll be the body of the function, which will contain the code that will be executed when function is invoked.

  2. let declares a variable limited to the block, statement, or expression on which it is used, while instead var defines a variable globally, or locally to an entire function regardless of block.

  3. it’s. function which will return always the same output, if that you give it the same inputs (like a univocal function). Therefore it does not have side effects (that is, other than returning the value it is supposed to return, it doesn’t produce any other change in the state of the program).

1 Like
  1. A function can be created in three different way that do the same thing apart from a minor detail.
    const function_name = function (paramter1, paramter2) { statement }
    function name_function (paramter1, paramter2) {statement}
    const name_function = (paramter1, paramter2) => {statement}
  2. Let is use to define a local variable uses into the small world defined by the function’s block. The code cannot see that variable before or after the funtion’s block. Var instead is used to define global variable that will be used during the program and can be seeing by the whole code.
  3. A pure function doesn’t depends on any external input as it doesn’t modifies any variable outside the function itself.
1 Like
  1. 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.

  2. var = bindings are not visible within the loop
    let = bindings, created with the var keyword, are visible

  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

  1. const rainForecast = function() { //code};

  2. A variable using ‘var’ statement is known throughout the function it is defined in/ from the start of the function whereas a ‘let’ statement is only known in the block it is defined in/from the moment it is defined.

3.A pure function will always return the same output when given the same input. There are no side effects.

1 Like

Hi @Nicksta,
I guess there is some confusion here.
var variables have a scope limited to the particular function that it is defined in, or globally if it is defined outside any function.
let variables however is limited to scope between the enclosing brackets only. (for loop brackets, if statement brackets etc)

Hope this clears it out.

Happy Learning! :slight_smile:

2 Likes

thanks. that helped me a lot

  1. How can you create a function in Javascript?
    A function is created with an expression that starts with the key word ‘function’. Functions have a set of parameters/none and a ‘body’, 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?
    The main difference is the scope difference , while ‘let’ can be only available inside the scope it’s declared, ‘var’ can be accessed outside the block.
  3. What is a pure function?
    A pure function’s return value is based only on its inputs and has no other dependencies or effects on the overall program.
1 Like
1. How can you create a function in Javascript? 
    1. You bind a certain piece of program to a value, where the value of the binding is the function.
2. What is the difference between var and let in regards to scope? 
    1. Var can be seen globally depending on where it is placed, while let can only be seen locally (i.e., within the scope of a certain function).
3. What is a pure function? 
    1. A function that does not rely on or produce side effects, always producing the same value when called with the same arguments.
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 have a set of parameters and a body, 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?
    bindings created with var are visible throughout the global scope but those created whit let are 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—for
    example, it doesn’t read global bindings whose value might change.
1 Like

How can you create a function in Javascript?**
const square= function(x) {
return x*x;
};

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

let and const are local to the block they are declared in, so it can only be used within that block. var is global, so it can be used throughout the whole function it is declared in, or if not in a function, it can be used globally.

What is a pure function?**

A pure function is a value producting function that can be subsituted with its return value without changing the meaning of the code. A pure function does not product a side effect and does not rely on global binding that may change. When called with the same arguments, it always produces the same value.

1 Like
  1. function name(parameter1, parameter2, parameter3) {
    code to be executed
    }

  2. f we create a var variable, it wil be visible as a global variable in the whole block, even if we created it in a loop for example. Let, on the other hand, will be regarded as a local variable, and could be accessed only in specific block, f.e. in that specific loop and not outside of this loop.

  3. It is a function that yields always the same results when provided with the same arguments, doesn’t depending on the context when it is used

1 Like

1.) You create a function by using the expression, “function” and the function has certain parameters along with a body, (that contain the statements that are to be executed.) The function body must be wrapped in braces, ie. function(x)

2.) The difference between let & var’s scopes are that; let, (when used) can only be seen within the local block that they are declared in and var, (when used) can be seen throughout a whole function they appear in or globally throughout the program when not used as a function.

3.) A pure function is a value producing function that has no side effects nor relies on side effects from other code and always returns the same result as long are the parameters are the same; these functions are a pillar of functional programming.

1 Like
  1. Const myNewF = function (parameters) …

Const myNewF = (parameters) => …

Function myNewF (parameters) …

  1. Let is local to the block and var are global if not in a function

  2. Function that produces a value and no side effects and didn’t rely on side effects from other functions

1 Like

Whoops meant arguments not parameters

  1. How can you create a function in Javascript?
    You create a function in Javascript by using the keyword function.

2.What is the difference between var and let in regards to scope?
Var is a global code that can be used thought-out the code where let is a local code used in a specific code function.

  1. What is a pure function?
    A pure function returns the same value. It has no side effects and relays on no side effects.
1 Like