Return Value & Pass by reference

navuser1navuser1 Member Posts: 1,329
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.
Now or Never

Comments

  • bbrownbbrown Member Posts: 3,268
    SetLevel (Value1,Value2);
    Tab.Field := Value2;
    Tab.Modify;
    
    There are no bugs - only undocumented features.
  • navuser1navuser1 Member Posts: 1,329
    bbrown wrote:
    SetLevel (Value1,Value2);
    Tab.Field := Value2;
    Tab.Modify;
    

    Already I have used this method. But I want to know from you Sir.
    Is it not possible in the way I want ?? :-k

    BTW Thank you Sir for your reply. =D>
    Now or Never
  • mdPartnerNLmdPartnerNL Member Posts: 802
    it can only return the value, not a reference. Just use the TAB "return value"
  • vaprogvaprog Member Posts: 1,139
    navuser1 wrote:
    Para2 ( Datatype Integer), and Para2 is using as Pass by Reference.
    Be careful, recursive funcions with VAR parameters will almost cerainly byte you!, especially so when you reuse the parameter for the recursive call.
    navuser1 wrote:
    I Want this function should return the Para2 as Return Value (Return Type Integer).
    Use EXIT(Para2) or assign Para2 to the name you have given the return parameter.
    navuser1 wrote:
    Is it Possible as the way I want ?
    From your code sample it is unclear "the way I want".

    The function SetLevel needs to define a return type. The way you gave it to us, a compile error will result.
    Moreover make sure the desired return value is defined on any way out.
    And then, this code is VERY liekely to produce endless loops. You need to saveguard against it.
    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);
        // No return value defined here!
       END
      ELSE
        EXIT(Level);
        // This statement only compiles when the function defines a return value (type)
    END;
    // No return value defined here!
    
  • navuser1navuser1 Member Posts: 1,329
    Be careful, recursive funcions with VAR parameters will almost cerainly byte you!, especially so when you reuse the parameter for the recursive call.
    Ok.
    Use EXIT(Para2) or assign Para2 to the name you have given the return parameter.
    already done
    From your code sample it is unclear "the way I want".

    I want that The function should return the desired result not the variable only.

    The function SetLevel needs to define a return type.
    Already done
    The way you gave it to us, a compile error will result.
    Still not facing any error.

    Moreover make sure the desired return value is defined on any way out.
    And then, this code is VERY liekely to produce endless loops. You need to saveguard against it.
    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);
        EXIT(Level); // I have added this line and my problem seems to solve
        // No return value defined here!
       END
      ELSE
        EXIT(Level);
        // This statement only compiles when the function defines a return value (type)
    END; // Ok
    // No return value defined here![ not necessary, I feel]
    
    Now or Never
Sign In or Register to comment.