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

  • Thread starter Thread starter flemmyd
  • Start date Start date
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
2 replies · 2K views
flemmyd
Messages
141
Reaction score
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
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.
 
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).