swap_floats function

Im supposed to create a function that swaps the floats...
the outcome of this code gives me: 2,4... i want it to give me 4,2...
how would i do that?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <math.h>
using namespace std;

void swap_floats (float nb1, float nb2 );

void main ()
{
	float nbb1= 2;
	float nbb2= 4;

	swap_floats (nbb1, nbb2);
	cout << nbb1 << " " << nbb2; 

	system ( " Pause " );
}

void swap_floats ( float nb1, float nb2 )
{
	int x;
	x = nb1;
	nb1 = nb2;
	nb2 = x;



}
Your main function must return an int value, so swap it from void to int.

To swap values in a void returning function you must pass the values by reference.
Your logic in the function is right otherwise.
Look at this tutorial it should answer your question.
http://www.cplusplus.com/doc/tutorial/functions/
that wasnt the problem. I figured it out.... in the function i had to set " float x = nb1; " instead of int x; and then saying x=nb1.

Thanks anyways
kemotoe is right; your call on line 12 is a fancy no-op. It does nothing as is, and will still do nothing if you make x a float.
this worked fine for me
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <math.h>
using namespace std;

void swap_floats (float & a, float & b);
void main ()
{
	float nb_1;
	float nb_2;

 cin >> nb_1;
 cin >> nb_2;

 swap_floats ( nb_1, nb_2 );
 cout << nb_1 << " " << nb_2;
		
 system ( " pause " );
}
void swap_floats (float & a, float & b)
{
 float temp = a;
 a = b;
 b = temp;
}
You made the changes kemotoe suggested, including the reference parameters. That's why. When you said "that wasn't the problem", it sounded like you weren't going to do that.
Topic archived. No new replies allowed.