{"id":16951,"date":"2022-10-22T15:59:10","date_gmt":"2022-10-22T10:29:10","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-what-is-the-result-of-calling-return-in-a-non-void-function-after-calling-a-function-returning-int\/"},"modified":"2022-10-22T15:59:10","modified_gmt":"2022-10-22T10:29:10","slug":"solved-what-is-the-result-of-calling-return-in-a-non-void-function-after-calling-a-function-returning-int","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-what-is-the-result-of-calling-return-in-a-non-void-function-after-calling-a-function-returning-int\/","title":{"rendered":"[Solved] What is the result of calling return in a non-void function after calling a function returning int?"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-41200617\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"41200617\" data-parentid=\"41148654\" data-score=\"0\" data-position-on-page=\"2\" data-highest-scored=\"0\" data-question-has-accepted-highest-score=\"0\" itemprop=\"suggestedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>The issue here is you don&#8217;t seem to understand what <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/en.wikipedia.org\/wiki\/Undefined_behavior\">undefined behavior<\/a> means.<\/p>\n<p>When you invoke undefined behavior, anything can happen.  Your program can crash, it can generate unexpected results, or it can appear to work correctly.  Making a seeming unrelated change, such as adding an unused variable or an extra call to <code>printf<\/code>, can change how undefined behavior manifests itself.<\/p>\n<p>This also means that two different compilers can generate different results for the same code, or that one compiler with two different optimization settings can generate different results.<\/p>\n<p>I&#8217;ll give you an example.  I compiled your original code on a CentOS 5.10 with gcc 4.1.2.  First I compiled without any optimization:<\/p>\n<pre><code>gcc -Wall -Wextra -g -o \/tmp\/x1 \/tmp\/x1.c\n<\/code><\/pre>\n<p>I ran the resulting code and then ran <code>echo $?<\/code>.  The latter prints the exit code of the previous process (i.e. the return value of <code>main<\/code>).  The result:<\/p>\n<pre><code>[dbush@db-centos tmp]$ \/tmp\/x1\n[dbush@db-centos tmp]$ echo $?\n3\n[dbush@db-centos tmp]$ \n<\/code><\/pre>\n<p>Now I&#8217;ll compile the same code on a Windows 7 machine under Visual Studio 2010:<\/p>\n<pre><code>cl x1.c\n<\/code><\/pre>\n<p>If I run this and then <code>echo %ERRORLEVEL%<\/code> to print the return code, I get this:<\/p>\n<pre><code>C:\\Users\\dbush\\Documents&gt;x1\n\nC:\\Users\\dbush\\Documents&gt;echo %ERRORLEVEL%\n0\n\nC:\\Users\\dbush\\Documents&gt;\n<\/code><\/pre>\n<p>As you can see, gcc and MSVC generate different results for the same code.  In one case it returns 3 while in the other case it return 0.<\/p>\n<p>Now let&#8217;s play around with optimization.  I compiled the same code on the same CentOS machine with the same version of gcc, but with optimization turned on:<\/p>\n<pre><code>gcc -Wall -Wextra -g -o \/tmp\/x1 \/tmp\/x1.c -O3\n<\/code><\/pre>\n<p>I then ran this twice. I then got the following result:<\/p>\n<pre><code>[dbush@db-centos tmp]$ \/tmp\/x1\n[dbush@db-centos tmp]$ echo $?\n68\n[dbush@db-centos tmp]$ \/tmp\/x1\n[dbush@db-centos tmp]$ echo $?\n36\n[dbush@db-centos tmp]$ \n<\/code><\/pre>\n<p>So not only do I get different results when running with different optimization settings, I get different results running the <strong>same program multiple times<\/strong>.  That&#8217;s what can happen with undefined behavior.<\/p>\n<p>So to answer your question &#8220;what return value will be produced&#8221;, the answer is it depends because you invoked undefined behavior.  You can&#8217;t reliably predict what will happen.<\/p>\n<p>EDIT:<\/p>\n<p>More examples with your updated code.<\/p>\n<p>On gcc with no optimizations:<\/p>\n<pre><code>[dbush@db-centos tmp]$ \/tmp\/x1\nmytest2 = 3\n[dbush@db-centos tmp]$ echo $?\n12\n<\/code><\/pre>\n<p>On gcc with optimization <code>-O3<\/code>:<\/p>\n<pre><code>[dbush@db-centos tmp]$ \/tmp\/x1\nmytest2 = -1078711820\n[dbush@db-centos tmp]$ echo $?\n22\n[dbush@db-centos tmp]$ \/tmp\/x1\nmytest2 = -1077511916\n[dbush@db-centos tmp]$ echo $?\n22\n<\/code><\/pre>\n<p>On MSVC with no optimizations:<\/p>\n<pre><code>C:\\Users\\dbush\\Documents&gt;x1\nmytest2 = 3\n\nC:\\Users\\dbush\\Documents&gt;echo %ERRORLEVEL%\n0\n\nC:\\Users\\dbush\\Documents&gt;\n<\/code><\/pre>\n<p>On MSVC with optimization <code>\/Ox<\/code>:<\/p>\n<pre><code>C:\\Users\\dbush\\Documents&gt;x1\nmytest2 = 1\n\nC:\\Users\\dbush\\Documents&gt;echo %ERRORLEVEL%\n0\n\nC:\\Users\\dbush\\Documents&gt;\n<\/code><\/pre>\n<p>So again, no guarantees.<\/p>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">1<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved What is the result of calling return in a non-void function after calling a function returning int? <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] The issue here is you don&#8217;t seem to understand what undefined behavior means. When you invoke undefined behavior, anything can happen. Your program can crash, it can generate unexpected results, or it can appear to work correctly. Making a seeming unrelated change, such as adding an unused variable or an extra call to printf, &#8230; <a title=\"[Solved] What is the result of calling return in a non-void function after calling a function returning int?\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-what-is-the-result-of-calling-return-in-a-non-void-function-after-calling-a-function-returning-int\/\" aria-label=\"More on [Solved] What is the result of calling return in a non-void function after calling a function returning int?\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[324,1818,2307],"class_list":["post-16951","post","type-post","status-publish","format-standard","hentry","category-solved","tag-c","tag-return","tag-return-value"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] What is the result of calling return in a non-void function after calling a function returning int? - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-what-is-the-result-of-calling-return-in-a-non-void-function-after-calling-a-function-returning-int\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] What is the result of calling return in a non-void function after calling a function returning int? - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] The issue here is you don&#8217;t seem to understand what undefined behavior means. When you invoke undefined behavior, anything can happen. Your program can crash, it can generate unexpected results, or it can appear to work correctly. Making a seeming unrelated change, such as adding an unused variable or an extra call to printf, ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-what-is-the-result-of-calling-return-in-a-non-void-function-after-calling-a-function-returning-int\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-10-22T10:29:10+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-what-is-the-result-of-calling-return-in-a-non-void-function-after-calling-a-function-returning-int\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-what-is-the-result-of-calling-return-in-a-non-void-function-after-calling-a-function-returning-int\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] What is the result of calling return in a non-void function after calling a function returning int?\",\"datePublished\":\"2022-10-22T10:29:10+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-what-is-the-result-of-calling-return-in-a-non-void-function-after-calling-a-function-returning-int\/\"},\"wordCount\":358,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"c++\",\"return\",\"return-value\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-what-is-the-result-of-calling-return-in-a-non-void-function-after-calling-a-function-returning-int\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-what-is-the-result-of-calling-return-in-a-non-void-function-after-calling-a-function-returning-int\/\",\"name\":\"[Solved] What is the result of calling return in a non-void function after calling a function returning int? - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-10-22T10:29:10+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-what-is-the-result-of-calling-return-in-a-non-void-function-after-calling-a-function-returning-int\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-what-is-the-result-of-calling-return-in-a-non-void-function-after-calling-a-function-returning-int\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-what-is-the-result-of-calling-return-in-a-non-void-function-after-calling-a-function-returning-int\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] What is the result of calling return in a non-void function after calling a function returning int?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] What is the result of calling return in a non-void function after calling a function returning int? - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-what-is-the-result-of-calling-return-in-a-non-void-function-after-calling-a-function-returning-int\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] What is the result of calling return in a non-void function after calling a function returning int? - JassWeb","og_description":"[ad_1] The issue here is you don&#8217;t seem to understand what undefined behavior means. When you invoke undefined behavior, anything can happen. Your program can crash, it can generate unexpected results, or it can appear to work correctly. Making a seeming unrelated change, such as adding an unused variable or an extra call to printf, ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-what-is-the-result-of-calling-return-in-a-non-void-function-after-calling-a-function-returning-int\/","og_site_name":"JassWeb","article_published_time":"2022-10-22T10:29:10+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-what-is-the-result-of-calling-return-in-a-non-void-function-after-calling-a-function-returning-int\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-what-is-the-result-of-calling-return-in-a-non-void-function-after-calling-a-function-returning-int\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] What is the result of calling return in a non-void function after calling a function returning int?","datePublished":"2022-10-22T10:29:10+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-what-is-the-result-of-calling-return-in-a-non-void-function-after-calling-a-function-returning-int\/"},"wordCount":358,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["c++","return","return-value"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-what-is-the-result-of-calling-return-in-a-non-void-function-after-calling-a-function-returning-int\/","url":"https:\/\/jassweb.com\/solved\/solved-what-is-the-result-of-calling-return-in-a-non-void-function-after-calling-a-function-returning-int\/","name":"[Solved] What is the result of calling return in a non-void function after calling a function returning int? - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-10-22T10:29:10+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-what-is-the-result-of-calling-return-in-a-non-void-function-after-calling-a-function-returning-int\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-what-is-the-result-of-calling-return-in-a-non-void-function-after-calling-a-function-returning-int\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-what-is-the-result-of-calling-return-in-a-non-void-function-after-calling-a-function-returning-int\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] What is the result of calling return in a non-void function after calling a function returning int?"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/16951","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=16951"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/16951\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=16951"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=16951"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=16951"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}