Returns without a "return" command

Related searches
C programming language, compiled with gcc, terminal bash in WSL

I have written a recursive function, to find the lowest number in an array, which works just fine.

/*01*/    int minimo(int array[], int n)
/*02*/    {
/*03*/      static int min = 0;
/*04*/    
/*05*/      if (n == N)
/*06*/      {
/*07*/          return array[n-1];
/*08*/      }
/*09*/      else
/*10*/      {
/*11*/          min = minimo(array, n+1);
/*12*/          if(array[n]<min){
/*13*/              min = array[n];
/*14*/          }
/*15*/      }
/*16*/    }

The only problem is that it shouldn't work, because it doesn't return "min" to the caller...

int main()
{
    //Var
    int array[N] = {10, 2, 5, 1, 7};
    printf("Min: %d\n", minimo(array, 0));
}

My concern is actually a problem, but not on my machine onto which the function works just fine as it is; it is a problem on my friends' laptops and IDEs, I tried copying to XCode on a friend's Macbook and it wouldn't work if the line "return min;" wasn't added at the end of the function.

Between line 15-16 I have to add return min;

/*15*/      }
            return min;
/*16*/    }

My questions for you are the following:

  1. How can a function return a variable automatically?
  2. Is it possible that it does return the only variable that I created (static int min)?
  3. Or is it a "problem" related to the static attribute that the variable has?
  4. Does it have anything to do with the nature of the function (recursive)?

This is my first post, please be kind if I'm breaking any forum rule.


My concern is actually a problem, but not on my machine onto which the function works just fine as it is; it is a problem on my friends' laptops and IDEs, I tried copying to XCode on a friend's Macbook and it wouldn't work if the line "return min;" wasn't added at the end of the function.

Typical example of undefined behavior. Works on one machine but not another. Works at daytime but not nighttime. Works with one compiler but not another. When you invoke undefined behavior, the C standard imposes no requirements on how the code should behave.

C11 standard 6.9.1.12

If the } that terminates a function is reached, and the value of the function call is used by the caller, the behavior is undefined.

In your code, that is precisely what happens. You invoke undefined behavior when you try to print the return value.

Contrary to what many believes, it IS completely allowed to omit the return statement in a non-void function. It only becomes undefined behavior if you try to use the non-existent return value.

To avoid this, always compile with at least -Wall -Wextra.

How to Return Nearly Anything Without a Receipt, Whether you've gotten a gift you really don't like or you're just prone to losing receipts, returning items without a receipt can sometimes be a� Returns and exchanges without a receipt may be limited. Other restrictions may apply. If you’re not satisfied with any Target Owned Brand item, return it within one year with a receipt for an exchange or a refund.


How can a function return a variable automatically?

It follows the protocol. It knows that the return of the function should find the returned value at some location and an explicit return from that function will fill that location. If you do not call return, some random value will be preserved at the given location.

Is it possible that it does return the only variable that I created (static int min)?

No, it is undefined what it returns. It can be whatever.

Or is it a "problem" related to the static attribute that the variable has?

Again, what it returns is undefined.

Does it have anything to do with the nature of the function (recursive)?

Yes and no. If the function is defined as external the returned value follows another protocol as in the case of static functions.

It can happen anything in the final code, the C language does not impose the way of implementing a function, be it recursive or not. For example, in case the function is recursive and can be precomputed, only the final value can be replaced at the place of call. This is also correct as time as the final result of the program is the expected result, conforming to the operational semantics that defines C in ISO9899.

I quote from the official document:

4 In the abstract machine, all expressions are evaluated as specified by the semantics. An actual implementation need not evaluate part of an expression if it can deduce that its value is not used and that no needed side effects are produced (including any caused by calling a function or accessing a volatile object).

It also can replace a call with the value of that call and this is correct.

So at all your questions, the answer is undefined behavior.

Returns, Some items sold by Target have a modified return policy noted on the receipt, packing slip or damaged or do not have a receipt may be denied a refund or exchange. Can I return or exchange an item without a receipt? shows more content� Store Returns. Lowe's requires a valid picture ID (state driver's license, state ID card or military ID) for returns without a valid receipt. Lowe's uses and checks verification systems. ID information or a phone number may be entered into these systems where authorized by law. All returns are subject to system approvals.


How can a function return a variable automatically?

I happens by chance, min variable happens to be in the correct return register for the ABI.

Is it possible that it does return the only variable that I created (static int min)?

I think it is more related to it having being used just before function exit, but I am guessing here: this is not a C language defined behavior, and it worked by chance.

Or is it a "problem" related to the static attribute that the variable has?

Does it have anything to do with the nature of the function (recursive)?

Since this was a chance behavior, could be or not. The difference between random and deterministic is that you have so many variables that you give up explaining.

7 Stores With the Best Return Policies, Most of the stores listed below will return or exchange an item without a receipt and give you 90 days or more to make the transaction. Returns without an original sales receipt or pack slip must be returned using a valid ID and will receive a store credit for the lowest selling price. Information from the ID will be held in a company-wide database of customer return activity.


I suppose you defined N somewhere (probably N=5 in you case).

Most compilers automatically adds "return 0" statement if return statement is missing.

* See comments for more info. Actually it is an Undefined Behavior*

In you recursive function, is better to pass the remaining length of the array and stop when you reach n==0.

Avoid to look at global variables or defines inside functions.

int minimo(int array[], int n)
{
  ...
  if (n == 0)
  ...
            min = minimo(array, n-1);
  ...
}

(other modification required)

Actually you should know the length of array only on main function, so call it as:

printf("Min: %d\n", minimo(array, N));

Best Buy Return & Exchange Policy, See the Best Buy return and exchange policy for info on returning or in cash or more than $250 by check or by a debit card without a major credit card logo, we� Making a Walmart Return Without a Receipt. If you need to return a Walmart purchase without a receipt, you should take your item to the customer service counter. Let the store associate know that you don’t have a receipt. The associate will then start the refund verification process (as previously reported). You need to bring all of the original packaging and accessories — the item’s packaging has a barcode that associates will scan to confirm the item was (and still is) sold at Walmart.


Customer Support: Return Policy at The Home Depot, Our Home Depot Return Policy is both fast, free & easy. Returns made without a valid sales receipt may be refunded as a Store Credit at the lowest advertised� Return to Store. Return to a Local Store. Return In-store Purchases. Mobile Express Return. Start a Mobile Express Return.


If you choose to return using Amazon Hub Locker, you'll be prompted to select an Amazon Hub Locker location. For more information about returning an item to an Amazon Hub Locker, go to Return a Package at an Amazon Hub Locker. Print your label and return authorization. Some returns don't require you to print a label or authorization.


If you would rather return the item by mail during your return and exchange time period, please call us for special instructions at 1-888-BEST BUY (1-888-237-8289). Returns lacking proof of purchase Reimbursements on returns lacking proof of purchase may be denied or limited, and state sales tax and fees will not be reimbursed.