CS4: Lecture 18 â Sub Procedures

1 / 18

# CS4: Lecture 18 â Sub Procedures - PowerPoint PPT Presentation

CS4: Lecture 18 – Sub Procedures. Roxana Gheorghiu Wednesday, Feb 16, 2011. Outline. General Procedure Sub Procedures with Parameters More about Parameters and Arguments Passing arguments By Value Passing arguments By Reference. General Procedures.

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

## PowerPoint Slideshow about 'CS4: Lecture 18 â Sub Procedures' - ivory

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

### CS4: Lecture 18 – Sub Procedures

Roxana Gheorghiu

Wednesday, Feb 16, 2011

Outline
• General Procedure
• Sub Procedures with Parameters
• More about Parameters and Arguments
• Passing arguments By Value
• Passing arguments By Reference
General Procedures
• Used to break complex problems into small problems
• Two types of procedures: Sub Procedures, Function Procedures
Sub Procedures
• The simplest Sub Procedure:

Sub SayHi()

MsgBox(“Hello world!”)

End Sub

• How to use it?
• with a call statement of the form:

SayHi()

*Let first test this

Procedure’s name

List of statements to be executed

Outline
• General Procedure
• Sub Procedures with Parameters
• More about Parameters and Arguments
• Passing arguments By Value
• Passing arguments By Reference
Sub Procedure with Parameters
• Passing arguments to the procedure:
• Say a personal HI
• Call: SayHi(“Hi There, My Name is Roxana”)
• Create procedure:

SubSayHi(ByVal text as String)

MsgBox(text)

End Sub

Sub Procedure with Parameters
• Passing arguments to the Sub Procedure

SubAddNumbers(ByVal num1 as Double , _

ByVal num2 as Double )

Dim sum as Double =num1+num2

End Sub

• Calling the sub procedure:

ARGUMENTS

PARAMETERS

Passing Arguments to Parameters

(ByVal num1 as Double, ByVal num2 as Double)

Outline
• General Procedure
• Sub Procedures with Parameters
• More about Parameters and Arguments
• Passing arguments By Value
• Passing arguments By Reference
• Temporary place holders for the values passed to the Sub procedure
• Must NOT forget the N.O.T.:
• The NUMBER of parameters is very important
• The ORDER or parameters is very important
• The TYPE of each parameter is very important
• Arguments’ N.O.T. must be the same as in the Sub Procedure header
• NUMBER of arguments
• ORDER of arguments
• TYPE of each argument
NOT of Arguments

(ByVal num1 as Double, ByVal num2 as Double)

Outline
• General Procedure
• Sub Procedures with Parameters
• Passing arguments to the parameters
• More about Parameters and Arguments
• Passing arguments By Value
• Passing arguments By Reference
Passing BY VALUE
• Step1: Create the Sub procedure:

SubAddNumbers(ByVal num1 as Double, ByVal num2 as Double)

Dim sum asDouble =num1 + num2

txtOutput.Text =CStr (sum)

End Sub

• Step2: Call the Sub procedure:
• Small Explanation:
• Only the VALUES of the parameters are passed to the sub procedure
Passing BY VALUE
• Deeper Explanation:
• when the Sub procedure ends its execution, the variable that is passed as an argument to that procedure will RETAIN its original value regardless of what was done to the corresponding parameter
• *****************

*Make a program that will display the value of a variable in a list box, then it will make a modification over the variable, through a sub procedure and it will display its value in the same listbox. When the sub procedure ends its execution, the value of the variable it’s again displayed in the listbox.

Outline
• General Procedure
• Sub Procedures with Parameters
• Passing arguments to the parameters
• More about Parameters and Arguments
• Passing arguments By Value
• Passing arguments By Reference
Passing BY REFERENCE
• Creating the Sub procedure:

ByRef num2 as Double)

End Sub

• Calling the Sub procedure:AddNumbers (5 ,2)
• Small Explanation:
• the arguments 5 and 2 are passed to the Sub Procedure by reference
Passing BY REFERENCE
• Deeper Explanation:
• when the Sub procedure ends its execution, the variable that is passed as an argument to that procedure will CHANGE its value according to the modification made in the sub procedure
• ********

Make a program that will display the value of a variable in a list box, then it will make a modification over the variable, through a sub procedure and it will display its value in the same listbox. When the sub procedure ends its execution, the value of the variable it’s again displayed in the listbox.