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

  • Thread starter Thread starter flemmyd
  • Start date Start date
Click For Summary
SUMMARY

The forum discussion centers on the issue of why a swap function in C does not modify the original values of numerator and denominator. The user attempts to swap these values using a function that takes them as parameters, but the changes do not persist outside the function due to C's pass-by-value behavior. The discussion highlights that the parameters in the swap function are distinct from the global variables, leading to confusion. The solution involves recognizing that C does not support pass-by-reference directly and that pointers or arrays are necessary for achieving this functionality.

PREREQUISITES
  • Understanding of C programming syntax and structure
  • Knowledge of function parameters and scope in C
  • Familiarity with pass-by-value versus pass-by-reference concepts
  • Basic understanding of pointers in C (even if not allowed in this context)
NEXT STEPS
  • Research how to use pointers in C for modifying values in functions
  • Learn about using structures in C to return multiple values from a function
  • Explore the concept of variable scope and lifetime in C programming
  • Investigate alternative methods to swap values without pointers, such as using global variables
USEFUL FOR

C programmers, computer science students, and anyone looking to understand function behavior and variable scope in C programming.

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).
 

Similar threads

  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
11K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
Replies
9
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K