Return Value & Pass by reference

Dear Sir,

I have Created a recursive Function called SetLevel which has two Parameter one is
Para1 ( Datatype Code) and another is
Para2 ( Datatype Integer), and Para2 is using as Pass by Reference.

I Want this function should return the Para2 as Return Value (Return Type Integer).

Is it Possible as the way I want ?

Tab.Field :=SetLevel (Value1,Value2)
Tab.Modify;

Function SetLevel(HierarchyNo,Level)
Level +=1;
Cust2.SETRANGE("Hierarchy No.",HierarchyNo);
IF Cust2.FINDFIRST THEN
BEGIN
HLine2.SETRANGE("Customer No.",Cust2."No.");
IF HLine2.FINDFIRST THEN
BEGIN
SetLevel(HLine2."Hierarchy No.",Level);
END
ELSE
EXIT(Level);
END;

Here SetLevel does not give the value where value2 gives.

What is the reason ?
Kindly reply.
Thank you.

Recursion can be tricky. Think about it this way. In your function, for the recursive call SetLevel(HLine2.“Hierarchy No.”, Level), what will that evaluate to? Some number right. You create a new copy of that same function in memory, execute that copy, then get the result. In the code you can replace that function call with the result.

So you are left with

IF HLine2.FINDFIRST THEN
BEGIN
Some Number
END

Notice you’re not returning that number. You really want something like:

IF HLine2.FINDFIRST THEN
BEGIN
```EXIT(SetLevel(HLine2.“Hierarchy No.”,Level)); END`