Why Doesn't My Swap Function Change the Original Values in C?

  • Thread starter flemmyd
  • Start date
In summary: To modify variables in the calling function you have to pass pointers to those variables. Since you are not allowed to use pointers, you cannot modify variables in the calling function.
  • #1
flemmyd
144
1

Homework Statement


I'm just writing a simple program to try and take the reciprocal and keep the value logged.

Homework Equations


? not a math problem..

The Attempt at a Solution



My code:

int = numerator, denominator; // global variables

void swap(int numerator, int denominator)
{
int temp = numerator;
numerator = denominator;
denominator = temp;
}

int main (void)
scanf("%d", numerator);
scanf("%d", denominator);

while (input != quit) //i'm omitting some lines here...
printf("The current fraction is %d/%d", numerator, denominator);

case 1:
swap(numerator, denominator)

That should be everything that is relevant for this program right now. I didn't want to copy everything out (most of it is junk right now).

Anyway, so I input the values in for numerator and denominator (of the type int). it gives me the value fine. when i use the swap function, it should give me the reciprocal (aka swap them) and it does-- if i insert a printf line in the function, it does give the reciprocal. but when it comes back the the main function, right under the while loop, it gives me the original number.

So i know about passing by reference and passing by value. It seems like when i leave the scope of the swap function and the main values aren't changing, it's a pass by value and not reference. I don't know how to get this to pass by reference without using a unit 1 array or a pointer (of which I'm apparently not allowed to use for the assignment).

Because I'm trying to return two values, I can't use
int swap
{
//blah

return a
}
right?

any one have any ideas?

EDIT: now that I think about it, is it a scoping issue? how can I define the equations such that the numbers are beyond the scope of the function?
 
Last edited:
Physics news on Phys.org
  • #2
Your problem is that you are confusing yourself in the naming of your variables.
The denominator and numerator you created in the function parameters for swap is not the same numerator and denominator you created at the outset.

Just because you called them the same name doesn't make them equal. Also nowhere in your code are you returning the results from swap to the original numerator and denominator. :-\

The way it is, if you initialed numerator and denominator in swap to be zero when you do your printf you would get the original numerator and denominator which are probably nonzero.
 
  • #3
flemmyd said:
So i know about passing by reference and passing by value. It seems like when i leave the scope of the swap function and the main values aren't changing, it's a pass by value and not reference. I don't know how to get this to pass by reference without using a unit 1 array or a pointer (of which I'm apparently not allowed to use for the assignment).
C does not have pass by reference. It has pass by value only. (A pointer is just another kind of value in C).
 

FAQ: Why Doesn't My Swap Function Change the Original Values in C?

1. What is the difference between pass by reference and pass by value in C?

Pass by reference in C means passing the address of a variable to a function, allowing the function to directly modify the value stored at that address. Pass by value means passing a copy of the variable's value to the function, leaving the original value unchanged.

2. How do I pass parameters by reference in C?

To pass parameters by reference in C, you need to use pointers. Declare a pointer variable in the function's parameter list and pass the address of the variable you want to modify when calling the function. Inside the function, you can use the pointer to directly modify the value at that address.

3. What are the advantages of using pass by reference in C?

Passing parameters by reference in C can be more efficient than passing by value, as it avoids creating a copy of the variable. It also allows for the modification of the original variable, which can be useful in functions that need to return multiple values.

4. Can I use both pass by reference and pass by value in the same C program?

Yes, you can use both pass by reference and pass by value in the same C program. However, it is important to remember that pass by reference requires the use of pointers, so you must be careful when using both methods to avoid any confusion or errors.

5. Are there any disadvantages of using pass by reference in C?

One potential disadvantage of using pass by reference in C is that it can make code more complex and difficult to understand, especially for beginners. It also requires careful handling of pointers to avoid any unintended changes to the original variable.

Similar threads

Replies
3
Views
860
Replies
3
Views
1K
Replies
1
Views
9K
Replies
3
Views
931
Replies
4
Views
1K
Replies
8
Views
1K
Replies
4
Views
2K
Back
Top