Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

call a function/sub

Status
Not open for further replies.

bateman23

Programmer
Mar 2, 2001
145
DE
Hi

i think it's a simple question - but i'm just beginning to learn c++, so..... ;-)
How can i call a function or a sub from within another function/sub ?
I know there is a statement like:
result = Function_to_call(input);
,but this doesn't work in my case:

void __fastcall TForm2::Button1Click(TObject *Sender)
{
//Close that form
Form2 -> Close();
}
//------------------------

void __fastcall TForm2::Beenden1Click(TObject *Sender)
{
//Also Close the form -> How to call the function above?
//what code do i need here?
}
//------------------------

BTW: Does anybody know a good online-tutorial / or a book for learning C++ ?

Thanks a lot,
bateman23
---------------------------------------
Visit me @:
 
Ahhh, looks like your coming from a vb background. In c/c++ functions are a bit different.

in vb
private sub ex1(by ref x as integer)
x = 7;
end sub

would translate to

void ex1(int& x)
{
x = 7;
}

////////////////////

private sub ex2(by val x as integer)
// stuff
end sub

would translate to

void ex2(int x)
{
// stuff
}

/////////////////////////

private function ex3(x as integer) as integer
{
ex3 = x*3;
}

would translate to

int ex3(int x)
{
return x*3;
}

/////////////////////////////

if you were not coming from a vb background here is the other half :)

Form2 must be know of by the function you are calling. If you are trying to call a function for the current form (i.e. trying to close the current form) which it appears you are trying to do you could either type

Close();
or
this->Close();

and in BeendenClick you would just type

Button1Click()
or
this->Button1Click()


Hope that helped.

Matt

 
Yes it helped...
Thanks a lot for the really good explanation!
And, indeed i'm coming from vb background. ;-)

But, one thing:
When trying to call the function with
"this->Button1Click();" the compiler complained about "less arguments for calling _fastcall....." (something like that).
So i had to call it:
"this->Button1Click(this);" - do i always have to pass an argument? (on what is the "this" referring) ?


Thanks
Daniel aka bateman23 ---------------------------------------
Visit me @:
 
Oh... my bad.. i missed the TObject* Sender argument for Button1Click. You have to pass the required arguments of the function.

Matt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top