80 likes | 280 Views
Parameter Passing in Visual Basic. Computer Science 2 Gerb Objective: Understand parameter passing in Visual Basic. Parameters. Recall that parameters are the data we pass to procedures to help them do their job When a procedure takes parameters , we declare them Give them a type
E N D
Parameter Passing in Visual Basic Computer Science 2 Gerb Objective: Understand parameter passing in Visual Basic
Parameters • Recall that parameters are the data we pass to procedures to help them do their job • When a procedure takes parameters, we declare them • Give them a type • Give them a name • Decide whether they are ByVal or ByRef (more on this later) • The parameters that a procedure is declared to accept are called formal parameters • The values passed to it when it is called are called actual parameters • Parameters are also called arguments
Formal Parameter Example: Three formal parameters Sub TwoDigits(ByVal intNum As Integer, ByRef intFirstDigit As Integer, ByRef intSecondDigits As Integer) intFirstDigit=intNum/10 intSecondDigit=intNum Mod 10 End Sub
The first is an integer, ByVal Sub TwoDigits(ByValintNum As Integer, ByRef intFirstDigit As Integer, ByRef intSecondDigits As Integer) intFirstDigit=intNum/10 intSecondDigit=intNum Mod 10 End Sub
Second and third are ByRef integers Sub TwoDigits(ByVal intNum As Integer, ByRef intFirstDigit As Integer, ByRef intSecondDigits As Integer) intFirstDigit=intNum/10 intSecondDigit=intNum Mod 10 End Sub
Difference between ByVal and ByRef • If we called our twodigits procedure: Call TwoDigits(i,j,k) • The value of i will not change • i is declared ByVal, • This means its value will not change when the procedure is called • j and k could change after the procedure finishes • j and k are ByRef • That means if the procedure changes the value of these parameters, they remain changed after the procedure finishes