In solidity, is passing a structure to a function more efficient than passing the individual variables?

I have a function that requires 9 parameters. Therefore I'm wondering what is more efficient? To create a struct, populate it and then pass the struct as one parameter? or to pass each value as individual arguments? Is there any advantage for using one over the other? If so, why?

Hey @nunezlt,

In fact, using structs may help to optimize gas costs but only if it's designed to tightly pack variables. See the layout of variables in storage spec.

Also, according to the same spec:

When dealing with function arguments or memory values, there is no inherent benefit because the compiler does not pack these values.

I'm assuming that a single struct argument will be better because it's packed, which is not the case for variables in calldata :slight_smile: as referenced here.

You can test it by implementing both cases and running transactions with Remix, where you might see the differences in calldata.

So, to answer your question about what's more efficient, I'd say it depends on the context.
You might save costs if EOAs are interacting because of the packing, but if other contracts interact, they might need to write a struct into memory before sending a call.

Best you can do is to do tests with gas measurements I'd say.

Hope this provides a pointer for you :smiley: