Functions - Reading Assignment

Welcome to the discussion about the reading assignment about Functions - Reading Assignment.

Leave your answers to the questions below in this thread. If you have any questions or you want to discuss something connected to the assignment feel free to do it in this thread as well, but please everything to the topic.

  1. How can you create a function in Javascript?
  2. What is the difference between var and let in regards to scope?
  3. What is a pure function?
38 Likes

1

A function is created with an expression that starts with the keyword function. Functions may or may not have a set of parameters.

function dislayName() {
  // statement or function body
}

We can call this function on a button click.

<button onClick="dislayName();">Ask for Name</button>

or

function fnName(p1, p2) {
  return p1 * p2;
}
document.write(fnName(4, 3));

Result will be 12

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 where the return value is only determined by its input values, without observable side effects.

11 Likes
  1. How can you create a function in Javascript?
  2. To start, a JavaScript function is a block of code designed to perform a particular task. A JavaScript function is defined with the function keyword, followed by a name, followed by parentheses ().

    function name(parameter1, parameter2, parameter3) {code to be executed}
    
  3. What is the difference between var and let in regards to scope?
  4. In functions, arguments (code to be executed) behave as local variables. In reguards 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 reguardless of block.
    Sources: Mozilla Developers

  5. What is a pure function?

    A pure function is a function that given the same input will always return the same output and it producess no side effects. They are completely independent of outside state, do not alter any external states, easy to move around and reuse throughout a program, and great for future adaptations. Here is a link to a video describing more about pure functions
    Also check out Fork this pen and change impure function to pure functions.

23 Likes
  1. How can you create a function in Javascript?
    To declare with a variable and make it equal to a function.
    Ex. const func = function() { //code inside };
    To write function then the name after it.
    Ex. function func() { //code inside};
  2. What is the difference between var and let in regards to scope?
    let and const use block scoping, they won’t appear locally. Var uses the global scope if they are not declared in a function.
  3. What is a pure function?
    It doesn’t produce or rely on side effects, avoids reading global bindings.
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. The function body of a function
created this way must always be wrapped in braces, even when it consists of only a single statement.
2-What is the difference between var and let in regards to scope?

let is recognized by the block in which the variable is declare. var is dif by all
the program

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. function funtionName (Var1, Var2, …) {set of instructions}
  2. let are variables that are limited to a block (for, if, white, etc), var can be globally
  3. a pure function always returns the same result given same parameters
1 Like

1. How can you create a function in Javascript?

By using the keyword **function** then giving it a parameter **( )** and then a body **{ }**.
2. What is the difference between var and let in regards to scope?
In a block { }
let ---- local scope to the block and can not be seen outside of it.
var---- global scope to the block and outside of it as long it's not in the function.
3. What is a pure function?
A pure function is a value producing function with no side effects and has no reliance on other code with side effects.

function one (x){
return x+1
}

3 Likes

1. How can you create a function in Javascript?
The first method (can only be called after it is created):

const greeting = function(){
  console.log("hello?");
}
greeting();

The second method(can be called anywhere as shown below):

console.log(farewell("good","bye"));

function farewell(param1,param2){
  return (param1 + param2 +"!");
}

2. 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.
3. 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. A function is defined using the keyword function, followed by name, followed by parentheses containing the arguments of the function, followed by the body of the function with curly brackets

  2. The let keywords devines a variable with it’s scope limitet to the block on which it is used, while var defines a variable with a larger scope. Loccalt within a funcition or globall.

  3. A pure function is a function that behaves in a predictable way depending on it’s input parameters. In other words it is not influenced by global variables.

3 Likes

1-
As an expression: const a = function(b) { return b }
As a statement: function a(b) { return b }
As an arrow: const a = b => b

2- var scope continues existing outside a block, let scope only keep inside blocks

3- Pure functions are a function which only read and write its own scope, they don’t rely on read or write bindings outer their scope and don’t make any side effects. Easy to test and very reliable

3 Likes
  1. A function expression in JavaScript is defined with the keyword function followed by a pair of parentheses containing zero or more parameters, followed by the function body in curly braces:
function(param1, param2) {
    // do something meaningful here...
}

The function can be given a name (function definition) by simply assigning the function expression to a binding, e.g.:

const add = function(x, y) { return x + y; };

The shorter and likely more intuitive way to create a function binding is to use the declaration notation, which has the name between the function keyword and the parameter list. The example above would look like:

function add(x, y) { return x + y; }
  1. Bindings declared with let (and const) are local to the block they are declared in, in contrast var bindings are visible in the whole function they are declared in (or global, if they aren’t in any function).

  2. A pure function is a function that doesn’t have any side-effects and doesn’t rely on side-effects from other code. It has the property that it always produces the same value if the same inputs are passed.

1. function functionName(parameters) {code to execute}

2. A binding declared with let is local to the block it is declared in. A binding declared with var is visible throughout the whole function or throughout the global scope, if it is not in a function.

3. Value-producing function that has no side effects and does not rely on side effects from other code.

1 Like

How can you create a function in Javascript?
- You can create a function with an expression that starts with the keyword function and the having some set of parameters that will be used within the function. Within the body of the function you will create a statement that will run when the function is called.

What is the difference between var and let in regards to scope?
- let is only visible within the function that it is created in and var can be called from outside the function.

What is a pure function?
- A pure function is a function that does not create any side effects or rely on side effects

  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 (in this case, only x) 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?
    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. In pre-2015 JavaScript, only functions created new scopes, so old-style 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.

Ex.
let x = 10;
if (true) {
let y = 20;
var z = 30;
console.log(x + y + z);
// → 60
}
// y is not visible here
console.log(x + z);
// → 40

  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. 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). A call to such a function can be substituted by its return value without changing the meaning of the code. When you are not sure that a pure function is working correctly, you can test it by simply calling it and know that if it works in that context, it will work in any context. Nonpure functions tend to require more scaffolding to test.

How can you create a function in Javascript?
-Define a regular binding though define it as equal to a function().

What is the difference between var and let in regards to scope?
-The term “let” and “const” are used for local bindings where “var” is for global bindings.

What is a pure function?
- A pure function is a function which does not have side effects itself nor does it rely on side effects from other functions.

1 Like
  1. How can you create a function in Javascript?
    Functions are created using the ‘function’ keyword, followed by parentheses to hold arguments (if any), with the body of code being surrounded by braces. Functions can be bound to variables just like regular assignments, where the returned value is bound to the variable.

  2. What is the difference between var and let in regards to scope?
    Bindings created with the ‘let’ keyword are local to the block they are created in, however bindings created with the ‘var’ keyword behave differently. If used within a function, they are visible throughout the whole function they appear in, but if used outside of a function they will be global.
    An example using the ‘let’ keyword:

    function test() {
        let check = "outer";
        if (true) {
            let check = "inner";
        }
        console.log(check);
    }
    test();

This code will print “outer” to the console, as the rebinding of the variable to “inner” is only within the scope of the ‘if’ statement.

An example using the ‘var’ keyword:

    function test() {
        var check = "outer";
        if (true) {
            var check = "inner";
        }
        console.log(check);
    }
    test(); 

This code will print “inner” to the console, as the reassignment of the variable using the ‘var’ keyword is global within the function.

Global variables should be avoided if at all possible, as they can cause unpredictable and buggy code if not kept in check.

  1. What is a pure function?
    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 Like
  1. By creating an expression with the keyword function and none, one or more parameters in between brackets ()

  2. A var variable outside a block can be referring to the same variable inside a block because they’re in the same function, a “let" creates a variable that can be local to the enclosing block but not to the enclosing function.

  3. A pure function doesn’t have any side effects and doesn’t rely on side effects from other code

1- By using the keyword “function” and parentheses that comes after it and the arguments can be placed in it or it has no arguments.Then the statement is to be placed between the brackets {}.

2- If you define a variable in a block with keyword “let”, it can’t be seen out of this block.It is local to this block.But if you define the variable by keyword “var”, it can’t be seen by the whole code(global) if it was not in a function.

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. One of Three ways:
    with keyword function and it can have none or many arguments and body.
    Function as value where a var or let is bind to the function.
    Arrow function which uses => instead of function keyword. It is less verbose.

  2. var is accessible throughout the program while let is local to the code block.

  3. Pure function is the one without side effects and it always behave exactly same and shouldn’t effected by side effects of other functions and change to the values of global bindings.

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. The function body of a function created this way must always be wrapped in braces, even when it consists of only a single statement.

  2. let is 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.

  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 — for example, it doesn’t read global bindings whose value might change.

1 Like