Control Structures Flashcards
What are Expressions?
Type of program phrase that returns a value but does not change the state or perform a side-effect.
Functional programming only uses expressions!
What are Commands?
Type of program phrase that does not return a value but changes the state or performs a side effect
Describe the following notations: function application notation prefix notation infix - notation postfix - notation
FAN: +(a, b)
prefix: + a b
infix: a + b
postfix: a b +
What is the difference between eager evaluation and lazy evaluation?
Eager evaluation is evaluating an application of an operator to expressions by first evaluating the arguments and then applying the correct operation to the values
Lazy evaluation is to start by applying the operation and evaluate the arguments only as/if you need them
Two models of variables
A variable points to a value
A variable points to a location in the heap containing a value (the variable being a pointer and all…)
What are l and r expressions?
l-expressions denote locations, r-expressions are values storable in those locations
An assignment is executed by…
evaluating the LHS to an l-value
evaluating the RHS to an r-value
overwriting the content of the l-value with the r-value
What are the basic structured forms of control of a high-level imperative language?
Sequencing
Conditionals
Loops
Recursion is an alternative to loops for making a programming language…
Turing-complete
A function call is tail recursive if…
a call to function g in a function f is tail recursive if f returns the result of g without performing any further computation
A function is tail recursive if all calls in it are tail calls
A call to a tail recursive function only needs a single frame
What is a subprogram?
A function : meant to return a value and typically does not change the outside environment
A procedure: not meant to return a value, but changes the outside environment
Describe Call By Value
- input only communication
- parameter can be any expression
- On the call, the parameter is evaluated to an r-value that is associated to the formal parameter
- On the return, the formal parameter is destroyed.
Describe Call By Reference
- gives bidirectional communication
- The parameter must be an l-expression
- On the call it is evaluated to an l-value and that is associated to the formal parameter, aliasing it
- On the return. the association of the formal parameter to this l-value is destroyed, but the l-value remains
Describe Call By Constant
- If the formal parameter is not modified in the body of the procedure, then call by value can be implemented by call by reference
- The l-value of the actual parameter is passed to the procedure, the r-value is not, it is not copied to the frame of the call
- Semantically this is just call by value but implemented differently
Describe Call By Result
- Output only communication
- The actual parameter must be an l-expression, on the call it is evaluated to an l-value.
- on the return the current r-value of the formal parameter is stored in this l-value