Krupski

February 17th, 2009, 03:20 PM

Hi all again...

(Note: I am not a student and this is not homework)

Quick question:

If I have a function that I wrote, and I pass parameters to it, and the function modifies the parameters, should I allow this or should the function make local copies of the parameters?

For example, I have a function:

void linedraw(*x0, *y0, *x1, *y1);

Which generates all the points of a line between start x0,y0 and end x1,y1.

In the process of working, the "x" and "y" parameters might get swapped so that, if I did this:

// show vars before call

printf("Before: %d, %d, %d, %d\n", x0, y0, x1, y1);

// call linedraw function

linedraw(&x0, &y0, &x1, &y1);

// show vars after call

printf("After : %d, %d, %d, %d\n", x0, y0, x1, y1);

The result might be:

Before: 1, 10, 5, 20

After : 10, 1, 20, 5

---or it could be---

Before: 1, 10, 5, 20

After : 1, 10, 5, 20

...depending on if the function needed to swap the variables.

Now, once I call "linedraw", I don't need the variables anymore (that is, if they get swapped I don't care).

But my question is, for GOOD PROGRAMMING PRACTICE, should my function make local copies of the parameters so that the originals don't get swapped, or is it OK to let whatever happens, happen?

Hope this makes sense... and thanks in advance for info.

-- Roger

(Note: I am not a student and this is not homework)

Quick question:

If I have a function that I wrote, and I pass parameters to it, and the function modifies the parameters, should I allow this or should the function make local copies of the parameters?

For example, I have a function:

void linedraw(*x0, *y0, *x1, *y1);

Which generates all the points of a line between start x0,y0 and end x1,y1.

In the process of working, the "x" and "y" parameters might get swapped so that, if I did this:

// show vars before call

printf("Before: %d, %d, %d, %d\n", x0, y0, x1, y1);

// call linedraw function

linedraw(&x0, &y0, &x1, &y1);

// show vars after call

printf("After : %d, %d, %d, %d\n", x0, y0, x1, y1);

The result might be:

Before: 1, 10, 5, 20

After : 10, 1, 20, 5

---or it could be---

Before: 1, 10, 5, 20

After : 1, 10, 5, 20

...depending on if the function needed to swap the variables.

Now, once I call "linedraw", I don't need the variables anymore (that is, if they get swapped I don't care).

But my question is, for GOOD PROGRAMMING PRACTICE, should my function make local copies of the parameters so that the originals don't get swapped, or is it OK to let whatever happens, happen?

Hope this makes sense... and thanks in advance for info.

-- Roger