Code request: Need an function that returns true when this function is called in an function or sub

General FreeBASIC programming questions.
Post Reply
mrminecrafttnt
Posts: 131
Joined: Feb 11, 2013 12:23

Code request: Need an function that returns true when this function is called in an function or sub

Post by mrminecrafttnt »

I need something like this

Code: Select all

If ThisIsInFunctionOrSubCalled then
	return true 
else 
	return false
end if
Is this possible?
caseih
Posts: 2157
Joined: Feb 26, 2007 5:32

Re: Code request: Need an function that returns true when this function is called in an function or sub

Post by caseih »

I'm not clear at all on what you mean there.

Can you be more specific and explain what you want to do?
SARG
Posts: 1763
Joined: May 27, 2005 7:15
Location: FRANCE

Re: Code request: Need an function that returns true when this function is called in an function or sub

Post by SARG »

+1

So I'm not alone :D
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Code request: Need an function that returns true when this function is called in an function or sub

Post by dodicat »

Take a punt?

Code: Select all


#macro set(n)
sub n 
    tally+=__function__+" "
    print timer
    print 
    sleep 100
end sub
#endmacro

function tally() byref as string
    static as string s
    return s
end function

set(a)
set(b)
set(c)
set(d)
set(e)
set(f)
set(g)
set(h)
set(i)

a
d
g
i


sub finish destructor
    print "procedures called:"
    print tally
    print "press any key to end"
    sleep
    end sub

 
caseih
Posts: 2157
Joined: Feb 26, 2007 5:32

Re: Code request: Need an function that returns true when this function is called in an function or sub

Post by caseih »

I like the macro idea.

Another idea is to use a code profiler to trace the execution of the program and all the subroutine calls.
hhr
Posts: 208
Joined: Nov 29, 2019 10:41

Re: Code request: Need an function that returns true when this function is called in an function or sub

Post by hhr »

I would do something like that with an additional parameter.

Code: Select all

function WhoCalledMe(ThisIsInFunctionOrSubCalled as boolean = false) as boolean
   if ThisIsInFunctionOrSubCalled = true then
      print "I am called by a function or a sub"
   else
      print "I am called by main"
   end if
   return ThisIsInFunctionOrSubCalled
end function

sub CallWhoCalledMe
   print WhoCalledMe(true)
end sub

print WhoCalledMe
print
CallWhoCalledMe

sleep
fxm
Moderator
Posts: 12106
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Code request: Need an function that returns true when this function is called in an function or sub

Post by fxm »

I did not quite understand what you are exactly looking to do, but the code below might be able to help:

Code: Select all

Function WhoCalledMe(Byref fct As String) As Boolean
    If fct = "__FB_MAINPROC__" Or fct = "__FB_MODLEVELPROC__" Then
        Print "I am called by main"
        Return False
    Else
        Print "I am called by a function or a sub ('" & fct & "')"
        Return True
    End If
End Function

Sub CallWhoCalledMe
    Print WhoCalledMe(__FUNCTION__)
End Sub

Print WhoCalledMe(__FUNCTION__)
Print
CallWhoCalledMe

Sleep

Code: Select all

I am called by main
false

I am called by a function or a sub ('CALLWHOCALLEDME')
true
mrminecrafttnt
Posts: 131
Joined: Feb 11, 2013 12:23

Re: Code request: Need an function that returns true when this function is called in an function or sub

Post by mrminecrafttnt »

fxm wrote: Mar 19, 2023 17:14 I did not quite understand what you are exactly looking to do, but the code below might be able to help:

Code: Select all

Function WhoCalledMe(Byref fct As String) As Boolean
    If fct = "__FB_MAINPROC__" Or fct = "__FB_MODLEVELPROC__" Then
        Print "I am called by main"
        Return False
    Else
        Print "I am called by a function or a sub ('" & fct & "')"
        Return True
    End If
End Function

Sub CallWhoCalledMe
    Print WhoCalledMe(__FUNCTION__)
End Sub

Print WhoCalledMe(__FUNCTION__)
Print
CallWhoCalledMe

Sleep

Code: Select all

I am called by main
false

I am called by a function or a sub ('CALLWHOCALLEDME')
true
Yep this works fine :)
Hedgehog
Posts: 3
Joined: Sep 30, 2022 17:45

Need an function that returns true...

Post by Hedgehog »

here the solution to your light problem:
'
'chuck.summer@mail.com:
dim shared as boolean f2,b1
'
function uf(x as ubyte) as boolean:return x=1:end function 'under_function_uf
function f0() as boolean:dim f1 as boolean:f1=uf(1):? "f1:";f1:return f1:end function 'call uf in function f0
sub f2b():f2=uf(1):? "f2:";f2:end sub 'call uf in sub
'
b1=f0() 'call uf (returns true) in main_function
f2b() 'call uf (returns true) in main_sub
b1=uf(0):? b1 'call uf (returns false) :code is not in function or sub
'
do:loop
'
'PCN w Hedgehog
Imortis
Moderator
Posts: 1924
Joined: Jun 02, 2005 15:10
Location: USA
Contact:

Re: Need an function that returns true...

Post by Imortis »

Hedgehog wrote: Mar 21, 2023 18:51 here the solution to your light problem:

Code: Select all

'
'chuck.summer@mail.com:
dim shared as boolean f2,b1
'
function uf(x as ubyte) as boolean:return x=1:end function 'under_function_uf
function f0() as boolean:dim f1 as boolean:f1=uf(1):? "f1:";f1:return f1:end function   'call uf in function f0
sub f2b():f2=uf(1):? "f2:";f2:end sub 'call uf in sub
'
b1=f0()       'call uf (returns true) in main_function
f2b()         'call uf (returns true) in main_sub
b1=uf(0):? b1 'call uf (returns false) :code is not in function or sub
'
do:loop
'
'PCN w Hedgehog
Please use the code tags around code in your posts: [ code ] [ /code ] without the spaces inside the brackets will display your code in a nicer way.
Post Reply