How is the data returned to main?

main() return
return c++
what is the return type of main() in c++
return by reference c++
return by reference in c++ ppt
c++ what to return from main
main return char
should main always return a value

I wrote this piece of code and it appears to work but when I re-analyze it, I wonder how the data (minimum) is returned to the main function? Given this binary tree:

       14      19
    12       18  20
       13          21

And this code:

struct Node {
   int data;
   Node* left;
   Node* right;

int min(Node* root) {
   if(root == NULL)
      return 0;
   else if(root->left == NULL)
      return root->data;

The last calling to min(Node*) will return root->data but the caller has no return in min(root->left). All the returns are already been skipped in the previous min(Node*).

The code has Undefined Behaviour, so it works by accident. It's illegal in C++ for control flow to reach the end of a non-void function without returning a value (main is the only exception).

Note that you should always compile your code with warnings enabled, in which case this would have been flagged by the compiler.

What may be happening is that the return value gets stored in a register (normally eax, I believe) by the most nested call, and since the other calls don't actually execute a return, they don't overwrite it. However, do note that this is pure speculation and could change with a different compiler, different compilation flags etc. Undefined behaviour is undefined and can never be relied upon.

What does main() return in C and C++?, What's Difference? Quizzes expand_more. C · C++ · Java · Python · Data Structures · Algorithms · Operating Systems · DBMS · Compiler Design  Handling Data Returned from a SQL Stored Procedure Posted on December 20, 2019 December 15, 2019 Author Drew 1 There are several ways to capture the output from a stored procedure in SQL Server, and if you are working with an existing code set chances are you will not be able to only rely on one of the methods.

This code contains undefined behavior, so from a language-lawyer perspective, it's sheer luck that it works.

In practice, what is most likely happening (on X86 architectures) is that the min() function stores the result in the eax register.

Since the function does nothing at all after recursively invoking itself, the eax register is left in whatever state the called function left it, and that implicitly becomes the result of the function itself.

In short, min(root->left); gets compiled exactly as if it was return min(root->left); out of sheer luck.


An additional note: Your function is written in a way that allows for something called tail recursion, which may or may not be coming into play here. In any case, you should never rely on that behavior anyways.

7.4a, The primary difference between the two is simply that the direction of data flow is reversed. so it's okay to return it by reference. return array[index];. } int main(). To return data to a caller, a function needs to include the keyword return, followed by the data to return. You have no limit on what you can return to a caller. Here are some types of data that you commonly see returned by a function to a caller: Values: Any value is acceptable. You can return numbers, such as 1 or 2.5; strings, such as “Hello There!”; or Boolean values, such as True or False.

Did you actually mean "and it [accidentally] works [on the test data I tried]?" For, to make it work, the last branch should read else return min(root->left); Did your compiler issued any warnings about possible control paths with no return?

What's the difference between the print and the return functions , return is the main way that a function returns a value. Return gives us a value, or any data that needs to be assigned to a variable or use print statement to print​  "on this task, the instructions were to use getMinutes(), to return a value to the main method. and then use that value on calcMeth(), to get the calculation" maybe it's just me, but that reads that the value returned from getMinutes (called in main) should be passed to calcMeth – MadProgrammer Sep 22 '15 at 23:43

6.2. Functions that Return Values, Fruitful functions still allow the user to provide information (arguments). However there is now an additional piece of data that is returned from the function. ../  In addition to passing a linking field to a subreport from a main report to limit the subreport s record selection, you may want to pass data from a main report to a subreport for other purposes. Or, you may want to pass data from a subreport back to the main report to use in summary calculations or similar functions.

3.6. Returned Function Values, using System; class Return1 { static int f(int x) { return x*x; } static void Main() Since the function returns data, and all data in C# is typed, there must be a type  I have been testing offline mode by pulling in data (online) into a collection.. Then using this collection to get the ID of the row i am about to update. Once i have the ID and all my updated data, I add it to a new collection (SendData) which i store in an offline table. Here is what happens on the Save button when updating a record within

C Programming Course Notes, The "return type" indicates what kind of data this function will return. void func( double x, double z, double b, double c ); int main( void ) { double x( 1.0 ), y( 2.0 )  Returning no data from a query in a previous test run is not an error! The buttons highlighted in the picture refer to a formula that is invoked OnSelect: ClearCollect(outstandingOrders, GoodsInToday.Run(WeekNo, SelectedYear, 3)) The AppChecker says these runs failed. They did not fail. They simply returned no data.

  • "I wrote this piece of code and it works" -> "I wrote this piece of code and it appears to work".
  • So, one of the solution is adding return in min(root->left)?
  • @NarutoUzumaki That's exactly what you should be doing. Personally, I would also get rid of that final else. so that all code paths have a return statement. The official rule is "every reachable code path of a non-void function must lead to a valid return statement", but applying the rule to all code paths, reachable or not, is cleaner.
  • The compiler didn't issued any warnings about this. I compiled it using this command: g++ file.cpp -o main. I'm using Windows Subsystem for Linux (Ubuntu).
  • @NarutoUzumaki: you should always add -Wall when you compile, i.e. g++ -Wall file.cpp -o main - otherwise you won't get helpful compiler warnings. Try it with the code above and you will see how much time this might have saved you !
  • But it is actually strange that default gcc did not complain at all.