Procedural abstraction
- Procedural abstraction means hiding how a value is computed or how an effect is achieved allowing a programmer to use a method without needing to know exactly how the method is written.
- Procedural abstraction allow us to “decompose” or break down a large problem into smaller sub-problems by writing procedures that call other procedures that each solve various sub-problems.
Function
- In Javascript the main units of procedural abstraction is the function.
- We can use functions provided to us by Javascript (as in the
Math
functions such as Math.sqrt
) and also function that we write ourselves.
- We can use functions without knowing exactly how they are implemented but it is also useful to write our own functions to hide details in one part of our code even though we have to deal with those details when we write them.
- The code in one function can call other functions, allowing us to build layers of abstractions.
- A function can also call itself. This is called recursion and we’ll talk about it separately in a later unit.
Function structure
- Function parameters are variables. The scope of function parameters is the body of the function. Outside of a function the names of the function’s parameters have no meaning.
- Because function parameters are purely local to the function, different functions can have parameters with the same names without any problem.
- Functions that take no arguments are written with an empty parameter list consisting of just a pair of parentheses.
- The body of the function follows the parameter and is usually enclosed in a pair of
{}
s.
- Within the body of a function we can declare other local variables whenever we need them. Like parameters, the scope of these variables is limited to the body of the function and the names have no meaning outside of the method.
- Code within the body of a function can refer to the parameters declared in the funtion’s parameter list any other local variables declared in the function.
Variable scope
- Every variable has a scope which is the part of the program in which the name is meaningful. This will become more important in future units when we talk about writing methods and control consturcts.
- In general we want to minimize the scope of variables, i.e. declare them as near to where they need to be used, so we don’t have to look at too much code to understand how they are used.
- Variables also have “extent” which is how long they exist in time. For intstance the parameters of a method are scoped to the body of the method (can only be referred to within the text of the method) and have exent of the duration of the method call—once the method returns the variables no longer exist. (Though when the method is called again, a new set of variables will come into existance and last for as long as that method call takes.)
Function calls
- Function calls consist of the name of the function and a possibly empty argument list consisting of a comma-separated list of expressions, e.g.
foo(10, "some string")
.
- The expressions in the argument list are evaluated to get the values that are passed as the arguments to the function. When the function runs the parameters declared in function’s parameter list are initialized to the values of the corresponding arguments, with the first parameter getting the value of the first argument, and so on.
- Arguments are passed to functions by copying values. This means that there is no way for code in a method to change the value of local variable in the code that called it.
- Recall, however, that the value of a reference type is the reference; this means code in a function that is passed a reference to a mutable object can use the reference to alter the state of the object and those changes will be visible to other code that also has a reference to that object. As a matter of good coding practice, functions should not modify mutable objects passed as arguments unless doing so is an inherent part of the function’s purpose.
- Calling a function causes the flow of control to jump to the start of the function after initializing the parameters. When the function returns, the flow of control returns to the point where the function was called. No code executes in a function after a
return
statement.\note{Technically, that’s not quite true; there are constructs that allow us to guarantee that certain bits of code execute just before returning from a method bet we’re not going to cover them.}
- The parameters of a function and other local variables exist only until the function returns.
- Values are returned from a function using a
return
statement consisting of the keyword return
followed by an expression that evaluates to a value which is then returned from the function.
- The value returned by a function is copied, just like method arguments. Again, this means a reference returned by a method can be used to modify the object it references.
- A function call is an expression whose value is the value returned by the function. (Some functions, which are intended to be called for their side effects rather than for a value, return
undefined
so aren’t very useful as expressions.)
- A function with no
return
statement will return undefined when it reaches the end of its code.