Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

How do you Call a function/procedure from another unit. SIMPLE?


  • Please log in to reply
6 replies to this topic

#1 standard

standard

    CC Lurker

  • Just Joined
  • Pip
  • 4 posts

Posted 20 April 2010 - 04:48 PM

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
  • 0

#2 Firebird_38

Firebird_38

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 117 posts

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:
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...

  • 1

Posted Image


#3 Firebird_38

Firebird_38

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 117 posts

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).
  • 0

Posted Image


#4 standard

standard

    CC Lurker

  • Just Joined
  • Pip
  • 4 posts

Posted 20 April 2010 - 05:24 PM

Thanks for your help!

All works fine
  • 0

#5 nursb

nursb

    CC Lurker

  • Just Joined
  • Pip
  • 2 posts

Posted 05 November 2013 - 01:34 AM

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).

What if in Unit1

  instead of   type

                  TMyType=record
                Num1,Num2:Integer;
                 end;

we have

    sometype=class(..)

          procedure someproc(param1,param2);...

    end;

and I want to call that someproc in Unit2 ??


Edited by nursb, 05 November 2013 - 01:38 AM.

  • 0

#6 Luthfi

Luthfi

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1320 posts
  • Programming Language:PHP, Delphi/Object Pascal, Pascal, Transact-SQL
  • Learning:C, Java, PHP

Posted 06 November 2013 - 10:45 AM

Basically, you put anything you want to be accessible from other units in the interface section.

 

For the following example unit, Variable1, Variable2, TMyRecord1, and TMyClass1 are accessible from other units. Of course those other units must put this unit into their uses list. But, Variable3, Variable4, TMyRecord2, and TMyClass2 will not accessible from other unit.

unit MyUnit;

interface

var
  Variable1: Integer;
  Variable2: string;
 
type
  TMyRecord1=record
    ...
  end;

  TMyClass1=class
    ...
  end;

function MyFunction1(..): variant;

implementation

var
  Variable3: string;
  Variable4: string;

type
  TMyRecord2=record
    ...
  end;

  TMyClass2=class
    ...
  end;

function MyFunction2(...): string;
begin
  ...
end;

// This is implementation of MyFunction1 declared in the interface section.
// Not a new function.
function MyFunction1(..): variant;
begin
  ...
end;

Classes/objects have another accessibility modifier. Basically there are 3 "protection" in class. They are private, protected, and public.

 

Members of private section originally intended to be accessible only from within the class itself. However in implementation they are accessible from any part of the same unit, as long as the accessing part located after the members they are interested in. Relatively new modifier strict actually will uphold the original intention, but only available in newer Delphi.

 

Members of protected section originally intended to be accessible from the class and its descendant. But it also has the same "weakness" of private section. Strict modifier also applicable for protected.

 

Members of public section are accessible to anything that has access to the class itself.

 

Some may argue that there is a fourth one, published. But published actually is not different with public, except it is used (originally) internally by Delphi IDE to "mark" members which information should be accessible in design time (e.g. to show properties in Object Inspector).

 

 

What if in Unit1

  instead of   type

                  TMyType=record
                Num1,Num2:Integer;
                 end;

we have

    sometype=class(..)

          procedure someproc(param1,param2);...

    end;

and I want to call that someproc in Unit2 ??

 

In this case, you have implicitly declared someproc method in public section. So it is accessible to anything that has access to sometype class. If you declare sometype class  in interface section, then it will accessible from any unit which uses it.

 

But note that someproc is a method, means that you need valid object instance for you to be able to execute it. Otherwise it would generate access violation error or invalid pointer operation (errors which related with accessing invalid memory location).

 

So, if you want to execute someproc. You need to create an instance of sometype, and then call someproc of this instance. Something like this:

var
  MySomeType: sometype;
begin
  MySomeType := sometype.Create;
  try
    MySomeType.someproc(...);
    ..
  finally
    MySomeType.Free;
  end;
end;

Edited by Luthfi, 08 November 2013 - 09:39 AM.

  • 1

#7 nursb

nursb

    CC Lurker

  • Just Joined
  • Pip
  • 2 posts

Posted 06 November 2013 - 10:16 PM

finally
MySomeType.Free;
end;
end;

thanks a lot!


  • 0




Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download