Results 1 to 4 of 4

Thread: [SOLVED] [C++] - warning: left-hand operand of comma has no effect

  1. #1
    Join Date
    Sep 2007
    Beans
    40
    Distro
    Ubuntu 7.04 Feisty Fawn

    [SOLVED] [C++] - warning: left-hand operand of comma has no effect

    I've written the following function to calculate the greatest common divisor using recursion(yes, the cliche textbook example, haha):

    Code:
    int gcd_recursive(int m, int n)
    {
    	if (n == 0)   //Base case of recursion
    	{
    		return m;
    	}
    	else         //Keep calling the function
    	{
    		return(n, gcd_recursive(n, m % n));
    	}
    }
    When I build the program that uses this function I get a warning saying "warning: left-hand operand of comma has no effect" for this line of code:

    Code:
    return(n, gcd_recursive(n, m % n));
    What does this warning mean? The program functions correctly as far as I can tell. I've tried changing my parentheses around but it still comes up.

    Thanks.

  2. #2
    Join Date
    Aug 2007
    Location
    Canada
    Beans
    420
    Distro
    Ubuntu 11.10 Oneiric Ocelot

    Re: [C++] - warning: left-hand operand of comma has no effect

    It is very clear... look your function is declared as

    PHP Code:
    int gcd_recursive(int mint n)
    {
        if (
    == 0)   //Base case of recursion
        
    {
            return 
    m;
        }
        else         
    //Keep calling the function
        
    {
            return(
    ngcd_recursive(nn));
        }

    as you can see it can return only a int value, here I repeat the declaration

    PHP Code:
    int gcd_recursive(int mint n
    and you can not return two as in the line

    PHP Code:
    return(ngcd_recursive(nn)); 
    There is no way to return two values in this way.

    pay attention that return can only return the type of data defined when the function is declare, for instance the following function:

    PHP Code:
    type function(foo){
       
    type r;
       return 
    r;


    can only return a data of type type.
    Last edited by gnusci; July 6th, 2008 at 10:22 PM.
    Best, Gnusci

    "Never make a calculation until you know the answer." -- Wheeler, Spacetime Physics, pg 60.

  3. #3
    Join Date
    Sep 2007
    Beans
    40
    Distro
    Ubuntu 7.04 Feisty Fawn

    Re: [C++] - warning: left-hand operand of comma has no effect

    Silly me.

    Thanks.

  4. #4
    Join Date
    May 2011
    Beans
    1

    Re: [SOLVED] [C++] - warning: left-hand operand of comma has no effect

    "Never make a calculation until you know the answer." -- Wheeler, Spacetime Physics, pg 60.

    Is this truth ?

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •