180 likes | 328 Views
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.
E N D
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 txtAnswer.Text = CStr(sum) End Sub • Calling the sub procedure: • AddNumbers(5,10) ARGUMENTS PARAMETERS
Passing Arguments to Parameters AddNumbers( 5 , 10) Sub AddNumbers _ (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
More about PARAMETERS • 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
More about ARGUMENTS • 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 AddNumbers( 5 , 10) Sub AddNumbers _ (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: • AddNumbers (5 ,2) • 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: Sub AddNumbers(ByRef num1 as Double, 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.