Functions inside functions

BlitzMax Forums/BlitzMax Programming/Functions inside functions

Curtastic(Posted 2005) [#1]
I totally thought this would never work. You know how a idiot is so stupid that he doesn't know what is impossible to do? so then he actually achieves the impossible...
You can have functions inside functions!


In case people don't know... this is pretty cool.
Strict

Graphics 800,600

'update() is not accessable here.

mainloop()



Function MainLoop()
	'timer is accesable only by functions inside mainloop()!
	Global timer
	timer=CreateTimer(30)
	
	Repeat
		update()
		draw()
	Forever

	
	
	Function Update()
		FlushMem

		WaitTimer timer

		If KeyHit(key_escape) Then End
	EndFunction
	
	
	
	
	Function Draw()
		Cls
	
		Flip
	EndFunction
EndFunction



Russell(Posted 2005) [#2]
Local functions? Cool! Another un-documented feature!

Russell


Curtastic(Posted 2005) [#3]
April fools!!!
Thanks for playing along Russell.

so how many people did I get? ....hehe


N(Posted 2005) [#4]
Nested functions? Didn't see that one coming.

This may prove to be interesting..
Function foo()
	Function bar(s$)
		Print s
	End Function
	
	bar("Yay") ' <- doesn't throw an error
End Function

foo()
'bar() ' <- throws an error


Edit: Not just one level, apparently.
Function foo()
	bar()
	Function bar()
		rofl()
		Function rofl()
			copter()
			Function copter()
				loller()
				Function loller()
					skates()
					Function skates()
						Print "foobar roflcopter lollerskates"
					End Function
				End Function
			End Function
		End Function
	End Function
End Function

foo()
Input$()



Xip(Posted 2005) [#5]
lol, well i dident se that one comming, but im not that supriced wen it comes to mr.Mark, it makes sence to me...
if i were to write a langugage, i whod proberbly do the function deklarations recursevly.

if one write the language smart, one can almost gain this function by it self.

not to shure if i will ever use this, but hell, still very nice doh!:)

but what the hell is Goorrae talking about???, posted a message 2005-04-07, calling it Aprils joke(?), and his post wasent even false(?)
.... or is that the joke part?


Curtastic(Posted 2005) [#6]

but what the hell is Coorrae talking about???, posted a message 2005-04-07, calling it Aprils joke(?), and his post wasent even false(?)
.... or is that the joke part?


April double fools!
Its called a April double fools joke.
You announce something big on april fools day and say it is a joke, but the joke is that it is actually real. People will then say "I didnt fall for it. I knew it was fake." hehe
Of course this is late because I just found out about it.



I am going to use nested functions a lot probably.
For my RTS, the workerupdate() function got really huge, so I split it into separate functions
workerupdateaction()
workerupdatewait()
workerupdatebuild()
workerupdategather()

Now I could have just workerupdate() and inside it there is action() gather() build()
It also shows that the function workerupdategather() should never be called outside of workerupdate(). It gives the correct scope


ziggy(Posted 2005) [#7]
This feature is not well supported (I don't know if it will be in the future or not).
Try this example:

Strict
rem 
Nesting things and seeing what happens
endrem 
Function Pepe(Variable:String)
	print Variable
	dentro()
	Function Dentro()
		print Variable
	End function
End Function
pepe("Hello")


In this example, this app shouldn't compile becouse the variable called 'Variable' is not defined inside the function Dentro(). But it compiles and we get a very unestable program.
Try it and rememer to make Ctrl + Alt + supr to stop the application if you're using BlitzMax IDE.


Xip(Posted 2005) [#8]
Coorray:

#1, my point is this, your "april joike" was posted 2005-04-07 , that is 7 days TO LATE.... so it is not even an april joke.

also, #2, "April double fools" sound's like somting a 10years old kid made up, ther is no sutsh thing, not were im from anyway.

so lets get this straight, you say somting that is true and make sence, like "it is a nice day to day..." and then people say, "ohh, yeas it is" , then you say... "april double fools!".... that makes no sence.... stupid and lame if you ask me....


ImaginaryHuman(Posted 2005) [#9]
It's a British thing, but they also do it in America .. April 1st is `all fools day` and people traditionally play practical jokes on each other.

As to the nested functions, I think I saw somewhere in the modules that Mark had used them???

If I'm mistaken, I'll just claim I was making an april fools joke ;-)


Curtastic(Posted 2005) [#10]
Xip, it sounds like I hurt you.


#1, my point is this, your "april joike" was posted 2005-04-07 , that is 7 days TO LATE


I already acknowledged the fact that the post was late.
Late april fools jokes happen on the internet.


so lets get this straight, you say somting that is true and make sence, like "it is a nice day to day..." and then people say, "ohh, yeas it is" , then you say... "april double fools!".... that makes no sence.... stupid and lame if you ask me....


oh my god

"Double fools" is when you announce something real and outrageous, and get people to beleive that it is FAKE.
"double fools" probably doesn't exist widespread. thats why I explained it.


N(Posted 2005) [#11]
Xip is just insane, pay no nevermind to his inane ramblings from beyond the FarGate (which has no relation to a movie of a similar name -- just so we're clear on that).


Xip(Posted 2005) [#12]
ehhh, what are you talking about Gino?
insane? why is that?
shall i take that as an personal attack?
if it is cus you are having problem anderstand me, then that has noting to do with "insance ramblings", maby you shod be a bit more open to other people and not jump to conklutions to fast.
beyond the fargate? .... if that is not ramlings, i dont not that is...

AngelDaniel:
yeas, we have April 1st `all fools day` in sweden to, but that is also my point, how can you fool some one if you claim stuff that is true and make sence, then you dont fool anyone, so i dont see the "praktical joke" in that...
and the fakt that it was a true statment after paril....

look, if the statment were made on April 1st, people may think that it might be an April joke and think that it is migh be false... and oky, that i can find funny, BUT in this case, the "joke" was made after april, so no one took it for an false statment joke(or a double ... whatever) false statment, ther for.... i dont think it made any sence to say "how manny did i fool" .... cus he dident....
no one said, "hell, it is 2005-04-07 to day, so this might be false...."

it was only a True statment that made sence, and that is all it was, no "april fools"...

did you get that Gino, or shal i go throw it one more time a bit slower?

sorry to you others, i dont mean to sound harsh, but "insance" is not a kompliment were im from.


MattVonFat(Posted 2005) [#13]
I agree with Xip if there are any inane ramblings on this thread they are coming from Coorrae with this 'April double fools' on the 7th April junk.


Xip(Posted 2005) [#14]
Tanx MattVonFat, i started to feel so alone in this thread ;)
hehe


N(Posted 2005) [#15]
Lighten up, Xip, it's called 'sarcasm'. I assumed you would have figured that out by the part with the FarGate, but apparently not. So take a chill pill.


Xip(Posted 2005) [#16]
ehhh, right...
you know, it dident sound like sarkasm, it sounded more like an insult, "pay no nevermind to his inane ramblings...."
and by the way, ther is alot of strange spelling in that message and not your last one?
so shall i take this ass a joke on my spelling to? after all, your message are all about "me not making any sence...."
you realy shod learn your manner m8....

so, maby you shod take one of your own "chill pills" and keep the nasty sarkasm to your self.

just a friendly advise.


N(Posted 2005) [#17]
Xip - Lord of blowing things out of proportion.


Curtastic(Posted 2005) [#18]
What were you thinking Ginormous???? You obviously can't make a joke when Xip is around.


N(Posted 2005) [#19]
Anyone else makes a joke and I'll hang'em! Off to class!


Xip(Posted 2005) [#20]
Corrae: i dont have any problems with fun jokes, but that is unless it is a mean or personal attack on someone, i dont care if its sarkasm, irony or a bad heir day.
tell other to "ignore" some one, cus thay are insane ramling idiots(might asswell used the last word, the line is the same), "pay no nevermind to his inane ramblings" is a not a very nice thing to say, and then he starts to crawl away from it by calling it sarkasm...

if you must use insulting comments on other people as sarkasm, then you shod make SHURE it is a noticable sarkastik with some smajlies or whatever.... or people might not take it very nice, i se no trace of smajlies or any type of sign of sarkasm in your message OR reply.

it looks like a mean comment, nothing fun about that.

look, most fun of all, lets drop it and not fill this thread with junk flame wars, i have alot more stuff to do with my time, like.... write insane rambling post's in other forums;)


Curtastic(Posted 2005) [#21]
okay cool


Diordna(Posted 2005) [#22]
Wow, this is a first, flame wars stopping themselves! Kudos to all of you.

Xip, you're not doing too bad for a dyslexic second-languager :)

Anyways, what I don't understand is why it's necessary to use local methods. What's wrong with making them global? In some game cases, it can make things much easier, such as rendering but not moving player ships if you're making a spiffy transition.


Xip(Posted 2005) [#23]
Eazy dont neseserly means best solution
but it in this case, i must say that im not so impressed with the hole local function consept. i have seent it in use it a fiew other langugages, like Delphi, but it is not somting i whod use for my larger projekts.
however, i like the freedom of shoise.

but your are right, it is not a nessesery thing, but the hole thinking is the same as the methods in objekts and not making it all in one huge pile of functions.

one dont want the "interface" to show you more then what you can/need to use.

it is more a way to make things clear to your self and others, that this bit of source, is only made to run in this function.

you can gaint the same respons when trying to explain OOP consept for C programmers, thay dont see the point of having functions inside methods, when it is "so mutsh nicer" with them all global. :)

well, hope it helps ;)


Bot Builder(Posted 2005) [#24]
Yeah, mark probably didn't design this in, it was probably just convenient to call the compiler recursivly when you hit a function/ whatever.

To test this, yes its like a program inside of a program:

Strict

Function drive()
	Global maxspeed=75
	type car
		field speed#
	End Type
	
	Local mycar:car=New car
	mycar.speed=50
	Print "my speed before: "+mycar.speed
	update(mycar)
	Print "my speed now: "+mycar.speed
	maxspeed=45
	update(mycar)
	Print "my speed now: "+mycar.speed

	Function update(c:car)
		Print "Speed limit "+maxspeed
		If c.speed>maxspeed Then c.speed=maxspeed
	EndFunction
End Function

drive


Notice there's a type and global inside the function. Oh, and consts work too.


WendellM(Posted 2005) [#25]
Are the scopes of those in-function types, globals, and consts limited to within the function, too? That is, if you put a "Print maxspeed" after the "drive" call do you get an undefined error due to the Strict? (I don't yet have Max or I'd test it myself.)

If so, that'd be pretty sweet. You could just stick "Function/End Function"s around previously stand-alone programs and they could happily co-exist even if they use the same type, global, and const names.


Curtastic(Posted 2005) [#26]
yep, its all local. You cant make a car variable unless youare in the drive() function.


This makes an error though and won't work.
If you paste this into untitled1.bmx and run it, it will instead just run whatever the old untitled1.exe is.
Strict

drive()

Function drive()
	Type car
		Field speed#
	End Type
End Function
 
Type car
	Field size:Int
End Type

I hope it gets fixed. for some reason I want this


WendellM(Posted 2005) [#27]
Thanks, Coorrae & Bot Builder; I look forward to trying those capabilities out. The more I see about Max, the more I like it (I'm just sticking with Blitz3D for now because it's more stable and 3D-capable).

P.S. Coorrae, what happens if you delete the old untitled1.exe before running your sample above?


Curtastic(Posted 2005) [#28]
Building untitled1
Compiling:untitled1.bmx
flat assembler version 1.51
C:/Documents and Settings/Curtis/Desktop/BlitzMaxCore106/tmp/.bmx/untitled1.bmx.gui.debug.win32.s [190]:
__bb_car_New:
error: symbol already defined.

Process complete


Curtastic(Posted 2005) [#29]
This works though. everything seems to be okay except duplicate types, Also its parameters are global to all functions inside it.

Const g=2

Function f()
	Const g=3
	f2()
	Function f2()
		Print g
	EndFunction
EndFunction

Print g
f()



Russell(Posted 2005) [#30]
Anything from Mark about this? Is it an accidental or deliberate feature? ;)

Russell


Bot Builder(Posted 2005) [#31]
Mark hasn't said anything, but I'm pretty sure its accidental. Its just that the compiler is so well structured it handles it gracefully. I imagine mark knows its possible though. The entire program is most likely delt with as if it was a function.


Xip(Posted 2005) [#32]
yeas, i agrie with bot builder, i think it is an effekt of smart rekursive function managment.

if i whod have finnished my Corax script engine(i dident add functions) i whod proberbly make it do the same thing.


Michael Reitzenstein(Posted 2005) [#33]
There is a function inside a function used in the module source somewhere.


Garred(Posted 2005) [#34]
Yes, in brl.blitzgl (blitzgl.bmx) there is a function (Pow2Size) inside a function (bglAdjustTexSize). It's a feature.


BadJim(Posted 2005) [#35]
I imagine it was kind of intended, but not propely debugged yet. Hence the silence from Mark Sibly.

I think this is very nice because when I have to write a big complicated Über function I can structure it without writing a heap of child functions with no relevence to the rest of the program.

A comment from Mark Sibly would be nice at this point . . .


rdodson41(Posted 2005) [#36]
A comment from Mark Sibly would be nice at this point . . .

this thread was started like 2 months ago. Id like to know why it was dug up


Will(Posted 2005) [#37]
Im glad it was dug up, as I missed it first time around and its a pretty cool feature - so my globals inside a function persist and are the same each time the function is called, right? Also, how do I call a function defined within a function from outside both, like:

Function Outer()
Function Inner()
End Function
End Function

How do I call inner without calling outer?


Perturbatio(Posted 2005) [#38]
you don't, Inner is a function local to the Outer function.


Bot Builder(Posted 2005) [#39]
Well, since its already been dug up, I'll say mark probably realized it was possible to do this while writing the recursive bit of the compiler, and it would probably take effort to remove this interesting feature.