Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Call by reference / value
#1
To once again take chances handing over arguments to procedures (and functions).

A "Shared" is not necessary to access the address of a variable, as "Call by reference" is the standard access for procedures and functions in QuickBasic (QB64). "Shared" is only necessary if one want to make a variable accessible throughout the entire program.

For "AlsWert einzahlung, (zinswert), jahre" (Line 31) only the interest value is passed as a value, since this value should be changed as a copy.

Code: (Select All)

'Uebung zu Call by reference/value - 27. Nov. 2023

Option _Explicit

Declare Sub AlsWert(einzahlung As Double, zinwertn As Double, jahre As Integer)
Declare Sub AlsReferenz(einzahlung As Double, zinwertn As Double, jahre As Integer)

'einzahlung = Deposit -- zinswert = Interest value
Dim As Double einzahlung, zinswert, endkapital, q
Dim As Integer jahre

Locate 2, 2
Input "Jaehrliche Einzahlung    : ", einzahlung
Locate 3, 2
Input "Hoehe der Verzinsubg      : ", zinswert
Locate 4, 2
Input "Einzahlungsdauer in Jahren: ", jahre

'Zinsfaktor berechnen - (Calculate interest factor)
q = (1 + (zinswert / 100))

'Berechnung des Endkapitals nach X Jahren - (Final capital after X years)
endkapital = (einzahlung * q * (((q ^ jahre) - 1) / (q - 1)))

Locate 6, 2
Print Using "Das Endkapital betraegt bei ##.##% Zinsen nach ## Jahren: ##,####.##"; zinswert, jahre, endkapital

Locate 8, 2
Print "Als Wertuebergabe (Call by value)"
Locate 9, 2
AlsWert einzahlung, (zinswert), jahre

Locate 11, 2
'The interest value was not changed (copy)
Print Using "Der Zinswert wurde nicht veraendert (Kopie): ##.##%"; zinswert

Locate 13, 2
Print "Als Referenz (Call by reference)"
Locate 14, 2
AlsReferenz einzahlung, zinswert, jahre

Locate 16, 2
'The interest value was changed because the program has access to the address of the variable.
Print "Der Zinswert wurde veraendert, da das Programm "
Locate 17, 2
Print Using "Zugriff auf die Adresse hat: ##.##%"; zinswert

End 'Hauptprogramm


Sub AlsWert (einzahlung As Double, zinswert As Double, jahre As Integer)

  Dim As Double endkapital, q

  zinswert = 5.7

  'Zinsfaktor berechnen
  q = (1 + (zinswert / 100))

  'Berechnung des Endkapitals nach X Jahren
  endkapital = (einzahlung * q * (((q ^ jahre) - 1) / (q - 1)))

  Print Using "Das Endkapital betraegt bei ##.##% Zinsen nach ## Jahren: ##,####.##"; zinswert, jahre, endkapital
End Sub

Sub AlsReferenz (einzahlung As Double, zinswert As Double, jahre As Integer)

  Dim As Double endkapital, q

  zinswert = 6.75

  'Zinsfaktor berechnen
  q = (1 + (zinswert / 100))

  'Berechnung des Endkapitals nach X Jahren
  endkapital = (einzahlung * q * (((q ^ jahre) - 1) / (q - 1)))

  Print Using "Das Endkapital betraegt bei ##.##% Zinsen nach ## Jahren: ##,####.##"; zinswert, jahre, endkapital
End Sub

[Image: Call-by-reference-Anzeige2023-11-27.jpg]
Reply
#2
Just as long as everybody remembers each parameter is passed to a subprogram by reference. It means the value of a variable could be changed accidentally. BYVAL cannot be used away from DECLARE LIBRARY block, and therefore there is no easy way to do anything about it except to declare local variable copies that protect the incoming parameters.
Reply
#3
Thing to remember is that passing via reference only works IF variable types match.  For example, change the types from DOUBLE to _FLOAT in the program above.

Best practice, if one doesn't want values to pass by reference, is just to pass to temp variables.

SUB foo(tempX, tempY)
    x = tempX: y = tempY
    'Now use x/y with zero worry over changing your original variable values.
Reply
#4
(11-28-2023, 02:36 AM)SMcNeill Wrote: Thing to remember is that passing via reference only works IF variable types match.  For example, change the types from DOUBLE to _FLOAT in the program above.

Best practice, if one doesn't want values to pass by reference, is just to pass to temp variables.

SUB foo(tempX, tempY)
    x = tempX: y = tempY
    'Now use x/y with zero worry over changing your original variable values.
Yep, I explain this method in Lesson 6 of the tutorial here. The code that generates Figure 7 (variablepassing2.bas) is an example of Steve's method.
There are two ways to write error-free programs; only the third one works.
QB64 Tutorial
Reply




Users browsing this thread: 1 Guest(s)