C compiler passing a variable

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

C compiler passing a variable

Jump to solution
3,053 Views
DanielMorley
Contributor I
I am using a C compiler to program a MC9S08GT16A
 
I have the following function
 void ReadPosition( int pos){
        
     if ( POSSW1==DOWN&&POSSW2==DOWN&&POSSW3==DOWN&&POSSW4==DOWN)
       pos=0;
    
    if ( POSSW1==DOWN&&POSSW2==DOWN&&POSSW3==DOWN&&POSSW4==UP)
     pos=1;
   
    if ( POSSW1==DOWN&&POSSW2==DOWN&&POSSW3==UP&&POSSW4==DOWN)
      pos=2;
 
    if ( POSSW1==DOWN&&POSSW2==DOWN&&POSSW3==UP&&POSSW4==UP)
     pos=3; 
        
    if ( POSSW1==DOWN&&POSSW2==UP&&POSSW3==DOWN&&POSSW4==DOWN)
     pos=4; 
        
    if ( POSSW1==DOWN&&POSSW2==UP&&POSSW3==DOWN&&POSSW4==UP)
     pos=5; 
        
    if ( POSSW1==DOWN&&POSSW2==UP&&POSSW3==UP&&POSSW4==DOWN)
     pos=6; 
        
    if ( POSSW1==DOWN&&POSSW2==UP&&POSSW3==UP&&POSSW4==UP)
     pos=7; 
        
    if ( POSSW1==UP&&POSSW2==DOWN&&POSSW3==DOWN&&POSSW4==DOWN)
     pos=8; 
        
    if ( POSSW1==UP&&POSSW2==DOWN&&POSSW3==DOWN&&POSSW4==UP)
     pos=9; 
        
    if ( POSSW1==UP&&POSSW2==DOWN&&POSSW3==UP&&POSSW4==DOWN)
     pos=10; 
        
    if ( POSSW1==UP&&POSSW2==DOWN&&POSSW3==UP&&POSSW4==UP)
     pos=11; 
     
        
    if ( POSSW1==UP&&POSSW2==UP&&POSSW3==DOWN&&POSSW4==DOWN)
     pos=12; 
        
    if ( POSSW1==UP&&POSSW2==UP&&POSSW3==DOWN&&POSSW4==UP)
     pos=13 ;       
    if ( POSSW1==UP&&POSSW2==UP&&POSSW3==UP&&POSSW4==DOWN)
     pos=14; 
     
        
    if ( POSSW1==UP&&POSSW2==UP&&POSSW3==UP&&POSSW4==UP)
     pos=15;
    
       
       
 }
 
 
Which works fine.  However it does not allow me to pass the pos value back to the main program. 
int x;
 ReadPosition(x);
   if (x==0){
   Forward(); 
   Delay(3);
   Brake();
    }
 
 
 
How should I set this up to pass the variable back to the main program
 
 
Labels (1)
Tags (1)
0 Kudos
Reply
1 Solution
1,307 Views
bigmac
Specialist III
Hello Daniel,
 


Daniel Morley wrote:
 
void ReadPosition(int pos)
{
   if (POSSW1==DOWN&&POSSW2==DOWN&&POSSW3==DOWN&&POSSW4==DOWN)
      pos=0;
    
   if (POSSW1==DOWN&&POSSW2==DOWN&&POSSW3==DOWN&&POSSW4==UP)
     pos=1;
   // etc.
   
}
 
Which works fine.  However it does not allow me to pass the pos value back to the main program. 
 
int x;
 
ReadPosition(x);
if (x==0) {
   Forward(); 
  Delay(3);
  Brake();
}
 
How should I set this up to pass the variable back to the main program
 


In this case, one method to pass the variable value is to setup pos as a pointer to a variable, and to directly update the variable value from within the function.
 
void ReadPosition(int *pos)
{
   if (POSSW1==DOWN&&POSSW2==DOWN&&POSSW3==DOWN&&POSSW4==DOWN)
      *pos=0;
    
   if (POSSW1==DOWN&&POSSW2==DOWN&&POSSW3==DOWN&&POSSW4==UP)
     *pos=1;
// etc.
}
 
 
 
int x;
 
ReadPosition(&x);
if (x==0) {
   Forward(); 
  Delay(3);
  Brake();
}
.
Is this what you are trying to achieve?  Alternatively, you could setup the function with a returned int value, i.e.  int ReadPosition(void);  In this case, pos would be a local int variable within the function.
.
Regards,
Mac

View solution in original post

0 Kudos
Reply
5 Replies
1,308 Views
bigmac
Specialist III
Hello Daniel,
 


Daniel Morley wrote:
 
void ReadPosition(int pos)
{
   if (POSSW1==DOWN&&POSSW2==DOWN&&POSSW3==DOWN&&POSSW4==DOWN)
      pos=0;
    
   if (POSSW1==DOWN&&POSSW2==DOWN&&POSSW3==DOWN&&POSSW4==UP)
     pos=1;
   // etc.
   
}
 
Which works fine.  However it does not allow me to pass the pos value back to the main program. 
 
int x;
 
ReadPosition(x);
if (x==0) {
   Forward(); 
  Delay(3);
  Brake();
}
 
How should I set this up to pass the variable back to the main program
 


In this case, one method to pass the variable value is to setup pos as a pointer to a variable, and to directly update the variable value from within the function.
 
void ReadPosition(int *pos)
{
   if (POSSW1==DOWN&&POSSW2==DOWN&&POSSW3==DOWN&&POSSW4==DOWN)
      *pos=0;
    
   if (POSSW1==DOWN&&POSSW2==DOWN&&POSSW3==DOWN&&POSSW4==UP)
     *pos=1;
// etc.
}
 
 
 
int x;
 
ReadPosition(&x);
if (x==0) {
   Forward(); 
  Delay(3);
  Brake();
}
.
Is this what you are trying to achieve?  Alternatively, you could setup the function with a returned int value, i.e.  int ReadPosition(void);  In this case, pos would be a local int variable within the function.
.
Regards,
Mac

0 Kudos
Reply
1,307 Views
DanielMorley
Contributor I
It is working now with the above recommendations being followed.  Thanks
0 Kudos
Reply
1,307 Views
rocco
Senior Contributor II

It is working now with the above recommendations being followed.
You read that book already ! ? :smileyhappy:
0 Kudos
Reply
1,307 Views
DanielMorley
Contributor I
in process
0 Kudos
Reply
1,307 Views
flashtoo
Contributor I
1) Get a copy of "The C programming Language" by Brian Kernighan and Dennis Ritchie and read it.
 
2) Create the funciton with a return value not void:
 
int ReadPosition(int pos) {
 // work on pos value and create return value here
  pos += 5;
 return (pos);   // return any variable or constant
}
 
and call as follows:
 
int x;
 x = ReadPosition(x);
 if (x==0){
   Forward(); 
   Delay(3);
   Brake();
 }
 
Or you could pass "X" as a pointer reference
 
void ReadPosition(int *pos) {
 // work on pos value and create return value here
 *pos += 5
 return;
}
 
and call as follows:
 
int x;
 ReadPosition(&x);
 if (x==0){
   Forward(); 
   Delay(3);
   Brake();
 }
 
Bill
0 Kudos
Reply