Thread: how do you make a function return a value?

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    84

    how do you make a function return a value?

    I'm trying create a function, but when it returns my value instead of a number I get a combination of letters and numbers.

    I input: 0 and 4

    I should get something like: 3.313

    I get: 1.#QNAN0

    Here's my code, do I need to do something different with my return command?

    thank you

    Code:
    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
    
    
    float secant ( float x_lower,float x_upper);
    
    
    main()
           {
                float root, lower_bound, upper_bound ;       
                
                       
                
                printf("\n\nInput lower bound:  ");
                
                scanf("%d", &lower_bound); 
                
                printf("\n\nInput upper bound:  ");
                
                scanf("%d", &upper_bound); 
                
                root = secant ( lower_bound, upper_bound );
                    
                  printf("\n\n answer is:  %f ",root);
                  
                  getchar();
                  getchar();
                    }
                    
                    
    float secant ( float x_lower,float x_upper) {
          
          int i ;
          float xo, xi ;
          
          for ( i = 1 ; i < 4 ; i++ ) {
              
              
       xo = exp ( -pow (x_lower , 2) ) - cos ( x_lower ) - 1 ;
       
       xi = exp ( -pow (x_upper , 2) ) - cos ( x_upper ) - 1 ;
    
    
     x_upper = xi - ( xi/(xo-xi) ) * ( x_lower - x_upper ) ;
     
    }
    
    return x_upper ;
    
    }

  2. #2
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    You might want to make a different variable in your secant function and assign your return values to it...

    Code:
    float secant(float x_lower, float x_upper)
    {
    
            int i;
            float xo, xi;
            float some_var;
    
            for (i = 1; i < 4; i++) {
    
    
                    xo = exp(-pow(x_lower, 2)) - cos(x_lower) - 1;
    
                    xi = exp(-pow(x_upper, 2)) - cos(x_upper) - 1;
    
    
                    some_var = xi - (xi / (xo - xi)) * (x_lower - x_upper);
    
            }
    
            return some_var;
    }
    Also, this nitpick:

    Code:
    scanf("%d", &upper_bound);
    Use %f for float input

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    84
    Thanks for the tip, I'm still getting letters for my output instead of a number? Do you know why?

  4. #4
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    Your scanf calls are using %d when they should be using %f. With many inputs, this causes the floating point approximation to be filled with bits that represent an "infinite" or "not a number" value. With other inputs, this gives the floating point approximation a completely wrong value.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Also, this nitpick:
    It's not a nitpick, it's fundamentally broken unless it's fixed.

    Code:
    gcc -W -Wall -ansi -pedantic -O2 foo.c -lm
    foo.c:10: warning: return type defaults to ‘int’
    foo.c: In function ‘main’:
    foo.c:17: warning: format ‘%d’ expects type ‘int *’, but argument 2 has type ‘float *’
    foo.c:21: warning: format ‘%d’ expects type ‘int *’, but argument 2 has type ‘float *’
    foo.c:29: warning: control reaches end of non-void function
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. New string functions
    By Elysia in forum C Programming
    Replies: 11
    Last Post: 03-28-2009, 05:03 AM
  2. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  3. Another weird error
    By rwmarsh in forum Game Programming
    Replies: 4
    Last Post: 09-24-2006, 10:00 PM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  5. C++ FTP class won't work
    By lord mazdak in forum C++ Programming
    Replies: 8
    Last Post: 12-18-2005, 07:57 AM