Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers DBPro Corner / Function in Function?

Author
Message
Yitzu
15
Years of Service
User Offline
Joined: 14th Feb 2009
Location:
Posted: 1st Mar 2010 04:38
Simple question, can i have a function inside another function?
Neuro Fuzzy
17
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 1st Mar 2010 05:23
no you can't.

And... how do you think it would have any use?


Masqutti
14
Years of Service
User Offline
Joined: 8th Jan 2010
Location: insanity
Posted: 1st Mar 2010 07:36
What? I'm not sure what do you guys mean but at least this is possible:



Outcome from this would be:

1
2

There's a function inside another.. And I know it works, Did i miss the point ?

hmmmh.. that didn't compile
Peter Parker
15
Years of Service
User Offline
Joined: 17th Apr 2009
Location:
Posted: 1st Mar 2010 08:49
Hi.

What you did is CALLING another function inside a function. You can do that. But because of what you asked everyone understands the following:

luskos
17
Years of Service
User Offline
Joined: 28th Jun 2007
Location:
Posted: 1st Mar 2010 15:26
As Peter Parker aka Spiderman says you can call funcion isnide another.

Where there is a will, there is a way.
Neuro Fuzzy
17
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 1st Mar 2010 15:45
blagh, didn't catch that

yeah, you can't declare a function inside a function but you can call it.


TDK
Retired Moderator
21
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 1st Mar 2010 16:06
The whole purpose of a function (and a subroutine) is primarily to save you writing the same section of code over and over again.

If your program needs to do the same task in six different places, and the code required each time is say 25 lines, that's 150 lines of code.

Putting those 25 lines of code in a function or subroutine means you only need a single line calling the function (or GOSUBing the subroutine) at each of the six locations. This means a total of 31 lines in your program instead of 150 - plus a saving of an additional 24 lines every time you need to add the same code elsewhere in your program!

With functions, you also have the added benefit of easily being able to pass different parameters and therefore use the same code to handle different conditions.

So, knowing this, you should be able to understand how pointless the notion of a function inside another function would actually be.

But, I assume that you meant what someone else has already suggested - that you are talking about calling another function from within a function. Which is of course OK.

Remember though that with functions, (unlike subroutines), you have local variables and variable scope to contend with - both of which are covered in the tutorial I pointed you to in your other thread.

TDK

Yitzu
15
Years of Service
User Offline
Joined: 14th Feb 2009
Location:
Posted: 1st Mar 2010 20:00
Ok, ty, actually what i meant to say was what Peter Parker said, but now i see is kinda dumb... so, thx
IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 1st Mar 2010 23:06
Actually, it's not dumb idea at all. There are languages that allow it to great effect.

What happens is the the nested function is only accessible from code within the function - you'd use it for repeated pieces of code within a function that are not called from anywhere else and that you don't want to make globally available.

the game creators 2
14
Years of Service
User Offline
Joined: 1st Mar 2010
Location:
Posted: 2nd Mar 2010 05:06
Did you mean a recursive function or what Yitzu ??

if yes, you could do it..!!! Let me know..!!

Lara..!!

.

Neuro Fuzzy
17
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 2nd Mar 2010 15:49
Quote: "What happens is the the nested function is only accessible from code within the function - you'd use it for repeated pieces of code within a function that are not called from anywhere else and that you don't want to make globally available.
"


didn't make the connection to private/public functions either

Quote: "if yes, you could do it..!!! Let me know..!!"


recursive functions are possible. EX:



Login to post a reply

Server time is: 2024-09-28 14:21:11
Your offset time is: 2024-09-28 14:21:11