QB64 Phoenix Edition
Call by reference / value - Printable Version

+- QB64 Phoenix Edition (https://qb64phoenix.com/forum)
+-- Forum: Chatting and Socializing (https://qb64phoenix.com/forum/forumdisplay.php?fid=11)
+--- Forum: General Discussion (https://qb64phoenix.com/forum/forumdisplay.php?fid=2)
+--- Thread: Call by reference / value (/showthread.php?tid=2209)



Call by reference / value - Kernelpanic - 11-27-2023

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]


RE: Call by reference / value - mnrvovrfc - 11-27-2023

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.


RE: Call by reference / value - SMcNeill - 11-28-2023

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.


RE: Call by reference / value - TerryRitchie - 11-28-2023

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