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

  • Thread starter Thread starter flemmyd
  • Start date Start date
AI Thread Summary
The discussion centers on a C programming issue where a swap function does not modify the original values of numerator and denominator. The user realizes that the parameters in the swap function are local copies, leading to confusion about variable scope and passing by value versus reference. They seek a solution without using pointers or arrays, as these are restricted for their assignment. Key insights reveal that the function's parameters are shadowing the global variables, preventing any changes from being reflected in the main function. Ultimately, the user is encouraged to understand the distinction between variable scopes and the nature of parameter passing in C.
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).
 
Thread 'Have I solved this structural engineering equation correctly?'
Hi all, I have a structural engineering book from 1979. I am trying to follow it as best as I can. I have come to a formula that calculates the rotations in radians at the rigid joint that requires an iterative procedure. This equation comes in the form of: $$ x_i = \frac {Q_ih_i + Q_{i+1}h_{i+1}}{4K} + \frac {C}{K}x_{i-1} + \frac {C}{K}x_{i+1} $$ Where: ## Q ## is the horizontal storey shear ## h ## is the storey height ## K = (6G_i + C_i + C_{i+1}) ## ## G = \frac {I_g}{h} ## ## C...

Similar threads

Replies
3
Views
1K
Replies
3
Views
1K
Replies
1
Views
10K
Replies
3
Views
1K
Replies
4
Views
1K
Replies
8
Views
1K
Replies
4
Views
2K
Back
Top