Saturday, November 28, 2009

What is Forward Declaration?

In PL/SQL (and other languates like Pascal) a program unit such as a procedure or function must be defined before it can be invoked by another program unit. In PL/SQL package bodies forward declarations provide an optional means to get around this. You declare all program units before they actually appear in the package. A forward declaration is merely the name of the program unit and any parameters required. With forward declarations in place you are now free to arrange the program units in the package body in any order so as to improve readability or group program units together logically. Note that forward declarations are not required as long as you code a program unit before it is called. Additionally forward declarations can appear anywhere not just at the top of the package body. Anytime you declare a program unit in advance of the actual coding that is a forward declaration.

Example:
procedure A is
begin
B;
end A;

procedure B is
begin
null;
end B;

Will not work because during call to B, B is still unknown (1 Step compiler) therefore we need a forward declaration,

procedure B;

procedure A is
begin
B;
end A;

procedure B is
begin
null;
end B;

Now we can compile

Source=> Net

3 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. I like your blog. This post gives some frequently asked questions and answers on PL/SQL. This is very good work. I am looking for some question ad answers on Java. Please include them in your next post. Thanks for this post.
    sap test

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete