I suppose Hollywood doesnt support nested functions?

Discuss any general programming issues here
Post Reply
Bugala
Posts: 921
Joined: Sun Feb 14, 2010 7:11 pm

I suppose Hollywood doesnt support nested functions?

Post by Bugala »

I did a try to see if Hollywood supports nested functions:

Code: Select all

Function test()
	
	test2()
	DebugPrint("test11")
	
	Function test2()
		DebugPrint("test2")
	EndFunction
EndFunction
However, it also didn't give any error message.

Which gives me two questions, one which is half a wish.

1. Would it be difficult to add possibility for nested functions? Nested functions that would only work inside the function they were declared in could help human readability of the codel
For naming small parts of code into their own functions would make it much easier to figure out what code is doing, however, doing loads of small functions will make it very hard to find anything if you need to keep hunting those functions all around, especially when they are used only inside one function only.

example:

Code: Select all

Function MoveEnemies()

   listofenemiestomove = CheckEnemiesThatAreGoingToMove(level.enemiestable)
   MoveEnemies(listofenemiestomove)
   CheckIfEnemiesThatMovedShoot(listofenemiestomove)

  function Listofenemiestomove(allenemies)
      for n = 1 to tableitems(allenemies)
	check lots of things
      next
  endfunction

  functionMoveEnemies(listofenemiestomove)
      for n = 1 to tableitems(listofenemiestomove)
	check lots of things
      next
  endfunction

  function CheckIfEnemiesThatMovedShoot(listofenemiesthatmoved)
      for n = 1 to tableitems(listofenemiesthatmoved)
	checklotofthing
      next
  EndFunction
   
EndFunction
As you can see, it is very easy to understand what is happening in the code when you just look at those 3 lines, then if necessary, you go and chedck the actual function how it works.

For this the nested functions would be great, and in the order that I can put the code first and functions after the code part, since otherwise if i keep jumping to functions in IDE, then I need to scroll down first to see the code first, which would be annoying.


2. As I mentioned, it didnt give any error, which I found odd, how so?

p-OS
Posts: 123
Joined: Mon Nov 01, 2010 11:56 pm

Re: I suppose Hollywood doesnt support nested functions?

Post by p-OS »

When I try your first Code, i got an "Function test2() no found" error

Of course you can have nested functions.

But you must declare a function always before you call it:

That works:

Code: Select all

Function test()
	
  Function test2()
		DebugPrint("test2")
  EndFunction
	
	test2()
	DebugPrint("test11")
	

EndFunction

test()
Note, that even if declaring test2() within test(), you get a global function !

To make its scope only be visible within test(), you have to use Local keyword:

Code: Select all

Function test()
	
  Local Function test2()
		DebugPrint("test2")
  EndFunction
	
	test2()
	DebugPrint("test11")
	

EndFunction

test()

Bugala
Posts: 921
Joined: Sun Feb 14, 2010 7:11 pm

Re: I suppose Hollywood doesnt support nested functions?

Post by Bugala »

Seems theres a bug then.

I am using Windows 10 Hollywood 8.0

Regardless If I put the test2 function first or last, it still doesnt do any of the degubprints, nor does it give me any error.
The whole input I get in Hollywood IDE is following:
Loading plugin rebelsdl.hwp...done
Opening script Unnamed1...done
Compiling script...done
Preparing display...done
And Action!


In which this rebelsdl.hwp part seems strange, since I am not calling it at any point, why does it then load it?

Bugala
Posts: 921
Joined: Sun Feb 14, 2010 7:11 pm

Re: I suppose Hollywood doesnt support nested functions?

Post by Bugala »

oh, and p-OS, thanks from the local function tip, that will be very useful when this starts working first.

Bugala
Posts: 921
Joined: Sun Feb 14, 2010 7:11 pm

Re: I suppose Hollywood doesnt support nested functions?

Post by Bugala »

Ah, silly me, just realised where my "bug" is. I only declared the function, never called it.

Now it is working fine when I add line:

Code: Select all

test()
Only thing is, is there any possibility that function declarations inside other functions could be moved to the bottom? Due to that code readability example that I mentioned earlier.

Bugala
Posts: 921
Joined: Sun Feb 14, 2010 7:11 pm

Re: I suppose Hollywood doesnt support nested functions?

Post by Bugala »

And thanks again p-OS, it was thanks to you that I realized where my "bug" was.

User avatar
jPV
Posts: 364
Joined: Sat Mar 26, 2016 10:44 am
Location: RNO
Contact:

Re: I suppose Hollywood doesnt support nested functions?

Post by jPV »

BTW. it's documented here https://www.hollywood-mal.com/docs/html ... cLoc_.html

And it's better to learn declare functions before using them, because this is how it's required in many, many other programming languages too. Bad habits aren't easy to fix then if you need to code other kind of stuff too :)

User avatar
Juan Carlos
Posts: 690
Joined: Mon Sep 06, 2010 1:02 pm
Contact:

Re: I suppose Hollywood doesnt support nested functions?

Post by Juan Carlos »

jPV wrote:
Sun Mar 29, 2020 5:51 pm
BTW. it's documented here https://www.hollywood-mal.com/docs/html ... cLoc_.html

And it's better to learn declare functions before using them, because this is how it's required in many, many other programming languages too. Bad habits aren't easy to fix then if you need to code other kind of stuff too :)
Humm,; I think that is the moment to make one book of Curse of programming with Hollywood, besides now que hace time to write it.

Post Reply