46
Subprograms Functions Procedures

Subprograms Functions Procedures. Subprograms A subprogram separates the performance of some task from the rest of the program. Benefits: “Divide and

Embed Size (px)

Citation preview

Page 1: Subprograms Functions Procedures. Subprograms A subprogram separates the performance of some task from the rest of the program. Benefits: “Divide and

Subprograms

Functions

Procedures

Page 2: Subprograms Functions Procedures. Subprograms A subprogram separates the performance of some task from the rest of the program. Benefits: “Divide and

Subprograms

A subprogram separates the performance of some task from the rest of the program.

Benefits:• “Divide and conquer“

– Tasks are broken down into pieces, each of which can be programmed separately. This is modularisation.

• A subprogram can implement a piece of the calculation that is duplicated in different places.– Rather than duplicating the same code you can simply

'call' the subprogram.

Page 3: Subprograms Functions Procedures. Subprograms A subprogram separates the performance of some task from the rest of the program. Benefits: “Divide and

Subprogram Statements

A subprogram is a section of code with a name. That name can be used as a statement in another part of the program.

When the name is encountered, the processing in the other part of the program halts while the named code is executed. This is an example of control coupling.

Page 4: Subprograms Functions Procedures. Subprograms A subprogram separates the performance of some task from the rest of the program. Benefits: “Divide and

Subprogram Statements

Page 5: Subprograms Functions Procedures. Subprograms A subprogram separates the performance of some task from the rest of the program. Benefits: “Divide and

Subprogram Statements

There are times when the calling unit needs to give information to the subprogram to use in its processing.

A parameter list is a list of the identifiers with which the subprogram is to work, along with the data types of each identifier.

These are placed in parentheses beside the subprogram name.

Page 6: Subprograms Functions Procedures. Subprograms A subprogram separates the performance of some task from the rest of the program. Benefits: “Divide and

Parameters

In general, a parameter is any factor that defines a system and determines (or limits) its performance.

It is useful, however, to distinguish between those that are used in the definition of the subprogram and those that are used by the calling program.

Page 7: Subprograms Functions Procedures. Subprograms A subprogram separates the performance of some task from the rest of the program. Benefits: “Divide and

Parameters

The parameters in the declaration of a subprogram are formal parameters.

Formal - relating to or involving outward form or structure.

The parameters in the statement that calls a subprogram are actual parameters.

Actual - being, existing, or acting at the present moment.

Page 8: Subprograms Functions Procedures. Subprograms A subprogram separates the performance of some task from the rest of the program. Benefits: “Divide and

Parameters

To avoid confusion between formal and actual parameters, and to shorten our references to them, we will adopt a different set of terms…

Page 9: Subprograms Functions Procedures. Subprograms A subprogram separates the performance of some task from the rest of the program. Benefits: “Divide and

Parameters

Parameters: Identifiers listed in parentheses beside the subprogram declaration;otherwise called formal parameters.

Arguments: Identifiers listed in parentheses on the subprogram call; otherwise called actual parameters.

Page 10: Subprograms Functions Procedures. Subprograms A subprogram separates the performance of some task from the rest of the program. Benefits: “Divide and

Subprogram Parameters

The passing of control and data between subprograms is called coupling.

Data coupling can occur in two ways:

– by value– by reference

Page 11: Subprograms Functions Procedures. Subprograms A subprogram separates the performance of some task from the rest of the program. Benefits: “Divide and

Parameters

Value parameter: A parameter that expects a copy of its argument to be passed by the calling unit.

Reference parameter: A parameter that expects the address of its argument to be passed by the calling unit.

Page 12: Subprograms Functions Procedures. Subprograms A subprogram separates the performance of some task from the rest of the program. Benefits: “Divide and

Subprogram Parameters

Coupling parameters ByVal provides the value of the parameter to the subprogram.

The subprogram can use this value in calculations, but changes will NOT be passed back. In effect, there’s a one way connection.

Coupling parameters ByRef provides the address of the parameter to the subprogram.

This creates a two way connection that carries data in both directions.

Functions…

Page 13: Subprograms Functions Procedures. Subprograms A subprogram separates the performance of some task from the rest of the program. Benefits: “Divide and

Function Subprograms

A function is a subprogram that

returns a value.

We have already used many pre-defined functions, but it is also possible to declare our own.

Page 14: Subprograms Functions Procedures. Subprograms A subprogram separates the performance of some task from the rest of the program. Benefits: “Divide and

Function Subprograms

The header of a function declaration lists the function name, the parameters that it will receive, and the data type of its result.

Private Function functionName (parameter list) _

As resultDataType‘ function body

End Function

Page 15: Subprograms Functions Procedures. Subprograms A subprogram separates the performance of some task from the rest of the program. Benefits: “Divide and

Function Subprograms

The parameter list may be empty, but the parentheses are required.

Private Function functionName () _

As resultDataType‘ function body

End Function

Page 16: Subprograms Functions Procedures. Subprograms A subprogram separates the performance of some task from the rest of the program. Benefits: “Divide and

Function Subprograms

Private Function functionName _

() As resultDataTypefunction bodyReturn expression

End Function

A function must include a Return statement to specify which value is to be returned as its result.

Page 17: Subprograms Functions Procedures. Subprograms A subprogram separates the performance of some task from the rest of the program. Benefits: “Divide and

Function Subprograms

Private Function functionName () As resultDataType function bodyReturn expression

End Function

The data type of the result is declared in the header. The expression that calculates the Return value must produce data of the specified type.

Page 18: Subprograms Functions Procedures. Subprograms A subprogram separates the performance of some task from the rest of the program. Benefits: “Divide and

Parameter Lists

The parameter list is very much like a list of declarations for variables used in the body of the function – with some important distinctions.

1. Use the keyword ByVal instead of Dim or Private

Page 19: Subprograms Functions Procedures. Subprograms A subprogram separates the performance of some task from the rest of the program. Benefits: “Divide and

Parameter Lists

2. The variable will have already been assigned the value of whatever argument was specified in the function is call. The values of arguments are passed to parameters in the order in which they appear in the list.

Page 20: Subprograms Functions Procedures. Subprograms A subprogram separates the performance of some task from the rest of the program. Benefits: “Divide and

Parameter Lists

3. The list is incomplete.Local variables needed for the function to do its job may be declared in the body of the function.

The parameter list establishes a set of data connections between the calling routine and the function.

Page 21: Subprograms Functions Procedures. Subprograms A subprogram separates the performance of some task from the rest of the program. Benefits: “Divide and

Parameter Lists

Private Function Celsius_(ByVal Fahrenheit As Single)_As Single

Return (Fahrenheit - 32) * 5 / 9

End Function

Page 22: Subprograms Functions Procedures. Subprograms A subprogram separates the performance of some task from the rest of the program. Benefits: “Divide and

Function Subprograms

Since a function produces a value, it will appear on the right side of an assignment operator.

Private Function Celsius_(ByVal Fahrenheit As Single)As SingleReturn (Fahrenheit - 32) * 5 / 9

End Function ____________________________________________________Private Sub cmdGo_Click ( )

Dim degrees As Singledegrees = txtInput.TexttxtOutput.Text = Celsius (degrees)

End Sub

Passes CONTROL

Page 23: Subprograms Functions Procedures. Subprograms A subprogram separates the performance of some task from the rest of the program. Benefits: “Divide and

Function Subprograms

The parameter names in the subprogram declaration are local and need NOT be the same as the call.

Private Function Celsius_(ByVal Fahrenheit As Single)As SingleReturn (Fahrenheit - 32) * 5 / 9

End Function ____________________________________________________Private Sub cmdGo_Click ( )

Dim degrees As Singledegrees = txtInput.TexttxtOutput.Text = Celsius (degrees)

End Sub

NOT the same

Page 24: Subprograms Functions Procedures. Subprograms A subprogram separates the performance of some task from the rest of the program. Benefits: “Divide and

Function Subprograms

When the function is called the value of its argument is passed to a formal parameter.

Private Function Celsius (Fahrenheit) As SingleReturn (Fahrenheit - 32) * 5 / 9

End Function____________________________________________________Private Sub cmdGo_Click ( )

Dim degrees As SingleDegrees = txtInput.TexttxtOutput.Text = Celsius (degrees)

End Sub

Passes data

Page 25: Subprograms Functions Procedures. Subprograms A subprogram separates the performance of some task from the rest of the program. Benefits: “Divide and

Function Subprograms

When the function is complete the result value is passed back to the calling statement.

Private Function Celsius (Fahrenheit) As SingleReturn (Fahrenheit - 32) * 5 / 9

End Function____________________________________________________Private Sub cmdGo_Click ( )

Dim degrees As SingleDegrees = txtInput.TexttxtOutput.Text = Celsius (degrees)

End Sub

Passes data

Returns result

Page 26: Subprograms Functions Procedures. Subprograms A subprogram separates the performance of some task from the rest of the program. Benefits: “Divide and

Function Subprograms

A function can have several arguments but should only produce a single value.

Private Function loops(first, last) As Integer Dim counter, loops As Integer loops = 0 For counter = first To last loops = loops + 1 Next Return loopsEnd Function

Procedures…

Page 27: Subprograms Functions Procedures. Subprograms A subprogram separates the performance of some task from the rest of the program. Benefits: “Divide and

Procedure Subprograms

A procedure is a subprogram thatperforms an action.

We have already written many procedures that are called by an event, so the syntax is not new:

Private Sub ProcedureName(argumentList)‘ procedure body

End Sub

Page 28: Subprograms Functions Procedures. Subprograms A subprogram separates the performance of some task from the rest of the program. Benefits: “Divide and

Procedure Subprograms

But it is also possible to create procedures that are not associated with control events.

Private Sub convert()

Dim degrees As Single

degrees = txtInput.Text

txtOutput.Text = (degrees - 32) * 5 / 9

End Sub

must be included

Page 29: Subprograms Functions Procedures. Subprograms A subprogram separates the performance of some task from the rest of the program. Benefits: “Divide and

Procedure Subprograms

These procedures can be called by name from another procedure.

Private Sub convert()Dim degrees As Singledegrees = txtInput.TexttxtOutput.Text = (degrees - 32) * 5 / 9

End Sub__________________________Private Sub cmdGo_Click ()

Call convertEnd Sub

Page 30: Subprograms Functions Procedures. Subprograms A subprogram separates the performance of some task from the rest of the program. Benefits: “Divide and

Procedure Subprograms

Note that procedures perform actions, so to invoke them we simply call their name.

Private Sub convert()Dim degrees As Singledegrees = txtInput.TexttxtOutput.Text = (degrees - 32) * 5 / 9

End Sub__________________________Private Sub cmdGo_Click ()

Call convertEnd Sub

Passes CONTROL

Page 31: Subprograms Functions Procedures. Subprograms A subprogram separates the performance of some task from the rest of the program. Benefits: “Divide and

Procedure Subprograms

End Sub passes control back to the calling subprogram.

Private Sub convert()Dim degrees As Singledegrees = txtInput.TexttxtOutput.Text = (degrees - 32) * 5 / 9

End Sub__________________________Private Sub cmdGo_Click ()

Call convertEnd Sub Passes CONTROL

Returns CONTROL

Page 32: Subprograms Functions Procedures. Subprograms A subprogram separates the performance of some task from the rest of the program. Benefits: “Divide and

Procedure Subprograms

Sometimes it is necessary to send data to procedures. This is accomplished with a parameter list.

Private Sub convert(ByVal degrees As Single)txtOutput.Text = (degrees - 32) * 5 / 9

End Sub______________________________________Private Sub cmdGo_Click ()

Dim temperature As Singletemperature = txtInput.TextCall convert (temperature)

End Sub

Page 33: Subprograms Functions Procedures. Subprograms A subprogram separates the performance of some task from the rest of the program. Benefits: “Divide and

Procedure Subprograms

Data is passed from the Call to the argument.

Private Sub convert(ByVal degrees As Single)txtOutput.Text = (degrees - 32) * 5 / 9

End Sub______________________________________Private Sub cmdGo_Click ()

Dim temperature As Singletemperature = txtInput.Text Call convert (temperature)

End Sub

Passes data

Page 34: Subprograms Functions Procedures. Subprograms A subprogram separates the performance of some task from the rest of the program. Benefits: “Divide and

Procedure Subprograms

Since the subprogram requires the data as input, it is passed ByVal.

Private Sub convert(ByVal degrees As Single)txtOutput.Text = (degrees - 32) * 5 / 9

End Sub______________________________________Private Sub cmdGo_Click ()

Dim temperature As Singletemperature = txtInput.Text Call convert (temperature)

End Sub

Passes data

Page 35: Subprograms Functions Procedures. Subprograms A subprogram separates the performance of some task from the rest of the program. Benefits: “Divide and

Subprogram Parameters

Often a procedure subprogram will both receive data and return data.

To return values to the calling routine, parameters must be declared as ByRef.

Page 36: Subprograms Functions Procedures. Subprograms A subprogram separates the performance of some task from the rest of the program. Benefits: “Divide and

Subprogram Parameters

Private Sub convert (ByVal F As Single,_ByRef C As Single)

C = (F - 32) * 5 / 9End Sub_______________________________Private Sub cmdGo_Click() Dim degreesF As Single Dim degreesC As Single degreesF = txtStart.Text Call convert (degreesF, degreesC) txtOut.Text = degreesCEnd Sub

Page 37: Subprograms Functions Procedures. Subprograms A subprogram separates the performance of some task from the rest of the program. Benefits: “Divide and

Subprogram Parameters

Notice how multiple arguments are passed from the Call statement:

Private Sub convert (ByVal F, ByRef C)_______________________________

Call convert (degreesF, degreesC)

Arguments are listed in the same order they appear in the procedure header, separated by commas.

Page 38: Subprograms Functions Procedures. Subprograms A subprogram separates the performance of some task from the rest of the program. Benefits: “Divide and

Subprogram Parameters

There are 2 things to note in the example:

Private Sub convert (ByVal F, ByRef C)

Page 39: Subprograms Functions Procedures. Subprograms A subprogram separates the performance of some task from the rest of the program. Benefits: “Divide and

Subprogram Parameters

There are 2 things to note in the example:

Private Sub convert (ByVal F, ByRef C)

1. More than one parameter can be used. Data is passed according to the ORDER of the parameters.

1

Page 40: Subprograms Functions Procedures. Subprograms A subprogram separates the performance of some task from the rest of the program. Benefits: “Divide and

Subprogram Parameters

There are 2 things to note in the example:

Private Sub convert (ByVal F, ByRef C)

1. More than one parameter can be used. Data is passed according to the ORDER of the parameters.

2. It is not necessary to identify the data type in the parameter list (but is good practice).

1

2

Page 41: Subprograms Functions Procedures. Subprograms A subprogram separates the performance of some task from the rest of the program. Benefits: “Divide and

Side Effects

Subprograms can produce dangerous side effects. For example:

Private Function convert(ByRef F ) As Single Return (F - 32) * 5 / 9 F = F + 50 ‘why? is unimportant End Function_____________________________________Private Sub cmdGo_Click() Dim degreesF As Single degreesF = txtIn.Text txtOut.Text = convert( degreesF ) txtIn.Text = degreesFEnd Sub

Page 42: Subprograms Functions Procedures. Subprograms A subprogram separates the performance of some task from the rest of the program. Benefits: “Divide and

Side Effects

Sub cmdGo_Click() calls the convert function, coupling degreesF with F.

Private Function convert(ByRef F ) As Single Return (F - 32) * 5 / 9 F = F + 50 End Function_____________________________________Private Sub cmdGo_Click() Dim degreesF As Single degreesF = txtIn.Text txtOut.Text = convert( degreesF ) txtIn.Text = degreesFEnd Sub

Page 43: Subprograms Functions Procedures. Subprograms A subprogram separates the performance of some task from the rest of the program. Benefits: “Divide and

Side Effects

The function calculates a value which is passed back and assigned to txtOut.Text.

Private Function convert(ByRef F ) As Single Return (F - 32) * 5 / 9 F = F + 50 End Function_____________________________________Private Sub cmdGo_Click() Dim degreesF As Single degreesF = txtIn.Text txtOut.Text = convert( degreesF ) txtIn.Text = degreesFEnd Sub

Page 44: Subprograms Functions Procedures. Subprograms A subprogram separates the performance of some task from the rest of the program. Benefits: “Divide and

Side Effects

A new value is assigned to F, which is coupled with degreesF .

Private Function convert(ByRef F ) As Single Return (F - 32) * 5 / 9 F = F + 50 End Function_____________________________________Private Sub cmdGo_Click() Dim degreesF As Single degreesF = txtIn.Text txtOut.Text = convert( degreesF ) txtIn.Text = degreesFEnd Sub

Changes F

And changes degreesF

Page 45: Subprograms Functions Procedures. Subprograms A subprogram separates the performance of some task from the rest of the program. Benefits: “Divide and

Side Effects

For safety, parameters passed to functions should ALWAYS be stipulated as ByVal.

Private Function convert (ByVal F ) As Single Return (F - 32) * 5 / 9 F = F + 50 End Function_____________________________________Private Sub cmdGo_Click() Dim degreesF As Single degreesF = txtIn.Text txtOut.Text = convert( degreesF ) txtIn.Text = degreesFEnd Sub

Changes F

But NOT degreesF

Page 46: Subprograms Functions Procedures. Subprograms A subprogram separates the performance of some task from the rest of the program. Benefits: “Divide and

Side Effects

Of course the same kind of thing can happen with procedure subprograms.

The best practice is to declare only those parameters specifically needed to return data as ByRef.