Which of the following correctly defines a procedure in computer programming? choose all the apply.

Functions "Encapsulate" a task (they combine many instructions into a single line of code). Most programming languages provide many built in functions that would otherwise require many steps to accomplish, for example computing the square root of a number. In general, we don't care how a function does what it does, only that it "does it"!

When a function is "called" the program "leaves" the current section of code and begins to execute the first line inside the function. Thus the function "flow of control" is:

  1. The program comes to a line of code containing a "function call".
  2. The program enters the function (starts at the first line in the function code).
  3. All instructions inside of the function are executed from top to bottom.
  4. The program leaves the function and goes back to where it started from.
  5. Any data computed and RETURNED by the function is used in place of the function in the original line of code.

Why do we Write Functions?

  1. They allow us to conceive of our program as a bunch of sub-steps. (Each sub-step can be its own function. When any program seems too hard, just break the overall program into sub-steps!)

  2. They allow us to reuse code instead of rewriting it.

  3. Functions allow us to keep our variable namespace clean (local variables only "live" as long as the function does). In other words, function_1 can use a variable called i, and function_2 can also use a variable called i and there is no confusion. Each variable i only exists when the computer is executing the given function.

  4. Functions allow us to test small parts of our program in isolation from the rest. This is especially true in interpreted langaues, such as Matlab, but can be useful in C, Java, ActionScript, etc.

Steps to Writing a Function

  1. Understand the purpose of the function.
  2. Define the data that comes into the function from the caller (in the form of parameters)!
  3. Define what data variables are needed inside the function to accomplish its goal.
  4. Decide on the set of steps that the program will use to accomplish this goal. (The Algorithm)

Parts of a "black box" (i.e., a function)

Functions can be called "black boxes" because we don't need to know how they work. Just what is supposed to go into them, and what is supposed to come out of them.

When defining a program as a black box, we must describe the following attributes of the function.

Note: most documentation systems are just this, the attributes of a function with no code associated with it.

  1. The Name - describes the purpose of the function. Usually a verb or phrase, such as "compute_Average", or just "average".

  2. The Inputs - called parameters. Describe what data is necessary for the function to work and gives each piece of data a Symbolic Name for use in the function.

  3. The Calculation - varies for each function

  4. The Output - Usually one (but sometimes zero or sometimes many) values that are calculated inside the function and "returned" via the output variables.

Function Workspace

Every function has its own Workspace. This means that every variable inside the function is only usable during the execution of the function (and then the variables go away).

Having a separate "workspace" for each function is critical to proper software engineering. If every function shared every variable in an entire program, it would be easy to inadvertently change the values of variables that you shouldn't. Further, it would be hard to remember what "names" have been used elsewhere, and coming up with new names to represent similar ideas would be challenging.

A side-effect of function variables not existing after the end of the function is that the only way to get information "out" of a function is by "returning" that information via the output of the function.

Additionally, the function can only "see" the information that is "passed" to it via parameters. Thus the only way information can get "in" to the function is by using parameters.

Note: In certain object oriented languages (e.g., C++, Java, ActionScript), a function can also see all of the variables associated with its containing object.

Formal vs. Actual Parameters

When we create a function, it should represent a "generic" action that can be applied in many circumstances. For example, if we want to find the average grade, it doesn't matter if it is on a test, or on a quiz, or an assignment, or a midterm, etc... given any list of grades we can compute an average!

...but if it can be any list of grades, how do we know what the list of grades will be called? The answer: we don't care. You, the programmer of the function, provide your own name for the data. This is much the same as when a sales person calls you and reads a script trying to sell something to you, they say: Dear _insert customer name here_, let me sell you our wonderful product.

When writing a function, the programmer must provide a blank to plug in what ever data is of current interest; the blank should have a good symbolic name saying what it will represent. Here is a pseudocode function example:

function average_grade( list_of_grades ) .... end function

Inside the average_grade function, the name list_of_grades will be used in place of whatever variable some other user has stored his or her grades in. Thus to call the function, I might write:

// This some other code (not the function code) midterm_grades = ... // create array of grades print "the average of the midterm was" print average_grade( midterm_grades )

In "My" code, the grades are stored in the variable, "midterm_grades". Inside the function, the grades are stored in the variable "list_of_grades". Thus, during the execution of the program, both names will refer to the same thing but at different times.

The parameter "list_of_grades" is called a Formal paramater; again, this just means a place holder name for any possible set of grades.

The variable midterm_grades is the Actual paramater. This means "what is actually used" for this call to the function, such as [90, 100, 70];

Nesting Functions?

Often the question comes up: Can we Nest functions (can we place the code for one function inside another function)? The answer is a resounding: NO!

It should be noted, that we can utilize (call) other functions inside a function, but we cannot create the recipe for a new function there.

Note: Sadly this question often comes up when we are careless with our indentation and syntax, and accidentally forget to end our first function, thus, to the computer, placing our second function inside our first function:

// Here is pseudocode of the correct layout of two functions function1() { code; code; code; } function2() { code; code; code; } // Here is pseudocode of the INCORRECT layout of two functions function1() { code; code; code; // forgot to end the function properly with function2() { code; code; code; } } // accidentally ended the first function AFTER the second! Back to Topics List

A Sub procedure is a series of Visual Basic statements enclosed by the Sub and End Sub statements. The Sub procedure performs a task and then returns control to the calling code, but it does not return a value to the calling code.

Each time the procedure is called, its statements are executed, starting with the first executable statement after the Sub statement and ending with the first End Sub, Exit Sub, or Return statement encountered.

You can define a Sub procedure in modules, classes, and structures. By default, it is Public, which means you can call it from anywhere in your application that has access to the module, class, or structure in which you defined it. The term method describes a Sub or Function procedure that is accessed from outside its defining module, class, or structure. For more information, see Procedures.

A Sub procedure can take arguments, such as constants, variables, or expressions, which are passed to it by the calling code.

Declaration syntax

The syntax for declaring a Sub procedure is as follows:

[modifiers] Sub SubName[(parameterList)] ' Statements of the Sub procedure. End Sub

The modifiers can specify access level and information about overloading, overriding, sharing, and shadowing. For more information, see Sub Statement.

Parameter declaration

You declare each procedure parameter similarly to how you declare a variable, specifying the parameter name and data type. You can also specify the passing mechanism, and whether the parameter is optional or a parameter array.

The syntax for each parameter in the parameter list is as follows:

[Optional] [ByVal | ByRef] [ParamArray] parameterName As DataType

If the parameter is optional, you must also supply a default value as part of its declaration. The syntax for specifying a default value is as follows:

Optional [ByVal | ByRef] parameterName As DataType = defaultValue

Parameters as local variables

When control passes to the procedure, each parameter is treated as a local variable. This means that its lifetime is the same as that of the procedure, and its scope is the whole procedure.

Calling syntax

You invoke a Sub procedure explicitly with a stand-alone calling statement. You cannot call it by using its name in an expression. You must provide values for all arguments that are not optional, and you must enclose the argument list in parentheses. If no arguments are supplied, you can optionally omit the parentheses. The use of the Call keyword is optional but not recommended.

The syntax for a call to a Sub procedure is as follows:

[Call] SubName[(argumentlist)]

You can call a Sub method from outside the class that defines it. First, you have to use the New keyword to create an instance of the class, or call a method that returns an instance of the class. For more information, see New Operator. Then, you can use the following syntax to call the Sub method on the instance object:

object.MethodName[(argumentList)]

Illustration of declaration and call

The following Sub procedure tells the computer operator which task the application is about to perform, and also displays a time stamp. Instead of duplicating this code at the start of every task, the application just calls tellOperator from various locations. Each call passes a string in the task argument that identifies the task being started.

Sub tellOperator(ByVal task As String) Dim stamp As Date stamp = TimeOfDay() MsgBox("Starting " & task & " at " & CStr(stamp)) End Sub

The following example shows a typical call to tellOperator.

tellOperator("file update")

See also