Passing Structures to Functions:
The whole structure can be passed to a function, or separate fields can be passed. For illustration, here are the two distinct versions of a function which computes the profit on a software package. The profit is then defined as the price minus the cost.
In the first version, the whole structure variable is passed to the function, therefore the function should use the dot operator to refer to the price and cost fields of the input argument.
>> calcprof(package)
ans =
19.9600
In the second version, merely the price and cost fields are passed to the function by using the dot operator in the function call. These are passed to the two scalar input arguments in function header; therefore there is no reference to a structure variable in the function itself, and the dot operator is not required in the function.
>> calcprof2(package.price, package.cost)
ans =
19.9600
It is significant, as always with the functions, to make sure that the arguments in the function call correspond one-to-one with the input arguments in the function header. In case of calcprof, the structure variable is passed to an input argument that is a structure. For second function calcprof2, the two individual fields, that is double values, and is passed to two double arguments.