Thread: need help returning float values from subprograms

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    4

    need help returning float values from subprograms

    it seems i can only return an integer value from a subprogram? i need a float value returned is there something i'm missing here? i wrote this quick block of code to show you what i mean. say i type in 6.67 in the get_z function it then returns to the main function and assigns it to z. when i print z in the main function it prints as 6.0000 etc

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
      float z;
      
          z=get_z();
          printf("z in main fuction = %f\n", z);
          
          system("PAUSE");
          return 0;
    }
    
    
    int get_z()
    {
      float z;
          
          printf("please enter # for z: ");
          scanf("%f", &z);
          
          return z;
    }

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    define get_z() to return a float
    Code:
    float get_z();
    Kurt

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    4
    now it just returns a value of 1.000 no matter what number i enter... any idea why?

  4. #4
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Post your code.
    Kurt

  5. #5
    Registered User
    Join Date
    Nov 2007
    Posts
    4
    its the same thing but just with the float get_z as opposed to the int get_z... i'm really confused as to why its not working

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
      float z;
      
          z=get_z();
          printf("z in main fuction = %f\n", z);
          
          system("PAUSE");
          return 0;
    }
    
    
    float get_z()
    {
      float z;
          
          printf("please enter # for z: ");
          scanf("%f", &z);
          
          return z;
    }

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    what compiler are you using? you need to turn up your warning levels.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    float get_z();
    
    int main()
    {
      float z;
      
          z=get_z();
          printf("z in main fuction = %f\n", z);
          
          system("sleep 2");
          return 0;
    }
    
    
    float get_z()
    {
      float z;
          
          printf("please enter # for z: ");
          scanf("%f", &z);
          
          return z;
    }

  7. #7
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Put
    Code:
    float get_z();
    before main.
    As your code is right now it should not even compile. ( redifinition of function with different returntype )
    Kurt

  8. #8
    Registered User
    Join Date
    Nov 2007
    Posts
    4
    Quote Originally Posted by ZuK View Post
    Put
    Code:
    float get_z();
    before main.
    As your code is right now it should not even compile. ( redifinition of function with different returntype )
    Kurt
    thanks man, i tried to declare the fuctions before hand but forgot the ";" after it so i got a ton of errors and just got rid of it. anyway it works now. thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-13-2009, 03:25 PM
  2. Promblem with code
    By watchdogger in forum C Programming
    Replies: 18
    Last Post: 01-31-2009, 06:36 PM
  3. Class won't call
    By Aalmaron in forum C++ Programming
    Replies: 3
    Last Post: 04-13-2006, 04:57 PM
  4. File input question
    By Beast() in forum C Programming
    Replies: 16
    Last Post: 07-09-2004, 03:23 PM
  5. Why does it work in Visual Studio and Not Borland
    By MonteMan in forum C++ Programming
    Replies: 14
    Last Post: 10-20-2002, 09:36 PM