{"id":87,"date":"2026-05-20T11:14:04","date_gmt":"2026-05-20T11:14:04","guid":{"rendered":"https:\/\/gigz.pk\/cpp\/?post_type=lesson&#038;p=87"},"modified":"2026-05-22T08:02:56","modified_gmt":"2026-05-22T08:02:56","slug":"function-declaration","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/cpp\/?lesson=function-declaration","title":{"rendered":"Function Declaration"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">A function declaration in C++ tells the compiler about a function before it is used in the program. It specifies the function name, return type, and parameters.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is a Function Declaration?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A function declaration is also called a function prototype. It informs the compiler that a function exists and may be defined later in the program.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Function declaration helps:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Improve code organization<\/li>\n\n\n\n<li>Allow functions to be used before definition<\/li>\n\n\n\n<li>Make programs easier to understand<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Syntax of Function Declaration<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>return_type function_name(parameters);<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Example of Function Declaration<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;<br>using namespace std;<br><br>void greet();<br><br>int main() {<br>    greet();<br><br>    return 0;<br>}<br><br>void greet() {<br>    cout &lt;&lt; \"Welcome to C++\";<br>}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">How Function Declaration Works<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li>The function is declared before <code>main()<\/code><\/li>\n\n\n\n<li>The compiler recognizes the function<\/li>\n\n\n\n<li>The function can be called inside <code>main()<\/code><\/li>\n\n\n\n<li>Actual function definition can appear later<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Function Declaration with Parameters<\/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><br>int main() {<br>    cout &lt;&lt; add(5, 3);<br><br>    return 0;<br>}<br><br>int add(int a, int b) {<br>    return a + b;<br>}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Parts of a Function Declaration<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Return Type<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Defines what type of value the function returns.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Function Name<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The name used to call the function.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>add<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Parameters<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Values passed into the function.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>(int a, int b)<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Difference Between Declaration and Definition<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Function Declaration<\/th><th>Function Definition<\/th><\/tr><\/thead><tbody><tr><td>Tells compiler about function<\/td><td>Contains actual function code<\/td><\/tr><tr><td>Ends with semicolon<\/td><td>Includes function body<\/td><\/tr><tr><td>No implementation<\/td><td>Includes implementation<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Example of Multiple Function Declarations<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;<br>using namespace std;<br><br>void display();<br>int square(int);<br><br>int main() {<br>    display();<br>    cout &lt;&lt; square(4);<br><br>    return 0;<br>}<br><br>void display() {<br>    cout &lt;&lt; \"Hello\" &lt;&lt; endl;<br>}<br><br>int square(int n) {<br>    return n * n;<br>}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Why Function Declaration is Important<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Function declarations are important because they:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Improve code readability<\/li>\n\n\n\n<li>Support modular programming<\/li>\n\n\n\n<li>Allow functions to be defined later<\/li>\n\n\n\n<li>Help manage large programs<\/li>\n\n\n\n<li>Make code more organized<\/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 movie trailer:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The trailer tells you the movie exists<\/li>\n\n\n\n<li>Full movie comes later<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Similarly, a function declaration introduces the function before its actual implementation.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Function declaration in C++ is an essential concept that informs the compiler about functions before they are used. It helps organize code, supports modular programming, and makes programs easier to maintain and understand.<\/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 Declaration<\/span><\/span><\/div>\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><\/div>\n","protected":false},"menu_order":20,"template":"","class_list":["post-87","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 Declaration - Learn C++Language with GiGz.PK<\/title>\n<meta name=\"description\" content=\"Learn C++ function declaration (prototype) with syntax, examples, parameters, and how it improves code structure and readability.\" \/>\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 Declaration - Learn C++Language with GiGz.PK\" \/>\n<meta property=\"og:description\" content=\"Learn C++ function declaration (prototype) with syntax, examples, parameters, and how it improves code structure and readability.\" \/>\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:02:56+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-declaration\",\"url\":\"https:\\\/\\\/gigz.pk\\\/\",\"name\":\"Function Declaration - Learn C++Language with GiGz.PK\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/cpp\\\/#website\"},\"datePublished\":\"2026-05-20T11:14:04+00:00\",\"dateModified\":\"2026-05-22T08:02:56+00:00\",\"description\":\"Learn C++ function declaration (prototype) with syntax, examples, parameters, and how it improves code structure and readability.\",\"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 Declaration\"}]},{\"@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 Declaration - Learn C++Language with GiGz.PK","description":"Learn C++ function declaration (prototype) with syntax, examples, parameters, and how it improves code structure and readability.","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 Declaration - Learn C++Language with GiGz.PK","og_description":"Learn C++ function declaration (prototype) with syntax, examples, parameters, and how it improves code structure and readability.","og_url":"https:\/\/gigz.pk\/","og_site_name":"Learn C++Language with GiGz.PK","article_modified_time":"2026-05-22T08:02:56+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-declaration","url":"https:\/\/gigz.pk\/","name":"Function Declaration - Learn C++Language with GiGz.PK","isPartOf":{"@id":"https:\/\/gigz.pk\/cpp\/#website"},"datePublished":"2026-05-20T11:14:04+00:00","dateModified":"2026-05-22T08:02:56+00:00","description":"Learn C++ function declaration (prototype) with syntax, examples, parameters, and how it improves code structure and readability.","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 Declaration"}]},{"@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\/87","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=87"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}