Interactive PowerBasic Forum

IT-Consultant: Charles Pegge => OxygenBasic Examples => Topic started by: Frank Brübach on June 01, 2024, 10:19:58 PM

Title: Function question
Post by: Frank Brübach on June 01, 2024, 10:19:58 PM
Hi Charles,

I have a General question if its possible with oxygen to call a function itself without calling it separately and extra as usual ? Dont using an include File?



'#include "testme.inc" '' with using an include file it's not a problem here

FUNCTION mytest() AS LONG
    local t as long
    LOCAL a,b,c AS LONG
    a=20
    b=50
    c=a*b
    print "calculation test "+ str(c)  ' result 1000
   
    ''' it's possible to call mytest() itself inner function?
    'if not t then
    'call mytest()
    'end if
    ''' it's possible to call mytest() itself inner function?

END FUNCTION

mytest() ' all ok that's the usual way to call a function

''
''mytest() ' if it's deactivated it's not possible to call mytest() function
''           or it's another way possible perhaps with asm?
Title: Re: Function question
Post by: Charles Pegge on June 01, 2024, 10:49:01 PM
If you mean recursion, yes. the function does not have to be declared first. But you have to make sure it stops calling itself before causing a stack overflow.

Example of simple direct recursion:
uses console
function rf(int i)
if i>0  'limit recursion
  print i tab i*i cr
  rf(i-1) 'recurse
endif
end function
rf 20
wait
Title: Re: Function question
Post by: Frank Brübach on June 01, 2024, 11:14:18 PM
Yes thanks..  I know already recursive function as Well but my question was more directed into this direction what happened If you dont call in your example the rf(20) ?

I tried to understand how does a function Like pbmain() Works without calling it itself at the end?

I think its Not possible but I can See it must Run ;)
Title: Re: Function question
Post by: Charles Pegge on June 01, 2024, 11:37:08 PM
All functions are designed to be called. In PB there is invisible code calling pbmain(). (OxygenBasic does not require a main function.)
Title: Re: Function question
Post by: Frank Brübach on June 02, 2024, 01:00:29 AM
Yes, thanks charles :) I have explored some Infos more... And I understand more about this Special topic..

"In PowerBASIC, the pbmain() function is a  special function that serves as the entry point for the program.
When you run a PowerBASIC program, the pbmain() function is automatically called by the PowerBASIC runtime, so you don't
need to call it explicitly in your code."

I am not quite sure but found an old simple example of a PowerBASIC program that demonstrates how The pbmain() function and the PowerBASIC runtime library work together:

#Include "PbWin90.inc"

Function pbMain() As Long

  MessageBox 0, "Hello, World!", "PowerBASIC Example", MB_OK

  EndFunction