flat assembler
Message board for the users of flat assembler.

Index > High Level Languages > Function in function

Author
Thread Post new topic Reply to topic
lamar



Joined: 05 Jun 2009
Posts: 16
lamar 23 Dec 2009, 12:00
How in C++ do like that:
Code:
void MainFunction(int hWnd,int SomeParam1,int SomeParam2)
{
 int AdditionalFunction()
 {
  return hWnd+2;
 }

 return AdditionalFunction();
}
    

HALP! In delphi that possible Crying or Very sad
Post 23 Dec 2009, 12:00
View user's profile Send private message Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr 23 Dec 2009, 13:43
lamar,

In C++ that is impossible.
Post 23 Dec 2009, 13:43
View user's profile Send private message Reply with quote
Tyler



Joined: 19 Nov 2009
Posts: 1216
Location: NC, USA
Tyler 24 Dec 2009, 00:07
Code:
int MainFunction(int hWnd,int SomeParam1,int SomeParam2) 
{
     return AdditionalFunction(hWnd);//Void functions cannot return stuff, since AdditionalFunction is an int make MainFunction an int.
}

int AdditionalFunction(int hWnd)
{
     return hWnd+2;//In oreder to use hWnd you need to pass it as a param or declare it in AdditionalFunction.
}
    

But for any of them to be used you have to declare them like variables(kinda), you do that like this,
Code:
int MainFunction(int hWnd,int SomeParam1,int SomeParam2);//Notice the ;
int AdditionalFunction();
    

Also, all the stuff I made comments on being wrong I fixed, look at your original code to find the difference. You should consider learning C first, it's more fun.
Post 24 Dec 2009, 00:07
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4624
Location: Argentina
LocoDelAssembly 24 Dec 2009, 01:02
Tyler, I think he means the nested function/procedure capabilities Delphi/TurboPascal have.

Code:
procedure foo(a: integer)

  function bar(): integer
  begin
    bar := 4 + a; { Notice it can access parameters and variables from its parent }
  end;

begin
  WriteLn(bar());
end;

{ bar can't be called from here }    


The scope part perhaps could be solved by putting the foo and bar in a separate file and make bar static, but the other aspect may not be so easy/safe to emulate and you would have to manually pass the variables/arguments bar requires.
Post 24 Dec 2009, 01:02
View user's profile Send private message Reply with quote
TmX



Joined: 02 Mar 2006
Posts: 841
Location: Jakarta, Indonesia
TmX 24 Dec 2009, 02:53
GCC does support nested functions.

Unfortunately, it's only available for C (and not C++) Wink
Post 24 Dec 2009, 02:53
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4624
Location: Argentina
LocoDelAssembly 24 Dec 2009, 03:12
Quote:

Unfortunately, it's only available for C (and not C++)

But thank you very much for mentioning it, I didn't know there was support for this.
Post 24 Dec 2009, 03:12
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.