Passing parameters to procedures and functions by reference and by value

1C:Enterprise script supports two methods of passing parameters to procedures and functions: by reference and by value.

Passing parameters by reference

By default, 1C:Enterprise script passes parameters to procedures and functions by reference. In other words, changing a formal parameter within a procedure or function affects the actual parameter value that is passed to the procedure or function.

Example:

Procedure Procedure1(FormalParameter1)
    FormalParameter1 = 2 * 3;
EndProcedure

ActualParameter = 10;

Procedure1(ActualParameter);
    // Value "6" will be displayed. Changing a formal
    // parameter within a procedure affects the  
    // actual parameter value that is passed to the
    // procedure. The parameter is passed using the 
    // default method: by reference.
    Message(ActualParameter);

Passing parameters by value

When a parameter is passed to a procedure or function by value, changing the formal parameter value within a procedure does not affect the actual parameter that is passed to the procedure or function. The Val keyword specifies that a parameter is passed by value.

Example:

Procedure Procedure2(Val FormalParameter1)
    FormalParameter1 = 2 * 3;
EndProcedure
ActualParameter = 10;
Procedure2(ActualParameter);
    // Value "10" will be displayed. Changing a formal
    // parameter within a procedure does NOT affect the  
    // actual parameter value that is passed to the
    // procedure. The parameter is passed by value.
    Message(ActualParameter);

Specifics of passing variales of specific types by value

Passing variables by value has its specifics based on the variable type. Calling context methods or properties of a formal parameter might change the state of the actual parameter passed to the procedure or function.

Example 1:

A value table is passed to a procedure by value. A new instance of the value table is created within the ProcessTable() procedure, and then three rows are added to the instance. This does not affect the value table VT that is passed as an actual parameter to the ProcessTable() procedure.

// The ValTable parameter is passed by value
Procedure ProcessTable(Val ValTable) 
    // Creating a value table 
    ValTable = New ValueTable;
    // Adding two columns to the value table
    ValTable.Columns.Add("Column1"); 
    ValTable.Columns.Add("Column2"); 
    // Adding rows to the value table
    For n = 1 To 3 Do
        NewRow = ValTable.Add(); 
        NewRow.Column1 = "Column"; 
        NewRow.Column2 = n; 
    EndDo; 
EndProcedure
// Creating a value table
VT = New ValueTable; 
// Adding three columns to the table
VT.Columns.Add("Column1"); 
VT.Columns.Add("Column2"); 
VT.Columns.Add("Column3"); 
// Adding rows to the value table
For n = 1 To 10 Do
    NewRow = VT.Add(); 
    NewRow.Column1 = "Column1"; 
    NewRow.Column2 = "Column2"; 
    NewRow.Column3 = n; 
EndDo; 
// Value "10" will be displayed
Message("Number of elements in VT before processing: " + VT.Count());
// Calling the procedure with passing actual parameter VT by value
ProcessTable(VT);
// Value "10" will be displayed: new value table is saved
// to a formal parameter inside the ProcessTable procedure
Message("Number of elements in VT after processing: " + VT.Count());

Example 2:

A value table is passed to a procedure by value. The passed value table is grouped within the GroupTable() procedure.

// The ValTable parameter is passed by value
Procedure GroupTable(Val ValTable) 
    // Grouping the value table by Column1 and Column3 
    ValTable.GroupBy("Column1", "Column3"); 
EndProcedure
// Creating a value table
VT = New ValueTable;  
// Adding three columns to the table
VT.Columns.Add("Column1"); 
VT.Columns.Add("Column2"); 
VT.Columns.Add("Column3");  
// Adding rows to the value table
For n = 1 To 10 Do
    NewRow = VT.Add(); 
    NewRow.Column1 = "Column1"; 
    NewRow.Column2 = "Column2"; 
    NewRow.Column3 = n; 
EndDo; 
// Value "10" will be displayed
Message("Number of elements in VT before grouping: " + VT.Count());
// Calling the procedure with passing actual parameter VT by value
GroupTable(VT);
// The value table is grouped; value "1" will be displayed
Message("Number of elements in VT after grouping: " + VT.Count());

The following figure illustrates what happens when the GroupTable(VT) procedure is called:

(1) Calling the GroupTable procedure. The actual procedure parameter is the VT variable that stores a reference to a value table instance.

(2) Calling the GroupBy method of the value table within the procedure.

(3) The formal parameter references the same value table instance as the actual parameter (the VT variable); grouping the value table instance that is referenced by the VT variable.

(4) The procedure is executed; the value table instance that is referenced by the VT variable is grouped.

Therefore, passing an actual parameter by value in this example does not lead to the creation of a value table copy. Calling methods or properties of the value table actually calls methods or properties of the value table that was passed to the GroupTable procedure.

For all types, the difference between passing parameters by reference and by value is in assigning a new value to the actual procedure or function parameter. Calling methods and properties of an actual parameter context (if it has any) affects the formal parameter regardless of the passing method.

Next page: Recursion in the script


Be the first to know tips & tricks on business application development!

A confirmation e-mail has been sent to the e-mail address you provided .

Click the link in the e-mail to confirm and activate the subscription.