Ok so say i have a function called 'Function1' in the unit named 'Unit1'.
I want to call this function in Unit 2. if i call the function on its own such as
//unit 2
begin
result := function1[parameter];
end;
it does not recognise the function.
Sure its simple but any help would be appreciated! thanks in advance
How do you Call a function/procedure from another unit. SIMPLE?
Started by standard, Apr 20 2010 04:48 PM
3 replies to this topic
#1
Posted 20 April 2010 - 04:48 PM
|
|
|
#2
Posted 20 April 2010 - 05:06 PM
First off, decide which function in unit1 you want to "share". For instance... Then add those to the "interface" section, but only the header (first line with name and params).
Then, in the "using" unit, add a uses clause (if it's not already there).
uses unit2;
Now you can use the functions defined in the interface of unit1 as if they were defined in unit2. The same goes for types, constants and global variables. Hit ctrl-enter on someone else's unit name in your uses clause to view it's source and see how others do it.
Example:
And unit2:
This is written off the top of my head, so if it doesn't work don't kill me. I didn't compile to see if I misspelled anything... or forgot something... :)
Happy programming.
Then, in the "using" unit, add a uses clause (if it's not already there).
uses unit2;
Now you can use the functions defined in the interface of unit1 as if they were defined in unit2. The same goes for types, constants and global variables. Hit ctrl-enter on someone else's unit name in your uses clause to view it's source and see how others do it.
Example:
unit unit1;
interface //this is the part you "share" with other4 units, or the "public" part
//You can have a uses clause here, if you're using types or constants from other units in your interface section.
//If you only use things in the implementation, don't add the uit to your interface. This is bad coding...
//That has to do with circular references...
//Units cannot refer to [I]each other[/I] in the interface sections, but one can refere to the other in the interface, and the other can refer back, but only in its implementation...
//Try it and see you get a "circular unit reference" error.
type
TMyType=record
Num1,Num2:Integer;
end;
function test(param:TMyType):string;
implementation //this is where the stuff is that is hidden from anyone that "uses" this unit
uses SysUtils; //stuff units you use here is only available from here on down...
function test(param:TMyType):string;
begin
result:=IntToStr(param.Num1+param.Num2); //this is just a silly test...
end;
end.
And unit2:
unit unit2; interface procedure Go; implementation uses Dialogs,[B]unit1[/B]; //here I'm "stating" that I wanna use the stuff in unit1's interface section... procedure Go; var param:TMyType; begin //To call this proc, you must use this unit in a unit that actually does something... :) param.Num1:=10; param.Num2:=15; ShowMessage(test(param)); //"ShowMessage" is defined and implemented in the "Dialogs" unit, used above... end; end.
This is written off the top of my head, so if it doesn't work don't kill me. I didn't compile to see if I misspelled anything... or forgot something... :)
Happy programming.
Edited by Firebird_38, 20 April 2010 - 05:10 PM.
Oops, forgot to use the unit...
#3
Posted 20 April 2010 - 05:07 PM
I just saw you had an error: Your function call uses [] (square brackets) to delimit the parameters. This is wrong, you have to use () (parentheses).
#4
Posted 20 April 2010 - 05:24 PM
Thanks for your help!
All works fine
All works fine


Sign In
Create Account

Back to top










