Procedures and Procedures Calls Scopes and Closures

CSC 206 Foundations of Sequential Programming DISCLAIMER

The contents of this document are intended for leaning purposes at the undergraduate level. The materials are from different sources including the internet and the contributor do not in any way claim authorship or ownership of them. Content

• Procedures and procedures calls • Describing and • Describing closures Describing Procedures

In , a procedure is a set of coded instructions that tell a computer how to run a program or calculation. Many different types of programming languages can be used to build a procedure.

Depending on the programming language, a procedure may also be called a , subprogram or function. Examples

JavaScript: function factorial(n) { if (n == 0) { return 1; } else { return n * factorial(n - 1); } }

VisualBASIC: Function factorial(n as integer) if n = 0 then factorial = 1 else factorial = n * factorial(n - 1) End if End Function Procedures are the basic building blocks of programs. They are small sections of code that are used to perform a particular task, and they are used for two main reasons.

The first reason is that they can be used to avoid repetition of commands within the program. If you have operations that are performed in various different parts of the program, then it makes sense to remove the repeated code and create a separate function or procedure that can be called from those places instead. Not only will this reduce the size of your program, but it will make the code easier to maintain, and it will remove the possibility that some of the code segments are updated, but not others.

The second reason for careful use of procedures is to help define a logical structure for your program by breaking it down into a number of smaller modules with obvious purposes. Depending on the programming language you use, you can also compile a library of functions and procedures and import them for use in other programs. Procedure calls A procedure call causes code for the procedure to be executed by the processor. Consider the following code: int add(int a, int b) {

return a + b;

} void main() {

int result = add(2, 3);

printf(“%\n”, result);

} Another example

Any given procedure might be called at any point during a program's execution, including by other procedures or itself, fn italic() { let i = 6; } fn bold() { let a = 5; let b = 100; let c = 1; italic(); } fn main() { let x = 42; bold(); } Definition of Scope/Visibility The scope of a variable describes where in a program's text the variable may be. The scope of a variable is actually a property of the name of the variable.

The scope of a variable is the portion of the program code for which the variable's name has meaning and for which the variable is said to be "visible". Scope/visibility cont..

Scope can also refer to the ability to access the contents of a particular variable at a certain point in the program.

Usually, a variable's scope is determined by its enclosing . A block can be enclosed explicitly in some languages (begin/end, {/} pairs), while in others the block is implicit.

Almost all languages have an overall bounding scope, and if a program declares a variable in this scope, it is known as a .

A , by contrast, is one that is only visible, or "in scope", within its block. Scope/visibility cont..

Consider this code: integer globalVariable globalVariable = 5 begin integer localVariable localVariable = 7 globalVariable = 8 end

globalVariable is declared outside of any block, so is in the (implicit) global scope, making it a global variable. localVariable is declared in the begin/end block, limiting its scope to that block. Nesting Nesting is a term used to describe the placement of one or more objects within another object. For example, when referring to a computer, nesting may refer to inserting a graphic image into a word processor.

With computer programming, A nested function is a function contained inside of another function within the source code of a program. An example of this in JavaScript is shown below. function outerFunction() { function innerFunction() { // code } } Typically, this nesting is done to limit the scope of the inner function (the inner function can only be called by the function containing it or another function within the containing function). In the example above, outerFunction() could call innerFunction(), but innerFunction() could not be called from the global scope or from any function outside of outerFunction().

This type of nesting helps to keep the inner functions from being altered or overwritten by code in the global scope or code within functions that are outside of the containing function. Describing Closure

A closure is a persistent local variable scope

A closure is a persistent scope which holds on to local variables even after the code execution has moved out of that block. Languages which support closure (such as JavaScript, Swift and Ruby) will allow you to keep a reference to a scope (including its parent scopes), even after the block in which those variables were declared has finished executing, provided you keep a reference to that block or function somewhere.

The scope object, and all its local variables, are tied to the function, and will persist as long as that function persists.

This gives us function portability. We can expect any variables that were in scope when the function was first defined to still be in scope when we later call the function, even if we call the function in a completely different context. For example

Here's a simple example in JavaScript that illustrates the point: outer = function() { var a = 1; var inner = function() { console.log(a); } return inner; // this returns a function } var fnc = outer(); // execute outer to get inner fnc();