func definition error

This is a discussion on func definition error within the C++ Programming forums, part of the General Programming Boards category; Can someone point out my error in the following function definition: Code: float Test2 (int i, float x) { i=i+7; ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    12

    func definition error

    Can someone point out my error in the following function definition:

    Code:
    float Test2 (int i,
                 float x)
    
    {
         i=i+7;
         x=4.8+float(i);
    }
    Thanks!

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Well, in your declaration you have

    float Test2( int i, float x )

    So that is a function which takes two parameters ( one integer and the second a floating point number ). Also you claim that this function will return to the caller a float. In your definition you change i and x which are temporary variables that will not get changed in your calling scope and you do not return anything. So either do one of the following things. Pass each parameter as a reference so you can modify it directly AND return void. so..
    Code:
    void Test2 ( int &i,  float &x )
    {
         i = i + 7;
       
         x = 4.8f + float(i);
    }
    Or I guess you could add up i and x or whatever you are trying to do and store that in a variable and then use:

    return ( fReturn );

    in your original code. Hope this helps.

    P.S. - Also you may want to use C++ sytle casting instead of C style.

    static_cast<float>(i);

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    4.8 is a double, since the result of an expression is the type of the longest floatiest operand, you are actually assigning a double value to a float variable. This should cause a warning. The error is that the function declares a return type of float yet returns nothing.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  2. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  3. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  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. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21