Introduction
This article describes the advantages and disadvantages of the form or format that is used for passing parameters to functions. The useful technique described in this article can be used in languages like C, C++, as well as in any language which supports pointers and composite data-types like struct.
Ways of parameter passing to a function
Conceptually, parameters can be passed to a function in two ways, including the combination of these two:
- by State: This way of parameter passing specifies whether the modifications made to parameters by the called function will be available to the caller of the function. Thus, this manner of parameter passing focuses on the state\ value of parameters, before and after the function call.
Under this, there are different actual techniques\ methods to pass parameters to a function:
- by Value: Modification made to parameters by the called function cannot be seen by the caller of the function.
int func(int P1, char P2)
{
P1 = 20; P2 = 'b';
return 1;
}
int Data_P1 = 10;
char Data_P2 = 'a';
func(Data_P1, Data_P2);
cout << "Data_P1 = " << Data_P1 << endl;
cout << "Data_P2 = " << Data_P2 << endl;
- by Reference: Modification made to the parameters by the called function can be seen by the caller of the function.
int func(int &P1, char *P2)
{
P1 = 20; *P2 = 'b';
return 1;
}
int Data_P1 = 10;
char Data_P2 = 'a';
func(Data_P1, &Data_P2);
cout << "Data_P1 = " << Data_P1 << endl;
cout << "Data_P2 = " << Data_P2 << endl;
- There are other techniques as well like by Name etc., the description of which can be easily found on the Internet.
- by Format: This way of passing parameters, by Format, is not something new. In fact, it is used in routine development. This way of parameter passing specifies whether the parameters are passed to a function individually (horizontally) or collectively (vertically).
Therefore, the word Format in the current discussion can be horizontally or vertically.
Under this, there are different actual techniques\methods to pass parameters to a function:
- by Train: Passing parameters individually (horizontally) or in horizontal format can be imagined as if parameters are passed in the form of a train, since a train carries its load (coaches\ bogies) one after the other in horizontal format.
- by Truck: Passing parameters collectively (vertically) or in vertical format can be imagined as if parameters are passed in the form of a truck, since a truck carries its load by keeping things one above the other in vertical format.
Let's see this "by Format" parameters passing method in detail with its pros and cons.
Parameter passing "by train"
int func(int P1, float P2, abc P3, char P4, xyz P5);
As can be seen from the image, there are a number of parameters that are passed to the function func
one after the other, horizontally in the form of a Train, and hence the name "parameter passing by train".
Disadvantages
- When a parameter in the function parameter list gets added, deleted, or modified, the function signature gets modified.
- If parameter
P1
's data-type gets modified from int
to float
, the function signature gets modified.
int func( float P1, float P2, abc P3, char P4, xyz P5);
- If parameter
P2
gets deleted, the function signature gets modified.
int func(int P1, abc P3, char P4, xyz P5);
- If a new parameter
P6
gets added to the parameter list, the function signature gets modified
int func(int P1, float P2, abc P3, char P4, xyz P5, pqr P6);
Advantages
- It is easy to pass some parameters by Value and some parameters by Reference.
int func(int P1, float &P2, abc P3, char *P4, xyz P5);
Parameter passing "by truck"
int func(PARAMETERTRUCK stParam);
struct PARAMETERTRUCK
{
int P1;
float P2;
abc P3;
char P4;
xyz P5;
};
As can be seen from the image, only one (composite\ container-like) parameter is passed to the function func
. This passed parameter can be considered as a Truck which loads all the parameters in it, hence the name "parameter passing by truck".
Advantages
- Even if a parameter in the function parameter list gets added, deleted, or modified, the function signature does not get modified.
- The parameter
P1
's data-type gets modified from int
to float
, but the function signature does not get modified.
struct PARAMETERTRUCK
{
float P1;
float P2;
abc P3;
char P4;
xyz P5;
};
int func(PARAMETERTRUCK stParam);
- The parameter
P2
gets deleted, but the function signature does not get modified.
struct PARAMETERTRUCK
{
int P1;
abc P3;
char P4;
xyz P5;
};
int func(PARAMETERTRUCK stParam);
- A new parameter
P6
gets added to the parameter list, but the function signature does not get modified.
struct PARAMETERTRUCK
{
int P1;
float P2;
abc P3;
char P4;
xyz P5;
pqr P6;
};
int func(PARAMETERTRUCK stParam);
Disadvantages
- All of the parameters can be passed either by Value or by Reference.
int func(PARAMETERTRUCK stParam); int func(PARAMETERTRUCK &stParam); int func(PARAMETERTRUCK *stParam);
We can overcome this disadvantage and allow the mixture of passing some parameters by Value and some parameters by Reference.
Let's see how:
struct PARAMETERTRUCK
{
int P1; char *P2; };
int func(PARAMETERTRUCK stParam)
{
stParam.P1 = 20; *stParam.P2 = 'b';
return 1;
}
int Data_P1 = 10;
char Data_P2 = 'a';
PARAMETERTRUCK stData;
stData.P1 = Data_P1; stData.P2 = &(Data_P2);
func(stData);
cout << "Data_P1 = " << Data_P1 << endl;
cout << "Data_P2 = " << Data_P2 << endl;
Conclusion
By using the technique of passing parameters by truck, we can prevent the signature of a function from getting modified when its parameter list gets modified. At the same time, we can pass some parameters by Value and some parameters by Reference through the use of pointers.
Therefore, use a horizontal list of parameters the least, and pass parameters using a struct
, i.e., "Truck".
There is one strong reason to support the above line. Since, good programming practices (Single Responsibility Principle) suggest that an entity ("function" in our case) should be responsible for perform only one task\ cohesive functionality, all the parameters that will be received by a function should participate or contribute towards the goal\ functionality which the function is trying to provide. Therefore, it is better to pass all the parameters under one name\ goal.
History
- Version 1.0: Initial uploading of article.