can any one tell why is output is bit stange

This is a discussion on can any one tell why is output is bit stange within the C++ Programming forums, part of the General Programming Boards category; Code: #include<iostream> using namespace std; bool testsame(int val1, int val2) { if(val1==val2) return true; else false; } int main() { ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    181

    Question can any one tell why is output is bit stange

    Code:
    #include<iostream>
    
    using namespace std;
    
    bool testsame(int val1, int val2)
    {
        if(val1==val2)
        return true;
        else
        false;
    }
    
    
    int main()
    {
        if(testsame(12, 23))
        cout<<"Both values  are same"<<endl;
        else
        cout<<"Values are not same"<<endl;
        
        cin.get();
    }
    and my output is
    Code:
    Both values are same
    but the output is wrong. i know the problem where it is. it in the function testsame
    Code:
    return true;
        else
        false;
    }
    when i put return in front of false everything seems tom work fine. i get correct answer. i dont understand why it is?? can any one tell me why this happens if i dont put return keyword keyword

    thank you

    s.s.harish

  2. #2
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    return false;
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    36
    Since the function is supposed to return a boolean value, whatever you want to return needs to have "return" in front of it. So just like The Brain said, and as you already realized, you need the "return" keyword in front of "false" if you want to return the value as false if the two numbers aren't equal to each other.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    If you don't put the "return" there, then "false;" is just a statement that does nothing. At that point your function returns without having a value to return. You probably got a warning about reaching the end of a non-void function without returning a value or something like that. When you don't specify a return value, the compiler could return anything it wants, normally whatever garbage is in the memory/register at the time. Since false is 0 and true is anything non-zero, chances are that a non-zero value is what is returned, and so the return value is equivalent to true. On another compiler or another machine the output might end up being correct. The point is that the behavior is undefined, and so you shouldn't rely on code like that working or not working, you should just fix the code as you did by adding the return.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    181
    thax david thats i what i wanted to know. i knew it was rongh, but i wanted to know the reason why.

    thax very much

    s.s.harish

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2d game
    By JordanCason in forum Game Programming
    Replies: 5
    Last Post: 12-08-2007, 09:08 PM
  2. Replies: 7
    Last Post: 12-10-2004, 07:18 AM
  3. bit patterns of negtive numbers?
    By chunlee in forum C Programming
    Replies: 4
    Last Post: 11-08-2004, 07:20 AM
  4. DOS, Serial, and Touch Screen
    By jon_nc17 in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 01-08-2003, 03:59 PM

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