{"id":91,"date":"2026-05-20T11:31:49","date_gmt":"2026-05-20T11:31:49","guid":{"rendered":"https:\/\/gigz.pk\/cpp\/?post_type=lesson&#038;p=91"},"modified":"2026-05-22T08:08:59","modified_gmt":"2026-05-22T08:08:59","slug":"return-values","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/cpp\/?lesson=return-values","title":{"rendered":"Return Values"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Return values in C++ are used to send data back from a function to the place where the function was called. They help functions produce results that can be used later in the program.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is a Return Value?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A return value is the value that a function sends back after completing its task.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>return<\/code> keyword is used to return a value from a function.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why Return Values are Important<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Return values are useful because they:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Allow functions to produce results<\/li>\n\n\n\n<li>Improve code reusability<\/li>\n\n\n\n<li>Make programs more organized<\/li>\n\n\n\n<li>Reduce repetitive code<\/li>\n\n\n\n<li>Help perform calculations efficiently<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Syntax of Return Value<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>return_type function_name() {<br>    return value;<br>}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Example of Return Value<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;<br>using namespace std;<br><br>int add(int a, int b) {<br>    return a + b;<br>}<br><br>int main() {<br>    int result = add(5, 3);<br><br>    cout &lt;&lt; result;<br><br>    return 0;<br>}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Output<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>8<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">How Return Values Work<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Function performs a task<\/li>\n\n\n\n<li><code>return<\/code> sends value back<\/li>\n\n\n\n<li>Returned value is stored or used<\/li>\n\n\n\n<li>Control returns to the calling function<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Return Types in C++<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Functions can return different data types.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Returning Integer<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>int square(int n) {<br>    return n * n;<br>}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Returning Float<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>float divide(float a, float b) {<br>    return a \/ b;<br>}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Returning String<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>string message() {<br>    return \"Hello\";<br>}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Void Functions<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">If a function does not return any value, use <code>void<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void greet() {<br>    cout &lt;&lt; \"Welcome\";<br>}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Example with User Input<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;<br>using namespace std;<br><br>int multiply(int a, int b) {<br>    return a * b;<br>}<br><br>int main() {<br>    int x, y;<br><br>    cout &lt;&lt; \"Enter two numbers: \";<br>    cin &gt;&gt; x &gt;&gt; y;<br><br>    cout &lt;&lt; \"Result: \" &lt;&lt; multiply(x, y);<br><br>    return 0;<br>}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Important Points<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A function can return only one value directly<\/li>\n\n\n\n<li><code>return<\/code> ends the function immediately<\/li>\n\n\n\n<li>Return type must match the returned value<\/li>\n\n\n\n<li><code>void<\/code> functions do not return values<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Why Return Values are Important<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Return values are important because they:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Make functions reusable<\/li>\n\n\n\n<li>Help create modular programs<\/li>\n\n\n\n<li>Allow result sharing between functions<\/li>\n\n\n\n<li>Improve program efficiency<\/li>\n\n\n\n<li>Simplify complex calculations<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Real-Life Example<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Think of a calculator:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>You enter numbers<\/li>\n\n\n\n<li>Calculator processes them<\/li>\n\n\n\n<li>Final answer is returned to you<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">This is similar to how functions return values.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Return values in C++ allow functions to send results back to the calling code. They are essential for creating reusable, organized, and efficient programs. Understanding return values is an important step in mastering functions in C++.<\/p>\n\n\n<div class=\"yoast-breadcrumbs\"><span><span><a href=\"https:\/\/gigz.pk\/cpp\">Home<\/a><\/span> \u00bb <span class=\"breadcrumb_last\" aria-current=\"page\">Intermediate C++ > Functions > Return Values<\/span><\/span><\/div>\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><\/div>\n","protected":false},"menu_order":22,"template":"","class_list":["post-91","lesson","type-lesson","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Return Values - Learn C++Language with GiGz.PK<\/title>\n<meta name=\"description\" content=\"Learn C++ return values with examples to understand how functions send results back and improve program efficiency and reusability.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/gigz.pk\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Return Values - Learn C++Language with GiGz.PK\" \/>\n<meta property=\"og:description\" content=\"Learn C++ return values with examples to understand how functions send results back and improve program efficiency and reusability.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gigz.pk\/\" \/>\n<meta property=\"og:site_name\" content=\"Learn C++Language with GiGz.PK\" \/>\n<meta property=\"article:modified_time\" content=\"2026-05-22T08:08:59+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\\\/\\\/gigz.pk\\\/cpp\\\/?lesson=return-values\",\"url\":\"https:\\\/\\\/gigz.pk\\\/\",\"name\":\"Return Values - Learn C++Language with GiGz.PK\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/cpp\\\/#website\"},\"datePublished\":\"2026-05-20T11:31:49+00:00\",\"dateModified\":\"2026-05-22T08:08:59+00:00\",\"description\":\"Learn C++ return values with examples to understand how functions send results back and improve program efficiency and reusability.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gigz.pk\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gigz.pk\\\/cpp\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Intermediate C++ > Functions > Return Values\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/cpp\\\/#website\",\"url\":\"https:\\\/\\\/gigz.pk\\\/cpp\\\/\",\"name\":\"Learn C++Language with GiGz.PK\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/gigz.pk\\\/cpp\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Return Values - Learn C++Language with GiGz.PK","description":"Learn C++ return values with examples to understand how functions send results back and improve program efficiency and reusability.","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:\/\/gigz.pk\/","og_locale":"en_US","og_type":"article","og_title":"Return Values - Learn C++Language with GiGz.PK","og_description":"Learn C++ return values with examples to understand how functions send results back and improve program efficiency and reusability.","og_url":"https:\/\/gigz.pk\/","og_site_name":"Learn C++Language with GiGz.PK","article_modified_time":"2026-05-22T08:08:59+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["WebPage","FAQPage"],"@id":"https:\/\/gigz.pk\/cpp\/?lesson=return-values","url":"https:\/\/gigz.pk\/","name":"Return Values - Learn C++Language with GiGz.PK","isPartOf":{"@id":"https:\/\/gigz.pk\/cpp\/#website"},"datePublished":"2026-05-20T11:31:49+00:00","dateModified":"2026-05-22T08:08:59+00:00","description":"Learn C++ return values with examples to understand how functions send results back and improve program efficiency and reusability.","breadcrumb":{"@id":"https:\/\/gigz.pk\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gigz.pk\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gigz.pk\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gigz.pk\/cpp"},{"@type":"ListItem","position":2,"name":"Intermediate C++ > Functions > Return Values"}]},{"@type":"WebSite","@id":"https:\/\/gigz.pk\/cpp\/#website","url":"https:\/\/gigz.pk\/cpp\/","name":"Learn C++Language with GiGz.PK","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/gigz.pk\/cpp\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/gigz.pk\/cpp\/index.php?rest_route=\/wp\/v2\/lesson\/91","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gigz.pk\/cpp\/index.php?rest_route=\/wp\/v2\/lesson"}],"about":[{"href":"https:\/\/gigz.pk\/cpp\/index.php?rest_route=\/wp\/v2\/types\/lesson"}],"wp:attachment":[{"href":"https:\/\/gigz.pk\/cpp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=91"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}