Skip to Main Content
  • Questions
  • Passing function as an input parameter to another function

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, jamal.

Asked: April 22, 2016 - 7:13 pm UTC

Last updated: April 27, 2016 - 4:24 am UTC

Version: 11g

Viewed 10K+ times! This question is

You Asked

i need your help/guidance to pass function as input parameter to another function. i need some good examples

and Connor said...

Do you mean something like this ?

create or replace
function F1 return number is
begin
  return 17;
end;
/

create or replace
function F2(p_input int) return number is
begin
  return p_input+20;
end;
/

create or replace
function F3(p_input int) return date is
begin
  return sysdate + p_input;
end;
/

declare
  x date;
begin
  x := f3(f2(f1));
end;
/



Rating

  (3 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Functions cannot be passed as objects

Jeffrey Kemp, April 23, 2016 - 11:44 pm UTC

I suspect the OP means to pass the function itself, a common technique in other languages like Javascript.

In PL/SQL, functions are not objects, and they cannot be passed as a parameter (e.g. to be executed by the called program). In the example above, the functions aren't passed - instead, they are executed once, in order, and the result is passed.
Connor McDonald
April 24, 2016 - 6:19 am UTC

Thanks for stopping by Jeff.

Cheers,
Connor

pass a function => pointer to function in C ??

david scott, April 25, 2016 - 8:05 pm UTC

I am wondering if the original questioner was looking for a way to use a function by supplying its name to a block, so that whenever the block was run next, it would do so with the name of a function provided, then invoke that function wherever needed in dynamic fashion. perhaps the matter could be restated as "is there a way to dereference other than a lengthy IF construct?"

IF inf_name = 'F1' THEN f1(some_args)
ELSIF inf_name = 'F2' then f2(some_args)
ELSE f3(some_args)
END IF;

I am personally unaware of either a method or a necessity for this ability, but would like the experts statement.
Connor McDonald
April 27, 2016 - 4:24 am UTC

It could all be done dynamically, ie,

execute immediate 'begin '||l_my_routine_name||'; end;'

but let me be clear - I think that's a really really bad idea!

Alternate Approach

Deb, October 11, 2016 - 8:55 pm UTC

One can pass functions as parameter to a function as using Types.

More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library