Call By Value/Reference Example
1. Understanding the Script
Functions can be invoked in two ways: Call by Value or Call by Reference. These two ways are generally differentiated by the type of values passed to them as parameters.
The following table lists the differences between the call-by-value and call-by-reference methods of parameter passing.
Call By Value | Call By Reference |
|---|---|
In this method, the value of each variable in the calling function is copied into corresponding dummy variables of the called function. | In this method, the address of actual variables in the calling function is copied into the dummy variables of the called function. |
With this method, the changes made to the dummy variables in the called function have no effect on the values of actual variables in the calling function. | With this method, using addresses we would have access to the actual variables and hence we would be able to manipulate them. |
In call-by-values, we cannot alter the values of actual variables through function calls. | In call by reference, we can alter the values of variables through function calls. |
This method is preferred when we have to pass some small values that should not change. | This method is preferred when we have to pass a large amount of data to the function. |
Call by value is considered safer as original data is preserved | Call by reference is risky as it allows direct modification in original data |
2. Call By Value Example
#include <io>
#include <read>
#include <float>
#include <string>
#include <core>
#include <write>
#include <define>
#include <debug>
#define RS232_SPEED 115200
#define WORD_LENGTH WORDLENGTH_8
#define STOP_BITS STOPBITS_1
#define PARITY PARITY_NONE
new variable_1 = 100;
new variable_2 = 150;
data_swap_function(a, b)
{
new temp_var = a;
a = b;
b = temp_var;
}
main()
{
Init(RS232, RS232_SPEED, WORD_LENGTH, STOP_BITS, PARITY);
debug_init(RS232);
debug_print(" Values before swap:\r\n variable_1 = %i\r\n variable_2 = %i\r\n\r\n", variable_1, variable_2);
data_swap_function(variable_1, variable_2);
debug_print(" Values after swap:\r\n variable_1 = %i\r\n variable_2 = %i\r\n\r\n", variable_1, variable_2);
for(;;)
{
Delay(10000);
}
}2.1. Printed Data
The data output to the RS232 interface using the debug_print() function will appear as follows:
Value before swap: |
|---|
3. Call by reference example
#include <io>
#include <read>
#include <float>
#include <string>
#include <core>
#include <write>
#include <define>
#include <socket>
#include <bluetooth>
#include <debug>
#define RS232_SPEED 115200
#define WORD_LENGTH WORDLENGTH_8
#define STOP_BITS STOPBITS_1
#define PARITY PARITY_NONE
new variable_1 = 100;
new variable_2 = 150;
data_swap_function(&a, &b)
{
new temp_var = a;
a = b;
b = temp_var;
}
main()
{
Init(RS232, RS232_SPEED, WORD_LENGTH, STOP_BITS, PARITY);
debug_init(RS232);
debug_print(" Values before swap:\r\n variable_1 = %i\r\n variable_2 = %i\r\n\r\n", variable_1, variable_2);
data_swap_function(variable_1, variable_2);
debug_print(" Values after swap:\r\n variable_1 = %i\r\n variable_2 = %i\r\n\r\n", variable_1, variable_2);
for(;;)
{
Delay(10000);
}
}3.1. Printed Data
The data output to the RS232 interface using the debug_print() function will appear as follows:
Value before swap: |
|---|
As demonstrated in the example, the variable values in the call-by-reference approach were modified, whereas the values in the call-by-value example remained unchanged.