Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the pros and cons of using a struct argument v.s. multiple parameters

Suppose I have a function that takes a pair of coordinates x and y:

myFunction(int x, int y)
{
    // Do stuff... 
}

Now, I could wrap those parameters into a Point struct:

struct Point
{
    int x;
    int y; 
}

myFunction(Point p)

Or even provide both options to the programmer:

myFunction(int x, int y)

myFunction(Point p)
{
    myFunction(p.x, p.y)
}

What are the advantages and disadvantages of grouping parameters using structs instead of passing them separately?

Do these trade-offs vary between programming languages?

like image 741
sdgfsdh Avatar asked Oct 21 '25 06:10

sdgfsdh


2 Answers

Structs are the preferred way!

When you have one or two parameters (like in your example) there's no much difference but when you have 4 or more parameters the difference is obvious (you have to type less code).

And if you want to pass the parameters as pointers or references you've to type even less code.

The other advantage using structs is that the code is easier to maintain.

Let's see it in practice using your own example

Imagine you're creating a CAD application and in the original idea the program work in a plane (X, Y) only so you'll have lot of functions using as parameter one or more points, something like this:

struct point
{
  int x;
  int y;
}

func Foo1(point a) {}
func Foo2(point a, point b) {}
.
.
.

But after some months the application design change and now it needs to work in the space (3D world) so you need to add the Z coordinate to the points.

If you're using a struct the only thing you need to do is to add the Z variable to the struct and done (of course, you need to update the formulas in the functions) but if you were not using a struct you need to add the Z parameter to every function that use a point as parameter.

func Foo1(int x, int y, int z) {}
func Foo2(int p1x, int p1y, int p1z, int p2X, int p2Y, int p2Z) {}

You can see a more detailed explanation here https://en.wikipedia.org/wiki/Struct_(C_programming_language)

like image 115
vcRobe Avatar answered Oct 23 '25 21:10

vcRobe


This is going to be highly language-specific. Structs are natural to create in some languages, not in others. Named parameters are natural in some languages, not in others. Some languages do not distinguish between your two cases (consider ML where an anonymous tuple of two values is basically the same thing as a struct).

This is also going to be highly context-specific. If you do a lot of work with points, then it makes a lot of sense to have a type that encapsulates points. If this is the only function in the whole system that takes a point, then creating a special type for that may be very cumbersome. There is no one answer to "should I use a type here" (which is the better version of your question).

Don't focus on the function arguments. Focus on the types. What types make sense in your system? Then create functions that work on those types.

like image 27
Rob Napier Avatar answered Oct 23 '25 21:10

Rob Napier



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!