{"id":84571,"date":"2021-09-06T09:00:46","date_gmt":"2021-09-06T03:30:46","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=84571"},"modified":"2021-09-06T09:00:46","modified_gmt":"2021-09-06T03:30:46","slug":"cpp-inline-function","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/cpp-inline-function\/","title":{"rendered":"C++ Inline Function with Examples"},"content":{"rendered":"<p>The objective behind using a function is to save memory space. When you call a function, it takes a lot of extra time in performing tasks such as jumping to the calling function. Sometimes, the time taken for jumping to the calling function will be greater than the time taken to execute that function.<\/p>\n<p>You can overcome this problem with the help of macros. But the main drawback of macros is that they are not functions that\u2019s why the error checking won\u2019t happen while compilation.<\/p>\n<p>But in C++, you can make use of inline functions to completely ignore this problem. Let us learn more about inline function in C++.<\/p>\n<h3>Inline Function in C++<\/h3>\n<p>From the name you can easily say that an inline function is a function that is expanded in line when it is invoked. Also helps in saving time.<\/p>\n<p>Using inline functions, the compiler will replace the function call with the function code which will reduce the overhead of function calls.<\/p>\n<p>But below are some points in which the compiler may not perform inlining:-<\/p>\n<ul>\n<li>A function containing a loop.<\/li>\n<li>If the function is recursive.<\/li>\n<li>If there are static variables present in the function.<\/li>\n<li>If there is a switch or goto statement in the function.<\/li>\n<li>If a return statement exists in the function.<\/li>\n<\/ul>\n<p><strong>Syntax of C++ Inline Function:-<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">inline function-header\n{\n\/\/ body of the function\n}\n<\/pre>\n<p><strong>Example of C++ inline function<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;iostream&gt;\nusing namespace std;\ninline int square(int a)\n{\nreturn a*a;\n}\ninline int cube(int s)\n{\nreturn s*s*s;\n}\nint main()\n{\nint a = 5;\ncout &lt;&lt; \"The cube of 5 is: \" &lt;&lt; cube(5) &lt;&lt; endl;\ncout &lt;&lt; \"Square of 5 is:  \" &lt;&lt; square(a) &lt;&lt; endl;\nreturn 0;\n}\n<\/pre>\n<p><strong>Output:-<\/strong><br \/>\nThe cube of 5 is: 125<br \/>\nSquare of 5 is: 25<\/p>\n<h4>When to use inline function in C++?<\/h4>\n<p>Below are some useful conditions to use the inline functions in C++:-<\/p>\n<ul>\n<li>If you need better performance, you can use an inline function.<\/li>\n<li>Over macros, you can also use the inline function.<\/li>\n<li>If you want to hide the implementation details of the function then you can use the inline keyword outside the class with the function definition.<\/li>\n<\/ul>\n<h4>Points to be noted when using C++ inline functions<\/h4>\n<ul>\n<li>You should always keep your inline function small to get better efficiency and great results.<\/li>\n<li>You should not turn all the functions into inline. Because it might decrease the efficiency and might lead to code bloat.<\/li>\n<li>You should define large functions outside the definition of a class using scope resolution.<\/li>\n<\/ul>\n<h4>Advantages of inline functions in C++<\/h4>\n<ul>\n<li>You don\u2019t have to call a function in the inline function. It avoids function call overhead.<\/li>\n<li>With the help of an inline function, you can also save the overhead of the return statement from a function.<\/li>\n<li>It does not require any stack.<\/li>\n<li>It is more beneficial to the embedded systems because it requires less code.<\/li>\n<\/ul>\n<h4>Disadvantages of inline functions in C++<\/h4>\n<ul>\n<li>If you make use of several inline functions then the binary executable will also become large.<\/li>\n<li>Use of several inline functions can reduce instruction cache hit rate.<\/li>\n<li>It can increase compile time overhead if any changes are made inside the inline function and then the code will have to recompile again to save the changes.<\/li>\n<li>If the size of the binary executable file is large then it can cause thrashing.<\/li>\n<\/ul>\n<h3>Summary<\/h3>\n<p>This was all about the inline function in C++. We discussed when to use the inline function. We also discussed the advantages and limitations of the inline function.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The objective behind using a function is to save memory space. When you call a function, it takes a lot of extra time in performing tasks such as jumping to the calling function. Sometimes,&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":84597,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3405],"tags":[4249,4250,4251],"class_list":["post-84571","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cpp","tag-advantages-of-c-inline-function","tag-disadvantages-of-c-inline-function","tag-inline-function-in-c"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>C++ Inline Function with Examples - TechVidvan<\/title>\n<meta name=\"description\" content=\"Learn about the inline function in C++. See when to use them. Learn advantages and limitations of the inline function.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techvidvan.com\/tutorials\/cpp-inline-function\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"C++ Inline Function with Examples - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"Learn about the inline function in C++. See when to use them. Learn advantages and limitations of the inline function.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/cpp-inline-function\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2021-09-06T03:30:46+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/08\/inline-Functions-in-C.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"C++ Inline Function with Examples - TechVidvan","description":"Learn about the inline function in C++. See when to use them. Learn advantages and limitations of the inline function.","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:\/\/techvidvan.com\/tutorials\/cpp-inline-function\/","og_locale":"en_US","og_type":"article","og_title":"C++ Inline Function with Examples - TechVidvan","og_description":"Learn about the inline function in C++. See when to use them. Learn advantages and limitations of the inline function.","og_url":"https:\/\/techvidvan.com\/tutorials\/cpp-inline-function\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2021-09-06T03:30:46+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/08\/inline-Functions-in-C.jpg","type":"image\/jpeg"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/cpp-inline-function\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/cpp-inline-function\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"C++ Inline Function with Examples","datePublished":"2021-09-06T03:30:46+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/cpp-inline-function\/"},"wordCount":533,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/cpp-inline-function\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/08\/inline-Functions-in-C.jpg","keywords":["advantages of C++ inline function","Disadvantages of C++ inline function","inline function in C++"],"articleSection":["C++ Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/cpp-inline-function\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/cpp-inline-function\/","url":"https:\/\/techvidvan.com\/tutorials\/cpp-inline-function\/","name":"C++ Inline Function with Examples - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/cpp-inline-function\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/cpp-inline-function\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/08\/inline-Functions-in-C.jpg","datePublished":"2021-09-06T03:30:46+00:00","description":"Learn about the inline function in C++. See when to use them. Learn advantages and limitations of the inline function.","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/cpp-inline-function\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/cpp-inline-function\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/cpp-inline-function\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/08\/inline-Functions-in-C.jpg","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/08\/inline-Functions-in-C.jpg","width":1200,"height":628,"caption":"inline Functions in C++"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/cpp-inline-function\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"C++ Inline Function with Examples"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22","name":"TechVidvan Team","description":"The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today\u2019s tech industry."}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/84571","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=84571"}],"version-history":[{"count":0,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/84571\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/84597"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=84571"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=84571"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=84571"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}