#swapping variables
32 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For more information use !howto ask.
It is giving errors
what is the expected behaviour? your function does not modify any variables passed to it by the caller
Can you explain?
main.cpp:6:16: error: expected ‘)’ before ‘;’ token 6 | void swap(int x; int y) { | ~ ^ | ) main.cpp:6:23: error: expected initializer before ‘)’ token 6 | void swap(int x; int y) { | ^ ```
The error
;
you aren’t supposed to have that semicolon there
in function declarations parameters are comma-seperated
people usually take a while till they start using functions
so shouldn't be first time
your function only modifies its own local copies of its arguments, not the versions you see printing out
How can i change the variables in main function
well you have to pass the variable by pointer or reference
do you know what those are
Yeah
void swap(int *x, int *y){
int temp;
x = temp;
x = y;
y = temp;
}```
Something like that?
Ahh , pointers are hard
int* temp
!solved
Thank you and let us know if you have any more questions!
[SOLVED] swapping variables
now try to do it without the temp
int temp = x*;
x* = y*;
y* = temp;```
Without the temp?