Thread: How this c++ code work

  1. #1
    Deleted Account
    Join Date
    Oct 2013
    Posts
    39

    Question How this c++ code work

    First of all merry xmas guys....
    i have this c++ code but i cannot understand why this output is coming plese help......

    Code:
    #include<iostream>
    using namespace std;
    
    
    int func(int&x,int y=10)
    {
        if(x%y==0)
            return ++x;
        else
            return y--;
    }
    
    
    
    
    int main()
    {
        int p=20,q=23;
        q=func(p,q);
        cout<<p<<" "<<q<<"\n";        //here the value of q would have become 22 but its still 23 why...
        p=func(q);
            cout<<p<<" "<<q<<"\n";
    q=func(p);
        cout<<p<<" "<<q<<"\n";
    return 0;
    
    
    }
    MY OUTPUTS ARE

    20 23
    10 23
    11 11
    Last edited by Username changed; 12-25-2013 at 03:28 AM. Reason: providing output

  2. #2
    Registered User
    Join Date
    Jun 2013
    Posts
    56
    Code:
    return y--;
    This line is why. Can you figure out why it is?

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by Ewiv View Post
    Code:
    return y--;
    This line is why. Can you figure out why it is?
    I was thinking this line is why?

    Code:
    int func(int&x,int y=10)
    Edit: But, I think you are correct and its your line posted.
    After re-reading the code, I saw the issues you hinted at.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  4. #4
    Registered User
    Join Date
    Jun 2013
    Posts
    56
    Can't say for sure if the int func line is not intended to be that way, so I chose to ignore it. The return line however is obviously wrong.

  5. #5
    Registered User
    Join Date
    Mar 2012
    Posts
    110
    It looks like a school example. I bet your teacher would be thrilled if you'd ask. (I used to work as a teacher)

  6. #6
    Registered User cstryx's Avatar
    Join Date
    Jan 2013
    Location
    Canada
    Posts
    123
    This question's answer is as trivial as understanding (?)-- vs --(?). Combined with the fact that it's being passed by value:
    Code:
    int y=10
    If it worked as you expected, you would've had:
    Code:
    return y-1;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Cant seem to get this code to work.....
    By Alex Coven in forum C Programming
    Replies: 2
    Last Post: 09-10-2013, 06:57 PM
  2. How do I work out the AWT and TAT in my code ?
    By spendotw in forum C Programming
    Replies: 0
    Last Post: 01-21-2012, 07:51 AM
  3. Why does this code work?
    By Whitewings in forum C Programming
    Replies: 8
    Last Post: 08-16-2011, 12:58 PM
  4. this code won't work
    By med linux in forum C Programming
    Replies: 7
    Last Post: 03-23-2011, 08:35 AM
  5. cant get code to work
    By duffy in forum C Programming
    Replies: 13
    Last Post: 10-20-2002, 05:23 AM