1 / 18

CS4: Lecture 18 – Sub Procedures

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.

ivory
Télécharger la présentation

CS4: Lecture 18 – Sub Procedures

An Image/Link below is provided (as is) to download presentation 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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS4: Lecture 18 – Sub Procedures Roxana Gheorghiu Wednesday, Feb 16, 2011

  2. Outline • General Procedure • Sub Procedures with Parameters • More about Parameters and Arguments • Passing arguments By Value • Passing arguments By Reference

  3. General Procedures • Used to break complex problems into small problems • Two types of procedures: Sub Procedures, Function Procedures

  4. 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

  5. Outline • General Procedure • Sub Procedures with Parameters • More about Parameters and Arguments • Passing arguments By Value • Passing arguments By Reference

  6. 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

  7. 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

  8. Passing Arguments to Parameters AddNumbers( 5 , 10) Sub AddNumbers _ (ByVal num1 as Double, ByVal num2 as Double)

  9. Outline • General Procedure • Sub Procedures with Parameters • More about Parameters and Arguments • Passing arguments By Value • Passing arguments By Reference

  10. 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

  11. 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

  12. NOT of Arguments AddNumbers( 5 , 10) Sub AddNumbers _ (ByVal num1 as Double, ByVal num2 as Double)

  13. 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

  14. 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

  15. 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.

  16. 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

  17. 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

  18. 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.

More Related