View Single Post
  #3   Spotlight this post!  
Unread 15-02-2006, 05:04
Unsung FIRST Hero
Mike Betts Mike Betts is offline
Electrical Engineer
no team
Team Role: Engineer
 
Join Date: Dec 2001
Rookie Year: 1995
Location: Homosassa, FL
Posts: 1,442
Mike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond repute
Re: Motor drive function?

Quote:
Originally Posted by Keith Watson
The original code just modifies a local copy of the argument passed in. You need to "pass by reference". Typical usage is:
Code:
void Drive(int& motor, unsigned char speed)
{
	// modify inputted speed
	motor = speed;
}
 
Drive(pwm01, 100);
Mark's example works but is less common in usage. It takes the address of the variable, passes it in as a pointer, then dereferences the pointer.
Keith,

I think that you are mixing C++ with C. in C++, "int&" is a pass by reference. In C there is no pass by reference only pass by value and a pointer is the only way to do it.

Mike



To All,

Mark's post is correct. Please read K&R chapter 5 before sliding down the perilous and provocative path of pointers.

Mike
__________________
Mike Betts

Alumnus, Team 3518, Panthrobots, 2011
Alumnus, Team 177, Bobcat Robotics, 1995 - 2010
LRI, Connecticut Regional, 2007-2010
LRI, WPI Regional, 2009 - 2010
RI, South Florida Regional, 2012 - 2013

As easy as 355/113...