Cs4 lecture 18 sub procedures
This presentation is the property of its rightful owner.
Sponsored Links
1 / 18

CS4: Lecture 18 – Sub Procedures PowerPoint PPT Presentation


  • 82 Views
  • Uploaded on
  • Presentation posted in: General

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.

Download Presentation

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

CS4: Lecture 18 –Sub Procedures

Roxana Gheorghiu

Wednesday, Feb 16, 2011


Outline

Outline

  • General Procedure

  • Sub Procedures with Parameters

  • More about Parameters and Arguments

  • Passing arguments By Value

  • Passing arguments By Reference


General procedures

General Procedures

  • Used to break complex problems into small problems

  • Two types of procedures: Sub Procedures, Function Procedures


Sub 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


Outline1

Outline

  • General Procedure

  • Sub Procedures with Parameters

  • More about Parameters and Arguments

  • Passing arguments By Value

  • Passing arguments By Reference


Sub procedure with parameters

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 parameters1

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

Passing Arguments to Parameters

AddNumbers( 5 , 10)

Sub AddNumbers _

(ByVal num1 as Double, ByVal num2 as Double)


Outline2

Outline

  • General Procedure

  • Sub Procedures with Parameters

  • More about Parameters and Arguments

  • Passing arguments By Value

  • Passing arguments By Reference


More about parameters

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

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

NOT of Arguments

AddNumbers( 5 , 10)

Sub AddNumbers _

(ByVal num1 as Double, ByVal num2 as Double)


Outline3

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

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 value1

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.


Outline4

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

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 reference1

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.


  • Login