{"id":93,"date":"2026-05-20T11:33:17","date_gmt":"2026-05-20T11:33:17","guid":{"rendered":"https:\/\/gigz.pk\/cpp\/?post_type=lesson&#038;p=93"},"modified":"2026-05-22T08:11:35","modified_gmt":"2026-05-22T08:11:35","slug":"function-overloading","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/cpp\/?lesson=function-overloading","title":{"rendered":"Function Overloading"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Function overloading in C++ allows multiple functions to have the same name but different parameters. It is a feature of polymorphism that improves code readability and flexibility.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is Function Overloading?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Function overloading means creating multiple functions with the same name in the same scope, but with different:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Number of parameters<\/li>\n\n\n\n<li>Data types of parameters<\/li>\n\n\n\n<li>Order of parameters<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">The compiler identifies the correct function based on the arguments passed.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why Use Function Overloading?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Function overloading is useful because it:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Reduces the need for different function names<\/li>\n\n\n\n<li>Improves code readability<\/li>\n\n\n\n<li>Makes programs easier to manage<\/li>\n\n\n\n<li>Supports reusable programming<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Example of Function Overloading<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;<br>using namespace std;<br><br>class Math {<br>public:<br>    int add(int a, int b) {<br>        return a + b;<br>    }<br><br>    int add(int a, int b, int c) {<br>        return a + b + c;<br>    }<br>};<br><br>int main() {<br>    Math obj;<br><br>    cout &lt;&lt; obj.add(2, 3) &lt;&lt; endl;<br>    cout &lt;&lt; obj.add(2, 3, 4);<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>5<br>9<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">How Function Overloading Works<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Functions have the same name<\/li>\n\n\n\n<li>Parameters are different<\/li>\n\n\n\n<li>Compiler checks function arguments<\/li>\n\n\n\n<li>Matching function is called automatically<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Overloading by Data Type<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;<br>using namespace std;<br><br>class Display {<br>public:<br>    void show(int a) {<br>        cout &lt;&lt; \"Integer: \" &lt;&lt; a &lt;&lt; endl;<br>    }<br><br>    void show(double b) {<br>        cout &lt;&lt; \"Double: \" &lt;&lt; b &lt;&lt; endl;<br>    }<br>};<br><br>int main() {<br>    Display obj;<br><br>    obj.show(10);<br>    obj.show(5.5);<br><br>    return 0;<br>}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Overloading by Number of Parameters<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;<br>using namespace std;<br><br>void print(int a) {<br>    cout &lt;&lt; a &lt;&lt; endl;<br>}<br><br>void print(int a, int b) {<br>    cout &lt;&lt; a &lt;&lt; \" \" &lt;&lt; b &lt;&lt; endl;<br>}<br><br>int main() {<br>    print(5);<br>    print(5, 10);<br><br>    return 0;<br>}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Rules of Function Overloading<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Function names must be the same<\/li>\n\n\n\n<li>Parameters must differ<\/li>\n\n\n\n<li>Return type alone cannot overload a function<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Invalid Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>int add(int a, int b);<br>float add(int a, int b);<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This causes an error because only the return type differs.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why Function Overloading is Important<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Function overloading is important because it:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Simplifies code writing<\/li>\n\n\n\n<li>Improves readability<\/li>\n\n\n\n<li>Supports polymorphism<\/li>\n\n\n\n<li>Reduces confusion with multiple function names<\/li>\n\n\n\n<li>Makes programs more flexible<\/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 mobile phone contact:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Same person name<\/li>\n\n\n\n<li>Different phone numbers<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">The name remains the same, but details differ.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Similarly, overloaded functions share the same name but work differently based on parameters.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Function overloading in C++ is a powerful feature that allows multiple functions with the same name to perform different tasks based on parameters. It improves flexibility, readability, and code organization in modern programming.<\/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 >Function Overloading<\/span><\/span><\/div>\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><\/div>\n","protected":false},"menu_order":23,"template":"","class_list":["post-93","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>Function Overloading - Learn C++Language with GiGz.PK<\/title>\n<meta name=\"description\" content=\"Learn C++ function overloading with examples to understand polymorphism, multiple functions with same name, and different parameters.\" \/>\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=\"Function Overloading - Learn C++Language with GiGz.PK\" \/>\n<meta property=\"og:description\" content=\"Learn C++ function overloading with examples to understand polymorphism, multiple functions with same name, and different parameters.\" \/>\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:11:35+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=function-overloading\",\"url\":\"https:\\\/\\\/gigz.pk\\\/\",\"name\":\"Function Overloading - Learn C++Language with GiGz.PK\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/cpp\\\/#website\"},\"datePublished\":\"2026-05-20T11:33:17+00:00\",\"dateModified\":\"2026-05-22T08:11:35+00:00\",\"description\":\"Learn C++ function overloading with examples to understand polymorphism, multiple functions with same name, and different parameters.\",\"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 >Function Overloading\"}]},{\"@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":"Function Overloading - Learn C++Language with GiGz.PK","description":"Learn C++ function overloading with examples to understand polymorphism, multiple functions with same name, and different parameters.","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":"Function Overloading - Learn C++Language with GiGz.PK","og_description":"Learn C++ function overloading with examples to understand polymorphism, multiple functions with same name, and different parameters.","og_url":"https:\/\/gigz.pk\/","og_site_name":"Learn C++Language with GiGz.PK","article_modified_time":"2026-05-22T08:11:35+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=function-overloading","url":"https:\/\/gigz.pk\/","name":"Function Overloading - Learn C++Language with GiGz.PK","isPartOf":{"@id":"https:\/\/gigz.pk\/cpp\/#website"},"datePublished":"2026-05-20T11:33:17+00:00","dateModified":"2026-05-22T08:11:35+00:00","description":"Learn C++ function overloading with examples to understand polymorphism, multiple functions with same name, and different parameters.","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 >Function Overloading"}]},{"@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\/93","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=93"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}