Binding, Functions and Control Flow - Reading Assignment

Hi @PAT22,

Some nice answers :ok_hand:

Just a few observations…

Yes…and a side effect can also be some kind of meaningful action produced by a piece of code (such as a function), and which occurs externally (outside of the program), e.g. logging to the console, or a pop-up prompt or alert.

b) :+1:
a) This is not a function: it’s a binding. An example of a function that produces a side effect is …

console.log(18 / 3);    // => 6

Yes … and I would add that the decision about which ā€œbranchā€ to take is based on whether a condition (a Boolean expression) evaluates to true or false . This is key to how conditional execution operates within a program.

2 Likes
  • What is an expression?
    An expression is a ā€œsentence fragment,ā€ code that produces some kind of a value.

  • What is a binding?
    A binding is a created term that defines and holds a value for use throughout a program. In the statement ā€œlet brian = really + confusedā€ ā€œbrianā€ becomes the binding with is ā€œreally confusedā€ which can be called up later (and most certainly will).

  • What is an environment?
    An environment is all the bindings and their values that exist in a program at any given moment.

  • What is a function?
    A function is a block of code that executes a specific task or calculates a value when invoked

  • Give an example of a function.
    Search engine results (?), Currency conversion, password call. I am playing with the code but not there yet.

  • What is a side effect?
    A side effect is something that happens on top of the intended purpose of a function, including triggering other processes/modifying existing variables.

Interesting that the textbook seems to like side effects whereas most recent internet chatter advocates ā€œpurityā€ with no side effects

  • Give an example of a function that produces a side effect and another function that produces a value.
    Basic math functions produce values with no side effects

Easy to find a list of side effects in javascript (writing to the screen, triggering external processes, modifying external variables. etc.). At this point more difficult to understand/document example code. Seems like many of these side effects would be intended (?)

  • What is control flow?
    The order of execution of statements in a program, top to bottom in the code.

  • What is conditional execution?
    When control flow is not straight forward top to bottom, when the execution of statements in a program is contingent on other factors

  • What kind of keyword do you need to use to invoke conditional execution?

if

1 Like

Thanks @jon_m

Great informational input - got it!

1 Like
  1. An expression is a fragment of code that produces a value.
  2. A binding is a variable that allows developers to hold values. Bindings are like tentacles - they grasp rather than contain values, and two bindings can refer to the same value
    Example: let caught = 5 * 5; (caught is the binding and 5 * 5 is the expression).
  3. The environment is a collection of bindings and their values that exist at a given time
  4. A function is a piece of program wrapped in a value
  5. Example of a function: prompt(ā€œEnter passcodeā€); where ā€œpromptā€ is a binding that holds a function that shows a dialog box asking users to enter a passcode.
  6. A side effect is a dialog box or written text that gets displayed when a function is invoked
  7. Example of a function that produces a side effect: prompt(ā€œEnter passcodeā€); Example of a function that produces a value: console.log (Math.min(2,4) +100 will output 102 (where Math.min takes any amount of numbers and returns the lowest value)
  8. Control Flow provides direction to programs to execute statements from the top down
  9. Conditional execution tells the program which code to execute depending on whether certain conditions hold
  10. The if keyword is used to invoke conditional execution - it executes or skips a statement depending on the value of a Boolean expression
1 Like
  • What is an expression? A fragment of code that produces a value.

  • What is a binding? To catch and hold values. A single let statement may define multiple bindings. The definitions must be separated by commas. Words with a special meaning, such as let, are keywords, and they may not be used as binding names.

  • What is an environment? The collection of bindings and their values that exist at a given time.

  • What is a function? A function is a piece of program wrapped in a value. Executing a function is called invoking, calling, or applying it.

  • Give an example of a function. prompt(ā€œEnter passcodeā€);

  • What is a side effect? Showing a dialog box or writing text to the screen. A lot of
    functions are useful because of the side effects they produce.

  • Give an example of a function that produces a side effect and another function that produces a value prompt(ā€œEnter passcodeā€); has the side effect of a prompting dialogue box.
    console.log(Math.max(2, 4)); produces no side effect, though produces a value.

    .

  • What is control flow? When your program contains more than one statement, the statements are executed as if they are a story, from top to bottom

  • What is conditional execution? Determines which path to take based on conditions met.

  • What kind of keyword do you need to use to invoke conditional execution? "if"

1 Like
  1. What is an expression?

Anything that produces a value or, to put it another way, any unit of code that can be evaluated to a value.

  1. What is a binding?

It is what we define as a connection to a value. By doing that, values can be held and remembered by the program. This link or connection isn’t rigid and can be redefined again. The binding will always directly influence the expression output but is never fully discernible in it.

  1. What is an environment?

The place where all bindings and values are collected at a given time. This collection enables interactions with the surrounding system in the first place and hence is basically built into the language standard. Thus, the environment is never empty.

  1. What is a function?

A written piece of program or subprogram in a written block of code designed to do something specific. Compared to statements which in most cases will not return values, functions usually do because they themselves are values. Further, when inside an object, functions are called object properties and object methods.

A function can look something like this:

function myFunction (parameter1, parameter2, ...) { // This line represents its signature: "function" is a reserved word and acts as the constructor while "myFunction" and the also omittable parameters in parentheses are customized by the programmer.
    return parameter1 / parameter2 ...			    // This line represents its instruction: "return" is a reserved word and is in this representation the action the programmer chooses to be executed in conjunction with the consecutive code. Here, "parameter1" is divided by "parameter2".
}							                        // This block of code is called the function definition, function declaration or function statement. The segment, following the constructor in the signature and ending with the closing curly bracket, is called the function object. Until here no action is performed, the function has yet to be called or invoked.
myFunction (argument1, argument2, ...)			    // The call or invokement of the function so its instruction gets performed. The arguments, which in the signature are aliased by the parameters, are the values passed into the function. The function call operator () refers to the result and making a call without it returns only the function object.
  1. Give an example of a function.

Example A:

function myMathFunction(a, b) {
	return a / b;
}					
myMathFunction(36, 6);					// By dividing 36 by 6 the result of 6 gets returned.

The same function definition with arrow function syntax (no constructor, return keyword and curly brackets needed - only possible if the function has only one statement):

myMathFunction = (a, b) => a / b;

Example B:

function welcome() {	
	alert("Greetings from 2084, Earthling");
}
welcome();						             // An alert box with the specified string in the instruction gets displayed.

The same with the shorter arrow function syntax:

welcome = () => "Greetings from 2084 Earthling";

  1. What is a side effect?

An indirect action that will be executed and is observable to the outside world. For example to display something or change the machine’s internal state. These are intended side effects while they can also be produced unintended when the function is not pure or deterministic in that it will not return the same value every time of its execution.

  1. Give an example of a function that produces a side effect and another function that produces a value.

Side effect:

prompt ("Enter your E-Mail");

Value:

Math.round (1.618);

  1. What is control flow?

The way how the code is interpreted according to the programming language protocol. Usually this reading happens like in the English language from left to right and from top to bottom. Exceptions like hoisting exist, where the flow can take turns by chronologically referencing to something that is discernible in a later appearing code segment.

  1. What is conditional execution?

A branching in the process that happens based on a given situation.

  1. What kind of keyword do you need to use to invoke conditional execution?

if

1 Like
  1. An Expression returns a Value

  2. A Binding, (or Variable) catches & holds a Value

  3. An Environment is a collection of Bindings & Values that exist within them

  4. A Function is a part of a program wrapped inside a Value

  5. " prompt(ā€˜Enter Passcode’); " (without outer quotations)

  6. A Side Effect is where a Return statement is returned from the control finding a statement, where a Function produces a Value

  7. " alert(ā€˜hello world’); " (without outer quotes) produces a pop-up

  8. Control Flow is where a each statement is read from top to bottom

  9. Conditional Execution follows a branch, depending on the value

  10. " if " (without outer quotation marks)

1 Like
  1. What is an expression?

An expression can be classified as a number of things, for example: it is a value (whether written or produced) or series of values with an operator applied to them, even a single value with an operator applied to it and they can have expressions within expressions, for example an expression, within parentheses, within an expression – (5+5)*2 [the (5+5) is an expression as well as the whole equation being an expression].

  1. What is a binding?

A binding, which is a variable, is a way to store a value. By defining a binding name and assigning it a value, this value can be stored in the computer’s memory to be called on throughout the program. Once a binding, or variable, is defined its name can be used as an expression and the value of that expression is the value that is bound to it

  1. What is an environment?

The environment is basically the current state of the program’s bindings and associated values stored in memory at any given time. Even before you allocate values to bindings, the environment is not empty as the language standard contains bindings.

  1. What is a function?

A function performs an action on a value or expression. It is a piece of the program that is defined by a value - invoking, calling or applying that value (the name of the function) will run that piece of program and output a desired result based on the parameters and arguments of the function.

  1. Give an example of a function.

function MathProblem(x*y+z)

  1. What is a side effect?

A side effect is the modification of a value outside of the scope of the function or anything that is input on the screen – like drawing or writing something. Any function that changes anything, or produces any result, other than what was intended for the function to execute or compute could be classified as a side effect.

  1. Give an example of a function that produces a side effect and another function that produces a value.

This side effect thing had me a little stumped. I understand I could have gone for the obvious examples and simply returned, for the side effect example, either the prompt function or alert function, but I wanted to understand the whole side effect in terms of local and global variables a little better. To that end, I think the following examples illustrate a function that could result in a side effect but still returns a value and then a function that only returns a value and should have no side effects. If I still have not quite understood this, can someone please let me know….

Side effect example:

let a = 4;

function incrementalCount() {

return a++;
}

In this value, the variable a is incremented by 1 and changed from a value of 4 to a value of 5, therefore a side effect of changing the value of a variable has occurred.

Return Value example:

function math(a,b) {

let a = 4;

let b = 5;

return a*b;
}

In this example, the variables are defined within the function and will return a value of 20 without changing anything or having any influence outside of the function.

  1. What is control flow?

Control flow is basically the way the program is read. Assume program coding tells a story, control flow then, is the way the story is told. The story is more like one of those novels that allow you as the reader to make decisions (eg a scary monster jumps out, you can choose to fight the monster or run away – if you chose to fight, turn to page x, if you choose to run away, turn to page y). So control flow, depending on the commands, tells the program what to do and in what order.

  1. What is conditional execution?

Conditional execution is where you want the program to do something if one or more conditions are met.

  1. What kind of keyword do you need to use to invoke conditional execution?

To invoke conditional execution, the keyword ā€˜if’ is used. Other keywords that can accompany is are ā€˜else’ and combining them to form ā€˜else if’. Switch is another form of conditional execution.

1 Like

Hi @BrianR,

Some good answers :ok_hand:

Here are some additional comments to help clarify a few things…

Here, you could have given a simpler example e.g.

alert("This is an alert!");
prompt("Enter your reference number");
console.log(6 + 14);     // => 20

Purity is something to always bear in mind, and aim for wherever possible, but there are lots of situations where a side effect is either unavoidable or even desirable (which also answers your following question).

Yes :+1:   e.g.

Math.max(7, 9);      // => 9

Above, I’ve already given 3 code examples of functions that produce side effects.

We need to be more specific here. Conditional execution is where the program’s control flow presents a Boolean expression and, depending on whether this expression evaluates to the Boolean value true or false (i.e. whether the condition is met or not), only one of two alternative branches of code will be executed next. Where a series of consecutive Boolean expressions are used, only one of several alternative branches will end up being executed.

2 Likes
  1. It is a value produced by code
  2. A binding gives a special word a specific value .
  3. An environment is the collection of bindings and their values .
  4. A function is a piece of program wrapped in a value .
  5. An example is a prompt that pops up requiring an answer .
  6. Side affects are when the dialogue box shows or text written on the screen .
  7. A function that produces a value would be an addition function that gives you the sum . An alert function produces a side affect .
  8. Control flow is the way the program is read like a story from top to bottom .
  9. Conditional execution would send the user on different paths depending on the situation or answer they give .
  10. The keyword ā€œifā€ us used to invoke conditional execution .
1 Like
  1. What is an expression?
    An expression resolves to a value. It is any code that produces a value.

  2. What is a binding?
    A binding is a variable used to catch or hold a value.
    var, let and const are examples of a binding.

  3. What is an environment?
    An environment is the term given to a collection of variables that exist at a given moment in time.

  4. What is a function?
    A function is a piece of code wrapped in a value. the function is something that makes writing and reading of code much cleaner.

  5. Give an example of a function.
    One of the first functions we learned was the alert() function. The code that contains the instruction to perform the task of the box appearing displaying the value we input into the function is neatly represented by the function value alert().

  6. What is a side effect?
    A side effect is the result of a function like writing text to the screen.

  7. Give an example of a function that produces a side effect and another function that produces a value.
    The alert() or prompt() functions are examples of functions that have side effects. The alert() also returns value.

  8. What is control flow?
    the order in which statements are read and executed. Simple value in value out is a straight line control flow.

  9. What is conditional execution?
    This is an environment where the flow can take different paths depending upon the input value.

  10. What kind of keyword do you need to use to invoke conditional execution?
    if
    if else
    else

1 Like

Nice answers @Jaysun :ok_hand:

Just one clarification with Q7…

Just to be clear…
Math.max(2, 4) is a function that produces a value ( 4 )
BUT…
…the console.log() that it’s wrapped in is a function that produces a side effect, the side effect being printing the value produced by the inner function ( 4 ) to the console.

1 Like

Excellent answers @hubitutl, with some great detail :+1:

A few observations and comments…

Arrow function examples

We would normally declare these with let or const  i.e.

const myMathFunction = (a, b) => a / b;
const welcome = () => prompt("Greetings from 2084 Earthling");

I’ve also added in the function call prompt() , which you’d missed out, and inserted the greeting as its argument.

Yes … and I would add that the decision about which branch to take is based on whether conditions (Boolean expressions) evaluate to true or false . This is key to how conditional execution operates within a program.

1 Like

Hi @sharpest,

Mainly good answers :ok_hand:

Just a few observations to clarify some points…

To be honest, I don’t understand what you mean here. I don’t think it’s right anyway :wink:

One type of side effect is a meaningful action produced by a piece of code (such as a function), and which occurs externally (outside of the program) e.g. logging a value to the console, or triggering a pop-up dialogue box.

A return statement returns a value from a function, and this is not a side effect.

This is a good example of a function that produces a side effect.
The question also asks for an example of a function that produces a value e.g.

Math.min(62, -9)     // => -9

Yes…and more specifically, the decision about which branch to take is based on whether conditions (Boolean expressions) evaluate to true or false (the value that you mention). This is key to how conditional execution operates within a program.

2 Likes
  1. A fragment of code - combination of symbols that represents value.
  2. A way to link variable to it’s value.
  3. Data structure - collection of bindings and their values that creates storage space.
  4. A fraction of code attached to it’s value and being used to run a program.
  5. Alert(ā€œWholeness!ā€)
  6. A dialog box or writing text to the screen.
  7. console.log(ā€œHello!ā€); // prints ā€œHello!ā€, but returns undefined
    Function myFunction(a, b) {
    return a * b;
    // Function returns the product of a and b
    }
  8. When a program contains more than one statement and being executed from top to bottom.
  9. A fork in the control flow appears when program decides to execute code partially if conditions agree with the statement.
  10. If…else
1 Like
  • What is an expression?
    A fragment of code that produces a value.
  • What is a binding?
    Binding is a way that enable program to hold a value in the internal state.
  • What is an environment?
    A collection of bindings and their values that exists at a given time.
  • What is a function?
    A function is a piece of program wrapped in a value.
  • Give an example of a function.
    alert(ā€œLondon bridge is falling down !ā€)
  • What is a side effect?
    Showing a dialogue box or writing text to a screen.
  • Give an example of a function that produces a side effect and another function that produces a value.
    alert(ā€œHello Worldā€); console.log(Math.max(5,7));
  • What is control flow?
    The order of program execute code
  • What is conditional execution?
    When using a logical operator to determine whether or not to execute certain codes
  • What kind of keyword do you need to use to invoke conditional execution?
    If
1 Like

Thanks jon_m. I think I’ve lost to focus there for some time.

1 Like

What is an expression?a fragment of code that produces a value

What is a binding? catching or holding values in a program

What is an environment? collections of binding and their values

What is a function? a piece of program wrapped in a value

Give an example of a function.prompt

What is a side effect? the outcome or whats produced from a function

Give an example of a function that produces a side effect- prompt("Enter passcode")
and another function that produces a value.- Math.max

What is control flow? statements in a program that run from top to bottom

What is conditional execution? when you use the "if" keyword and makes the program run       based on the situation or "conditions"

What kind of keyword do you need to use to invoke conditional execution? "if"
1 Like

Hi @cryptocrom

Some good answers :ok_hand:

Here are some additional comments to help clarify a few things…

That function will required 2 parameters (ā€œmath(a,b)ā€), so it’s pointless for the function to ask for 2 parameters that are going to change inside the function logic, Here is a syntax example of what i mean:

//this is your function
function math(a,b) { //2 parameters required in order to function

let a = 4; //set a = 4

let b = 5; //set b = 5

return a*b; //return the result of the operation/logic = 20
}

if i call you function with parameters a= 10 & b =20, the result still will be 20

//declare & binding new values
var a = 10;
var b = 20;

math(a, b); //invoke the function with parameters
//the result will still be 20.

Hope this gives you a clear view of the subject, keep learning! :

If you have any doubt, please let us know so we can help you!

Carlos Z.

1 Like
  • What is an expression?
    Fragment of code that produces a value
  • What is a binding?
    It combines the values and holds them together. For example let color=blue, then it recalls blue when color is inserted.
  • What is an environment?
    It’s a collection of bindings and their values.
  • What is a function?
    A piece of program wrapped in value.
  • Give an example of a function.
    Asking for a user to enter specific value, for example log-in name
  • What is a side effect?
    Displaying text on page or showing a display box is a side effect and it’s produced by function.
  • Give an example of a function that produces a side effect and another function that produces a value.
    An example of function with side effect would be log-in name or password. Function that produces a value would be identifying the largest number in the chain of valued provided.
  • What is control flow?
    It runs the program in the line of code from top to bottom and it’s useful when there are multiple statements (2 or more). It will execute the top one first, then work down the list.
  • What is conditional execution?
    It creates a fork in the road based on the input. Instead of executing the entire code, it operates based on the condition. For example, if number<100 then display screen blue, if number>100 then display screen red, if number=100 then display black screen
  • What kind of keyword do you need to use to invoke conditional execution?
    IF
1 Like