Functions - Reading Assignment

1.

  1. by using the keyword: function(a,b) ;

  2. using arrow (=>) Example: Const F = (a, b) => { codes here };

2. “var” defines an argument in global scope. “Let” defines argument in a local scope.
3. Pure function is of a value producing kind of functions which has no side-effect also does not rely on effects produced by other codes.

1 Like
  1. A function is created by using the keyword Function followed by the name of that Function and followed by parenthesis(). A Parenthesis() with values inside are the Parameters of that Function.
  2. In functions, arguments (code to be executed) behave as local variables. In regards to scope, “let” declares a variable limited to the block, statement, or expression on which it is used. var on the other hand defines a variable globally, or locally to an entire function regardless of block.
  3. A pure function is a function where the return value is only determined by its input values, without observable side effects
1 Like

Reading Assignment: Chap 3 Functions (Answers)

  1. Create with an expression that start with keyword “function”.
  2. (let) bindings are local to the block they’re declared in, in a loop, the code before and after can not “see” it. Unlike (var) keyword which is visible in global scope.
  3. A pure function, a specific kind of value producing function, with no side effects and doesn’t rely on side effects from other code.
1 Like
  1. A function is created by formulating an expression which starts with the keyword function. A function has a set of parameters and a body. The body is wrapped in braces containing executable statements when the function is called.

  2. In regards to scope “var” and “let” are seen globally and locally in reference to the block where they appear in or out off. The let keyword is local to its declared block (invisible elsewhere), and the var keyword is global, meaning its visible throughout the whole function or global environment where they appear.

  3. A pure function is a kind of Function where the return value is determined by its parameter passed at call time. It a specific kind of value-producing function with no side effects and don’t rely on side effects from other code. It’s a function when called with the same arguments always produces the same values.

1 Like

How can you create a function in Javascript?

A function in Javascript is first created with a keyword, then the name of the function, then a list of parameters between the parantheses, and lastly the code of the function created between curly braces.

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

Let is limited to the block in which it is declared while var has the global scope which is defined throughout the program.

What is a pure function?

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

1 Like
  1. How can you create a function in Javascript?
    With an arrow: =>

  2. What is the difference between var and let in regards to scope?
    Let is relevant only within a block, between the {}'s. Var works “globally” throughout the program.

  3. What is a pure function?
    A pure function is a function which will always return the same output; it produces no side effects.

1 Like
  1. How can you create a function in Javascript?

by starting with the keyword “function”

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

let = declare variables that are limited to the given block, statment or expression
var = define variables globally / to the entire function regardless to the block scope.

  1. What is a pure function?

A pure function has no side effects. It is only defined by the input valueables.

1 Like
  1. How can you create a function in Javascript?
    There are two ways:
  • Using the keyword “function”, e.g.
    function examplefunction() {
    console.log(“This is a function!”)
    }

  • Using the arrow notation:
    sum = (a, b) => a + b

  1. What is the difference between var and let in regards to scope?
    Let only has scope within the block it is defined in; Var has scope within the entire function it is defined in.

  2. What is a pure function?
    A function that only returns a value and doesn’t have any side effects and doesn’t rely on side effects from another code. The only dependent variables for a pure function are its arguments - a pure function always returns the same value for certain 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”. There are several ways to create a function:
    –const power = function(base, exponent) {…};
    –Function as value: let launchMissiles = funtion() {…}
    –Declaration notation: function square(x) {return x * x;}
    –Arrow functions: const power = (base, exponent) => {…};
    Personally, I like the declaration notation since it is more familiar to the way we do things in python.
  2. What is the difference between var and let in regards to scope?
    —Variables defined with “let” are limited in scope to the block they are created in while variables created with “var” are global in scope.
  3. What is a pure function?
    —A pure function is a function which, given the same input, will always return the same output. A pure function produces no side effects.
1 Like
  1. You can create a function in Javascript “with an expression that starts with the keyword function. Functions have a set of parameters ( variables / inputs ) and a body, which contains the statements that are to be execute when the function is called.”

  2. var has either the scope of the whole function, OR a global scope.
    let only has scope limited to its block. This block may be the function it is defined in, or even as small (in block terms) as the loop it is defined in.

  3. A pure function is a function that has no side effects, nor does it rely on side effects from other code; “When called with the same arguments, it always produces the same value (and doesn’t do anything else).” If a pure function works in a given context, it will work in any given context.

1 Like
  1. A function is created with key word function which you use to wrap values.

  2. the let function can only be seen/read by programs within its scope, var function can be read by the global scope of the code.

  3. A pure function is function that has no side effects and is also not reliant on other side effects such as global bindings.

1 Like
  1. How can you create a function in Javascript?
    A few different ways.
    const square = function(x) { return x * x; };
    function square(x) { return x * x; }
    let square = x => x * x;

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

  3. What is a pure function?
    A pure function is a function that returns a value and doesn’t produce any side effects.

1 Like
  1. How can you create a function in Javascript?

By writing the word function followed by name and parameters:
as an expression: const name = functions(params) {return body}
as a statement: function name(params) {return body}

  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 where as bindings declared with var are global to the function they are in or the whole program if they are declared outside functions.

  1. What is a pure function?
    In a pure function, given the same input will always produce same output. A pure function doesn’t produce any side effects.
1 Like
  1. “A function is created with an expression that starts with the keyword function” - Eloquent JavaScript
  2. The difference between var and let in regards to scope is that var can be seen globally throughout the code and let is seen locally within the block it resides in.
  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…” - Eloquent JavaScript
1 Like

Q1. How can you create a function in Javascript?

keyword function function_name(arguments list…arg1, arg2, arg3 etc){
statement 1;
statement 2;

return ;
}

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

let keyword is used to define variable in a function block. The scope of variable is local to that function and cannot be accessed outside the function.
Variables defined with var keyword outside of function block are available throughout the program.

Q3. What is a pure function?
A pure function when called does not produce any side-effect, does not rely on external side-effect and returns same value when called every time with same arguments.

1 Like

How can you create a function in Javascript?
Answer: 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.

What is the difference between var and let in regards to scope?
Answer: Using the var keyword creates a “global binding” that can be seen by the entire program. Using a let keyword creates a local binding that can only be seen within the loop where it resides.

What is a pure function?
Answer: 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).

1 Like
  1. How can you create a function in Javascript?
    Using the function keyword at the start of a statement allows a function to be called anywhere. It has a set of parameters and a body that contains the statements to be executed when the function is called.
function(param1, param2) {
    // some useful output...
}

Another method calls the function just after it is created:

const square = function(x) {
 return x * x; 
};
 console.log(square(12)); 
// → 144

And more simply with declaration notation, the function name is between the keyword function and the parameters in {}.

function add(x, y) {
 return x + y;
 };
  1. What is the difference between var and let in regards to scope?

var (variable) was pre-2015 JS binding declaration… This keyword is visible
throughout the whole function that they appear in—or throughout the global
scope, if they are not in a function.

let (and const) are local to the block that they are declared in. (If in a loop, code before/after the loop will not see it)

  1. What is a pure function?
    A function that produces a value that is not affected by other code. I.e. it will return the same values given the same parameters.
1 Like
  1. You can create a function by writing ‘function()’ and assigning a variable to it, by using the function notation followed by its name and parameters, or by using arrow functions where the parameters are defined followed by the body or the value to be returned.

  2. With the ‘let’ and ‘const’ keywords, the bindings are local to the block they are declared in, whereas with ‘var’ keyword, it is visible to the whole function it is declared in or if it’s global it is visible to the global scope.

  3. Pure functions do not rely on any side effects from other code, and doesn’t produce any. They return a value.

2 Likes

1-> A function is created with an expression that starts with the keyword function
2-> A var has a global scope and a let is a local scope => this means the var is visible outside the function body and the let is not visible in the function body
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

2 Likes
  1. How can you create a function in Javascript?
    To create a function in JavaScript you type the word function is coded. You must have a set of parameters and a {body} that executes the code.

  2. What is the difference between var and let in regards to scope?
    The difference between let and var in regards to the scope is that let is for local bindings and var is for global bindings. Let can be seen inside a local block. Var can be seen outside the local binding.

  3. What is a pure function?
    A pure function is a specific kind of value that produces a f(x) without side effects and relies on side effects from other code (doesnt read global binding whose value may change).

1 Like