Use the program, Passing-by-value, on the bottom of pp. 248–249 and the program,
Passing-by-reference, on pp. 255–256 as a starting point for this assignment. Write a similar program, but change the code to pass two variables to the function call rather than one.

that's my assignment and i have tried to come up with something but i am totally lost.. here is what i have so far... I think im supose to use a const im not sure though...

#include <iostream>

using std::cout;
using std::endl;


int accelerate(int* speed); 
int gear(int* gear); 

int main (void)
{
int speed = 20; 
int gear = 3;

cout << endl 
     << "Current speed: " << speed << endl
     << "Current gear: " << gear << endl;
      


int* pspeed = &speed;
int* pgear = &gear; 

cout << endl
     << "Memory address passed for speed variable: " << pspeed << endl
     << "Memory address passed for gear: " << pgear << endl;  

int accelResult = accelerate (pspeed); 
int changedGear = gear (pgear); 


cout << endl
	 << "Changed gears to:  " << changedGear << endl
	 << "Speed accelerated: " << accelResult << endl;

system("PAUSE"); 

return 0; 
}

int accelerate(int* speed)
{
	*speed += 25;
	return *speed; 
}

int gear(int* gear)
{
   *gear += 1;
   return *gear;

}

Thank you for your time.

Based on what I see, you won't want a const. Are you familiar with the terms "arguments" and "parameters"?

Based on what I see, you won't want a const. Are you familiar with the terms "arguments" and "parameters"?

yes but im learning so im probably forgetting...


i think i got it with pointers.

int main (void)
{
	int ispeed = 20; 
	int igear = 3;

	cout << endl 
		 << "Current speed: " << ispeed << endl
		 << "Current gear: " << igear << endl;
	      


	int* pspeed = &ispeed;
	int* pgear = &igear; 

	cout << endl
		 << "Memory address passed for speed variable: " << pspeed << endl
		 << "Memory address passed for gear: " << pgear << endl;  

	acc_gear (pspeed, pgear); 

	cout << endl
		 << "Changed gears to:  " << igear << endl
		 << "Speed accelerated: " << ispeed << endl;

	system("PAUSE"); 

	return 0; 
}

void acc_gear(int* speed, int* gear)
{
	*speed += 25;
	*gear += 1;
}

That's one way to do it. Another way is

acc_gear (&ispeed, &igear);
cout << endl
	<< "Changed gears to:  " << igear << endl
	<< "Speed accelerated: " << ispeed << endl;

That way, you don't need to create and initialize the pointers. You simply pass the reference directly. Essentially the same net effect, just fewer steps

That's one way to do it. Another way is

acc_gear (&ispeed, &igear);
cout << endl
	<< "Changed gears to:  " << igear << endl
	<< "Speed accelerated: " << ispeed << endl;

That way, you don't need to create and initialize the pointers. You simply pass the reference directly. Essentially the same net effect, just fewer steps

yes that's perfect, that's what i was hoping i could do thank you!

yes that's perfect, that's what i was hoping i could do thank you!

im getting:

1>.\passing_by_value.cpp(37) : error C3861: 'acc_gear': identifier not found

i have no idea ...

Did you create the prototype for it above your main()? I don't see a prototype in your second post (post #3).

Edited 6 Years Ago by Fbody: n/a

Did you create the prototype for it above your main()? I don't see a prototype in your second post (post #3).

#include <iostream>

using std::cout;
using std::endl;

int accelerate(int* speed); 
int gear(int* gear); 

int main (void)
{
	int ispeed = 20; 
	int igear = 3;

	cout << endl 
		 << "Current speed: " << ispeed << endl
		 << "Current gear: " << igear << endl;
	      


	int* pspeed = &ispeed;
	int* pgear = &igear; 

	cout << endl
		 << "Memory address passed for speed variable: " << pspeed << endl
		 << "Memory address passed for gear: " << pgear << endl;  

	 acc_gear (&ispeed,&igear);

	cout << endl
		 << "Changed gears to:  " << igear << endl
		 << "Speed accelerated: " << ispeed << endl;

	system("PAUSE"); 

	return 0; 
}

void acc_gear(int* speed, int* gear)
{
	*speed += 25;
	*gear += 1;
}

In other words, you don't have a prototype. Remove lines 6 and 7 and replace them with the proper prototype/declaration for your function "acc_gear".

In other words, you don't have a prototype. Remove lines 6 and 7 and replace them with the proper prototype/declaration for your function "acc_gear".

AHHH YES DUH!

here i got it thank you for helping me learn

void acc_gear (int* speed, int* gear);

This question has already been answered. Start a new discussion instead.