Thread: problem with passing value of a function

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    34

    problem with passing value of a function

    Hello

    I'm having a problem with this code I'm stumped as to why the function Get_Distance() is returning the wrong value back to the caller. When I run a test in my IDE I watch the values of each variable at breakpoints. When executed distance is 1391.261 which is correct. When the value is returned back to the caller it turns into -6057.000 and assigns -6057.000 to distance2pet and that is what is displayed on my LCD. If I make distance2Pet a global and just make everything 1 big source file then it works fine, but I have a lot more code to do and don't want 5,000 lines of code in 1 file. Thank you for any input!

    File 1 mainDemo.C
    Code:
    #include <p24FJ128GA010.h>
    #include <stdio.h>
    
    
    
    _CONFIG1(JTAGEN_OFF & GCP_OFF & GWRP_OFF & BKBUG_OFF & ICS_PGx2 & FWDTEN_OFF)
    _CONFIG2(IESO_OFF & FCKSM_CSDCMD & OSCIOFNC_ON &  FNOSC_FRC & POSCMOD_NONE)
    
    
    char  distance2PetString[15];
    
    main ()
    {
         float distance2Pet;  //distance from pet to base
    	
         LCDInit(); //initialize LCD
    
         LCDClear(); //clear the LCD
    	
         distance2Pet =  Get_Distance(41.0344, 85.1662, 41.0444, 85.1762);
    
         sprintf(distance2PetString, "%.3f", distance2Pet);
    	
         while(1) {
    			
             displayPetDistance();
    
         } //end while
    
    } //end main
    
    displayPetDistance()
    {
         int d;
    
         LCDL1Home();
    
         for(d = 0; d<15; d++){
    
         LCDPut(distance2PetString[d]);
         }
    }
    File 2
    gps_read.c
    Code:
    #include <p24fj128ga010.h>
    #include <math.h>
    #include "gps_read.h"
    
    #define EarthRadius	        (6378100)      // meters
    #define Degrees_2_Radians	(0.017453293)  // pi/180
    #define Radians_2_Degrees	(57.29577951)  // 180/pi
    
     float Get_Distance( float lat1,  float long1,  float lat2,  float long2)
     {
    	
    	float dLat, dLong, sideA, sideC;
    	
    	lat1  *= Degrees_2_Radians;  // 0.716185  value of variable at this point
    	long1 *= Degrees_2_Radians;   // 1.48643
    	lat2  *= Degrees_2_Radians;   // 0.71636
    	long2 *= Degrees_2_Radians; // 1.48661
    	
    	dLat  = lat2  - lat1;   // 0.000175
    	dLong = long2 - long1;  // 0.00018
    	
    	sideA = (sinf(dLat/2)) * (sinf(dLat/2)); // 7.65625e-9
    
    	sideA = sideA + ((cosf(lat1)) * (cosf(lat2)) * (sinf(dLong/2)) * (sinf(dLong/2))); 
    
    	sideC = 2 * atan2f( sqrt(sideA), sqrt(1-sideA) ); 
    
    	distance = (EarthRadius*sideC); // meters 1391.261
    	
    	return distance;
    }
    header file gps_read.h
    Code:
    float distance;

  2. #2
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    404
    Are you including the header file that has the prototype for your Get_Distance function, or does p24FJ128GA010.h take care of this?

    I see nothing wrong with your code. I even tested what you were doing on a small scale and had no issues.

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    34
    no I didn't include the gps_read.h in the main source I'll try that. I have made distance a local variable in gps_read.c and then tried it as a global in gps_read.c and deleted the header file and still got the same results.

    The p24fj128ga010.h configures all the registers and things inside the microcontroller.

    What really stumps me is that I can make distance an extern variable type and the program will work with 2 source files, or I can put all the code in 1 file and make it work correctly.

  4. #4
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    404
    Oh yes, if you have a variable in one C file that you want to be public (used in other C files), you need to declare it with extern.

    Example:

    main.c
    Code:
    #include <stdio.h>
    #include "other.h"
    
    int mynum;
    
    int main (void)
    {
        printf("%d\n", mynum);
        return 0;
    }
    other.h
    Code:
    //Function Prototypes from other.c
    other.c
    Code:
    #include "other.h"
    
    extern int mynum = 5;
    
    //Some functions here
    Last edited by carrotcake1029; 02-10-2009 at 09:26 PM.

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    34
    I tried including gps_read.h in the main but no go. I'm trying to stay away from extern if i can but I figured I would give it a try just to do some troubleshooting. Maybe it's a compiler issue, I've just never had a value "change" when it is returned to the calling function.

  6. #6
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    404
    As many suggest, staying away from extern is ideal. Instead of using a public variable, create a pointer and access it's address when you need to read/write to it. This example shows how to using an int, but you can do the same with a float or double.

    Example:
    main.c
    Code:
    #include <stdio.h>
    #include "other.h"
    
    int main (void)
    {
        int mynum = 0;
    
        modifyvar(&mynum);
    
        printf("%d\n", mynum);
        return 0;
    }
    other.h
    Code:
    void modifyvar(int *myvar);
    other.c
    Code:
    #include "other.h"
    
    void modifyvar(int *myvar)
    {
        *myvar = 5;
    }

  7. #7
    Registered User
    Join Date
    Nov 2006
    Posts
    34
    I've tried every combination of pointer that I could think of and couldn't get anywhere. I have made other functions where a value is passed back to the caller and assigned to a local variable in that function, it is just frustrating why this one isn't working.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Can you create a breakpoint that breaks when the variable is changed?
    That is a common technique I use to hunt down corrupted things.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM