Author Topic: A Function as an argument for a Function  (Read 3586 times)

0 Members and 1 Guest are viewing this topic.

Offline Kernelpanic

  • Newbie
  • Posts: 94
    • View Profile
A Function as an argument for a Function
« on: October 22, 2020, 03:40:25 pm »
I convert a C-Program to QBasic64, and my question is: Can one in Basic hand over at a Function another Function as an argument ?

This is the source in C - my own program
Quote
printf("\nDas Kapital reicht bei einer monatlichen Abhebung von %4.2lf fuer %2.1lf Jahre.\n"   ,monatsabhebung
                                                 ,auszahlungsdauer((endausgabe(einzahlung, q, zeit)), monatsabhebung));
   return(0);                                 
}

//Funktionsdefinition
//Endsumme ausgeben, auch wenn die Anzahl Jahre nicht durch 5 teilbar ist
double endausgabe(double kapital, double q, int zeit)
{
   double endkapital;
   endkapital = (kapital * (( pow( q, zeit ) - 1 ) / ( q -1 )));
   return(endkapital);
}

double auszahlungsdauer(double endkapital, double monatliche_abhebung)
{
   double auszahlungszeit;
   auszahlungszeit = ((endkapital / monatliche_abhebung) / 12.0);
   return(auszahlungszeit);
}

In QBasic 64 I get error messages only. Up to the point the program works correct.

Mark Twain
"Als wir das Ziel endgültig aus den Augen verloren hatten, verdoppelten wir unsere Anstrengungen."
„Having lost sight of our goals, we redoubled our efforts.“

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: A Function as an argument for a Function
« Reply #1 on: October 22, 2020, 03:54:55 pm »
You are missing a parentheses in the first error. The other ones look correct as well.
Shuwatch!

Offline Kernelpanic

  • Newbie
  • Posts: 94
    • View Profile
Re: A Function as an argument for a Function
« Reply #2 on: October 22, 2020, 06:02:58 pm »
You are missing a parentheses in the first error. The other ones look correct as well.

I tried everything for three hours. . .
Mark Twain
"Als wir das Ziel endgültig aus den Augen verloren hatten, verdoppelten wir unsere Anstrengungen."
„Having lost sight of our goals, we redoubled our efforts.“

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: A Function as an argument for a Function
« Reply #3 on: October 22, 2020, 07:18:28 pm »
Perhaps a QB64 code snippet you are trying?

Offline Kernelpanic

  • Newbie
  • Posts: 94
    • View Profile
Re: A Function as an argument for a Function
« Reply #4 on: October 22, 2020, 08:39:22 pm »
That is the program:

Code: QB64: [Select]
  1. 'Berechnet ein angespartes Kapital nach n Jahren
  2. 'Wie lange reicht die angesparte Summe, wenn
  3. 'monatlich 500,00 abgehoben werden?
  4. 'In QBasic 64 uebertragen 21. Okt. 2020
  5.  
  6. Declare Endkapital# (einzahlung as Double, q as Double, zeit as Integer)
  7. Declare Auszahlungsdauer# (endkapital as Double, monatliche_abhebung as Double)
  8.  
  9. DIM einzahlung AS DOUBLE, zinswert AS DOUBLE, zwischen_sum AS DOUBLE, q AS DOUBLE
  10. DIM monatsabhebung AS DOUBLE, dummy AS DOUBLE
  11. DIM zeit AS INTEGER, zaehler AS INTEGER
  12.  
  13. PRINT "Berechnet zu welcher Endsumme sich eine gleichbleibende"
  14. PRINT "jaehrlich Kapitaleinzahlung summiert."
  15. INPUT "Geben Sie die jaehrlich Einzahlung ein      : ", einzahlung
  16. INPUT "Geben Sie den Zinsfuss ein                  : ", zinswert
  17. INPUT "Geben Sie die Einzahlungsdauer (Jahre) ein  : ", zeit
  18. LOCATE (CSRLIN + 2), POS(0)
  19.  
  20. 'Zinsfaktor berechnen
  21. q = (1 + (zinswert / 100))
  22.  
  23. zaehler = 5
  24. DO WHILE zaehler < zeit
  25.   'Zwischensummen alle 5 Jahre ausgeben
  26.   zwischen_sum = (einzahlung * (((q ^ zaehler) - 1) / (q - 1)))
  27.   PRINT USING "Das Kapital belaeuft sich nach ### Jahren auf: ######,.## Euro"; zaehler, zwischen_sum
  28.  
  29.   zaehler = zaehler + 5
  30.   IF zaehler = zeit THEN EXIT DO
  31. PRINT USING "Das Endkapital betraegt nach ### Jahren      : ######,.## Euro"; zeit, Endkapital#(einzahlung, q, zeit)
  32. INPUT "Monatliche Abhebung: "; monatsabhebung
  33. PRINT USING "Das Kapital reicht bei einer monatlichen Abhebung von ####,.## fuer ### Jahre."; monatsabhebung, Auszahlungsdauer#((Endkapital#(einzahlung, q, zeit)), monatsabhebung)
  34.  
  35. END 'Hauptprogramm
  36.  
  37. FUNCTION Endkapital# (einzahlung AS DOUBLE, q AS DOUBLE, zeit AS INTEGER)
  38.  
  39.   Endkapital# = (einzahlung * (((q ^ zeit) - 1) / (q - 1)))
  40.  
  41. FUNCTION Auszahlungsdauer# (endkapital AS DOUBLE, monatliche_abhebung AS DOUBLE)
  42.  
  43.   Auszahlungsdauer# = ((endkapital / monatliche_abhebung) / 12.0)
  44.  
« Last Edit: October 22, 2020, 08:42:24 pm by Kernelpanic »
Mark Twain
"Als wir das Ziel endgültig aus den Augen verloren hatten, verdoppelten wir unsere Anstrengungen."
„Having lost sight of our goals, we redoubled our efforts.“

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: A Function as an argument for a Function
« Reply #5 on: October 22, 2020, 09:44:42 pm »
It works but no idea what is going on to confirm it does what you want.
Code: QB64: [Select]
  1. 'Berechnet ein angespartes Kapital nach n Jahren
  2. 'Wie lange reicht die angesparte Summe, wenn
  3. 'monatlich 500,00 abgehoben werden?
  4. 'In QBasic 64 uebertragen 21. Okt. 2020
  5.  
  6. DECLARE Endkapital# (einzahlung AS DOUBLE, q AS DOUBLE, zeit AS INTEGER)
  7. DECLARE Auszahlungsdauer# (endkapital AS DOUBLE, monatliche_abhebung AS DOUBLE)
  8.  
  9. DIM einzahlung AS DOUBLE, zinswert AS DOUBLE, zwischen_sum AS DOUBLE, q AS DOUBLE
  10. DIM monatsabhebung AS DOUBLE, dummy AS DOUBLE
  11. DIM zeit AS INTEGER, zaehler AS INTEGER
  12.  
  13. PRINT "Berechnet zu welcher Endsumme sich eine gleichbleibende"
  14. PRINT "jaehrlich Kapitaleinzahlung summiert."
  15. INPUT "Geben Sie die jaehrlich Einzahlung ein      : ", einzahlung
  16. INPUT "Geben Sie den Zinsfuss ein                  : ", zinswert
  17. INPUT "Geben Sie die Einzahlungsdauer (Jahre) ein  : ", zeit
  18. LOCATE (CSRLIN + 2), POS(0)
  19.  
  20. 'Zinsfaktor berechnen
  21. q = (1 + (zinswert / 100))
  22.  
  23. zaehler = 5
  24. DO WHILE zaehler < zeit
  25.     'Zwischensummen alle 5 Jahre ausgeben
  26.     zwischen_sum = (einzahlung * (((q ^ zaehler) - 1) / (q - 1)))
  27.     PRINT USING "Das Kapital belaeuft sich nach ### Jahren auf: ######,.## Euro"; zaehler, zwischen_sum
  28.  
  29.     zaehler = zaehler + 5
  30.     IF zaehler = zeit THEN EXIT DO
  31. PRINT USING "Das Endkapital betraegt nach ### Jahren      : ######,.## Euro"; zeit, Endkapital#(einzahlung, q, zeit)
  32. INPUT "Monatliche Abhebung: "; monatsabhebung
  33. PRINT USING "Das Kapital reicht bei einer monatlichen Abhebung von ####,.## fuer ### Jahre."; monatsabhebung, Auszahlungsdauer#((Endkapital#(einzahlung, q, zeit)), monatsabhebung)
  34.  
  35. END 'Hauptprogramm
  36.  
  37. FUNCTION Endkapital# (einzahlung AS DOUBLE, q AS DOUBLE, zeit AS INTEGER)
  38.  
  39.     Endkapital# = (einzahlung * (((q ^ zeit) - 1) / (q - 1)))
  40.  
  41. FUNCTION Auszahlungsdauer# (endkap AS DOUBLE, monatliche_abhebung AS DOUBLE)
  42.  
  43.     Auszahlungsdauer# = ((endkap / monatliche_abhebung) / 12.0)
  44.  
  45.  
  46.  

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: A Function as an argument for a Function
« Reply #6 on: October 22, 2020, 09:46:00 pm »
@bplus He is wanting to pass a function as an argument. He was using "endkapital" in his function header and I think he thought it would mean that it would call it? That's what I'm gathering, at least.
Shuwatch!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: A Function as an argument for a Function
« Reply #7 on: October 22, 2020, 09:52:46 pm »
Looks to me like he wants a double as argument to second function which just happens to be what the first function returns, so plug in the function as argument (not parameter) for the required double in the actual application of the functions called in the main code.


Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: A Function as an argument for a Function
« Reply #8 on: October 22, 2020, 09:53:57 pm »
That's of course the right way to do it but since I couldn't read any of the language I was confused what was actually happening haha
Shuwatch!

Offline Kernelpanic

  • Newbie
  • Posts: 94
    • View Profile
Re: A Function as an argument for a Function
« Reply #9 on: October 23, 2020, 07:22:25 am »
Now I have the mistake; I it should have known. At "endkapital" the program came to skid. Thus the message "is already in use (endkapital)"
After changing the variable name it works correct now.

The Text is:
'Calculates a saved capital after n years
'How long will the saved amount last, if
one 1200.00 Euro/$  withdraw monthly?
'Transferred to QBasic64 Oct. 21, 2020

The problem:
« Last Edit: October 23, 2020, 07:23:29 am by Kernelpanic »
Mark Twain
"Als wir das Ziel endgültig aus den Augen verloren hatten, verdoppelten wir unsere Anstrengungen."
„Having lost sight of our goals, we redoubled our efforts.“

Offline Kernelpanic

  • Newbie
  • Posts: 94
    • View Profile
Re: A Function as an argument for a Function
« Reply #10 on: October 23, 2020, 07:36:21 am »
So it is correct. And the originally question can be answered with "yes".

Code: QB64: [Select]
  1. 'Berechnet ein angespartes Kapital nach n Jahren
  2. 'Wie lange reicht die angesparte Summe, wenn
  3. 'monatlich 1200,00 abgehoben werden?
  4. 'In QBasic 64 uebertragen 21. Okt. 2020
  5.  
  6.  
  7. Declare Endkapital# (einzahlung as Double, q as Double, zeit as Integer)
  8. Declare Auszahlungsdauer# (kapital as Double, monatliche_abhebung as Double)
  9.  
  10. DIM einzahlung AS DOUBLE, zinswert AS DOUBLE, zwischen_sum AS DOUBLE, q AS DOUBLE
  11. DIM monatsabhebung AS DOUBLE, dummy AS DOUBLE
  12. DIM zeit AS INTEGER, zaehler AS INTEGER
  13.  
  14. PRINT "Berechnet zu welcher Endsumme sich eine gleichbleibende"
  15. PRINT "jaehrlich Kapitaleinzahlung summiert."
  16. INPUT "Geben Sie die jaehrlich Einzahlung ein      : ", einzahlung
  17. INPUT "Geben Sie den Zinsfuss ein                  : ", zinswert
  18. INPUT "Geben Sie die Einzahlungsdauer (Jahre) ein  : ", zeit
  19. LOCATE (CSRLIN + 2), POS(0)
  20.  
  21. 'Zinsfaktor berechnen
  22. q = (1 + (zinswert / 100))
  23.  
  24. zaehler = 5
  25. DO WHILE zaehler < zeit
  26.   'Zwischensummen alle 5 Jahre ausgeben
  27.   zwischen_sum = (einzahlung * (((q ^ zaehler) - 1) / (q - 1)))
  28.   PRINT USING "Das Kapital belaeuft sich nach ### Jahren auf: ######,.## Euro"; zaehler, zwischen_sum
  29.  
  30.   zaehler = zaehler + 5
  31.   IF zaehler = zeit THEN EXIT DO
  32. PRINT USING "Das Endkapital betraegt nach ### Jahren      : ######,.## Euro"; zeit, Endkapital#(einzahlung, q, zeit)
  33. INPUT "Monatliche Abhebung: ", monatsabhebung
  34. PRINT USING "Das Kapital reicht bei einer monatlichen Abhebung von ####,.## fuer ### Jahre."; monatsabhebung, Auszahlungsdauer#((Endkapital#(einzahlung, q, zeit)), monatsabhebung)
  35.  
  36. END 'Hauptprogramm
  37.  
  38. FUNCTION Endkapital# (einzahlung AS DOUBLE, q AS DOUBLE, zeit AS INTEGER)
  39.  
  40.   Endkapital# = (einzahlung * (((q ^ zeit) - 1) / (q - 1)))
  41.  
  42. FUNCTION Auszahlungsdauer# (kapital AS DOUBLE, monatliche_abhebung AS DOUBLE)
  43.  
  44.   Auszahlungsdauer# = ((kapital / monatliche_abhebung) / 12.0)
  45.  
Mark Twain
"Als wir das Ziel endgültig aus den Augen verloren hatten, verdoppelten wir unsere Anstrengungen."
„Having lost sight of our goals, we redoubled our efforts.“

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: A Function as an argument for a Function
« Reply #11 on: October 23, 2020, 09:00:33 am »
So to clarify: there are programming languages that can actually take a function as a parameter but QB64 is not one of them. The closest we might get is passing an array of a user defined type, UDT.

But you can use a function that returns a double ( or string or integer... ) as an argument to pass a double value ( or string or integer... ) to the 2nd function where it calls for a double ( or string or integer... ) in the parameters list of it's definition.

Code: QB64: [Select]
  1. FUNCTION Auszahlungsdauer# (endkap AS DOUBLE, monatliche_abhebung AS DOUBLE)
  2.  
  3.     Auszahlungsdauer# = ((endkap / monatliche_abhebung) / 12.0)
  4.  
  5.  

The parameters list for the above FUNCTION is: (endkap AS DOUBLE, monatliche_abhebung AS DOUBLE)

We can call the FUNCTION above, like this:
PRINT Auszahlungsdauer#( x , y )
where x is a double, literal like .01234567 or variable x# or a function like: Endkapital#( with it's own arguments) but it returns a double.

 
« Last Edit: October 23, 2020, 09:07:46 am by bplus »

Offline Kernelpanic

  • Newbie
  • Posts: 94
    • View Profile
Re: A Function as an argument for a Function
« Reply #12 on: October 23, 2020, 11:57:36 am »
Yes, that was not clearly expressed. In the present case, the "Endkapital" function becomes a parameter of the "Auszahlungsdauer" function, or better, its return value.

(I know how to do it, but I often mix up the names.)
Mark Twain
"Als wir das Ziel endgültig aus den Augen verloren hatten, verdoppelten wir unsere Anstrengungen."
„Having lost sight of our goals, we redoubled our efforts.“