Author Topic: Function inside function  (Read 55 times)

acbaile

  • Jr. Member
  • **
  • Posts: 66
    • View Profile
Function inside function
« on: September 22, 2018, 02:17:24 AM »
Sometime it is very convinient to define function inside function. Like subroutine. With rules of visibility for local variables. Assembler analog - "CALL lable".

It is not very important wish, but, if you can and have free time... :)

bas

  • Full Member
  • ***
  • Posts: 177
    • View Profile
Re: Function inside function
« Reply #1 on: September 24, 2018, 12:20:16 PM »
Features like this are not used very often in C and do make a language more complex than needed. So it was removed.

magnusi

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: Function inside function
« Reply #2 on: November 09, 2018, 11:44:55 PM »
If it has visibility of local variables, then we are talking about a closure rather than a function and when you want to have closures in your language, you need to be wary of many factors, for example:

  • Do you clone the values of the local vars as they were when the closure was created?
  • Do you keep a reference or move them into the closure?
  • Can the closure be freed? If so, how? You probably wouldn't want a memory leak

Solving these is probably beyond the scope of this language.

acbaile

  • Jr. Member
  • **
  • Posts: 66
    • View Profile
Re: Function inside function
« Reply #3 on: November 14, 2018, 09:00:51 AM »
  • Do you clone the values of the local vars as they were when the closure was created?
  • Do you keep a reference or move them into the closure?
  • Can the closure be freed? If so, how? You probably wouldn't want a memory leak

The idea was to have a part of code inside function with possibility to call it many times. Like call by local label in assembler - without usual rules of saving registers and passing parameters. Operating with variables is like this is high-function's code. I did not think about passing arguments inside even, it seems you simply don't need it in this idea - you can access them directly.

But, i agree: this feature is not used often, and it can be difficult to realize it. It's too expensive.
« Last Edit: November 14, 2018, 08:33:03 PM by acbaile »

lerno

  • Full Member
  • ***
  • Posts: 159
    • View Profile
Re: Function inside function
« Reply #4 on: November 14, 2018, 06:30:42 PM »
This C feature is only in GCC/Clang or?