{"id":31053,"date":"2021-04-16T11:53:47","date_gmt":"2021-04-16T06:23:47","guid":{"rendered":"https:\/\/www.mygreatlearning.com\/blog\/function-in-c\/"},"modified":"2025-06-20T19:46:22","modified_gmt":"2025-06-20T14:16:22","slug":"function-in-c","status":"publish","type":"post","link":"https:\/\/www.mygreatlearning.com\/blog\/function-in-c\/","title":{"rendered":"C++ Functions"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\" id=\"what-is-a-c-function\">What Is a C++ Function?<\/h2>\n\n\n\n<p>A C++ function is a named block of code designed to perform a specific operation, accepting input values (arguments) and optionally returning a result. You define it once and call it multiple times.<\/p>\n\n\n\n<p>Using C++ functions provides several benefits:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><b>Code Reusability:<\/b> You write code once and use it many times. This saves effort and reduces errors.<\/li>\n\n\n\n<li><b>Modularity:<\/b> Functions break down large programs into smaller, manageable pieces. This makes your code easier to understand and debug.<\/li>\n\n\n\n<li><b>Readability:<\/b> Well-defined functions make your program flow clear. You can see what each part of the code does.<\/li>\n\n\n\n<li><b>Easier Debugging:<\/b> When an issue arises, you can isolate the problem to a specific function. This speeds up debugging.<\/li>\n\n\n\n<li><b>Reduced Redundancy:<\/b> Avoid repeating the same code block. Call a function instead.<\/li>\n<\/ul>\n\n\n\n    <div class=\"courses-cta-container\">\n        <div class=\"courses-cta-card\">\n            <div class=\"courses-cta-header\">\n                <div class=\"courses-learn-icon\"><\/div>\n                <span class=\"courses-learn-text\">Academy Pro<\/span>\n            <\/div>\n            <p class=\"courses-cta-title\">\n                <a href=\"https:\/\/www.mygreatlearning.com\/academy\/premium\/learn-c-programming-for-beginners-to-advanced\" class=\"courses-cta-title-link\">C++ Programming Course<\/a>\n            <\/p>\n            <p class=\"courses-cta-description\">Master key C++ programming concepts like variables, functions, OOP, and control structures. Build real-world projects such as a banking system and grade management tool.<\/p>\n            <div class=\"courses-cta-stats\">\n                <div class=\"courses-stat-item\">\n                    <div class=\"courses-stat-icon courses-user-icon\"><\/div>\n                    <span>Beginner to Advanced Level<\/span>\n                <\/div>\n                <div class=\"courses-stat-item\">\n                    <div class=\"courses-stat-icon courses-star-icon\"><\/div>\n                    <span>8.1 hrs<\/span>\n                <\/div>\n            <\/div>\n            <a href=\"https:\/\/www.mygreatlearning.com\/academy\/premium\/learn-c-programming-for-beginners-to-advanced\" class=\"courses-cta-button\">\n                Start Free Trial\n                <div class=\"courses-arrow-icon\"><\/div>\n            <\/a>\n        <\/div>\n    <\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"types-of-c-functions\">Types of C++ Functions<\/h2>\n\n\n\n<p>C++ has two main types of functions:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><b>Standard Library Functions:<\/b> These are built-in functions ready for you to use. Examples include <code>sqrt()<\/code> for square root or <code>cout<\/code> for printing. You include specific headers to access them, like <code>&lt;cmath&gt;<\/code> or <code>&lt;iostream&gt;<\/code>.<\/li>\n\n\n\n<li><b>User-Defined Functions:<\/b> You create these functions to perform specific tasks unique to your program. Most of this guide focuses on these.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"how-to-define-a-c-function\">How to Define a C++ Function<\/h2>\n\n\n\n<p>Defining a C++ function involves a few key parts:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nreturn_type function_name(parameter_list) {\n    \/\/ Function body (code to be executed)\n    return value; \/\/ Optional, if return_type is not void\n}\n<\/pre><\/div>\n\n\n<p>Let's break down each part:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><b><code>return_type<\/code>:<\/b> This specifies the data type of the value the function sends back. If the function does not return any value, use <code>void<\/code>.<\/li>\n\n\n\n<li><b><code>function_name<\/code>:<\/b> This is the unique name you give your function. Follow naming conventions (e.g., <code>calculateSum<\/code>, <code>printMessage<\/code>).<\/li>\n\n\n\n<li><b><code>parameter_list<\/code>:<\/b> This is a comma-separated list of inputs the function accepts. Each parameter has a type and a name (e.g., <code>int num1, double num2<\/code>). If the function takes no inputs, use empty parentheses <code>()<\/code>.<\/li>\n\n\n\n<li><b>Function Body:<\/b> This is the code enclosed in curly braces <code>{}<\/code>. It contains the instructions the function executes.<\/li>\n\n\n\n<li><b><code>return<\/code> statement:<\/b> This statement (optional for <code>void<\/code> functions) sends a value back to the code that called the function. The data type of this value must match the <code>return_type<\/code>.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example-of-a-simple-function-definition\">Example of a Simple Function Definition:<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nint addNumbers(int a, int b) {\n    int sum = a + b;\n    return sum;\n}\n<\/pre><\/div>\n\n\n<p>This function is named <code>addNumbers<\/code>. It takes two integer inputs (<code>a<\/code> and <code>b<\/code>). It returns an integer, which is the sum of <code>a<\/code> and <code>b<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"declaring-a-c-function-function-prototype\">Declaring a C++ Function (Function Prototype)<\/h2>\n\n\n\n<p>Before you use a function, you must declare it. A function declaration (also called a function prototype) tells the compiler about the function\u2019s name, return type, and parameters. This allows you to call the function before its full definition appears in the code.<\/p>\n\n\n\n<p>Place function declarations at the beginning of your program, often before the <code>main()<\/code> function.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"syntax-for-function-declaration\">Syntax for Function Declaration:<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nreturn_type function_name(parameter_list);\n<\/pre><\/div>\n\n\n<p>Notice the semicolon <code>;<\/code> at the end instead of curly braces.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example-of-function-declaration\">Example of Function Declaration:<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n#include &amp;lt;iostream&gt;\n\n\/\/ Function declaration\nint multiply(int x, int y);\n\nint main() {\n    int result = multiply(5, 3); \/\/ Call the function\n    std::cout &amp;lt;&amp;lt; &quot;The product is: &quot; &amp;lt;&amp;lt; result &amp;lt;&amp;lt; std::endl;\n    return 0;\n}\n\n\/\/ Function definition\nint multiply(int x, int y) {\n    return x * y;\n}\n<\/pre><\/div>\n\n\n<p>Here, <code>int multiply(int x, int y);<\/code> is the function declaration. It appears before <code>main()<\/code>, so <code>main()<\/code> knows about <code>multiply()<\/code> even though its definition is below <code>main()<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"how-to-call-a-c-function\">How to Call a C++ Function<\/h2>\n\n\n\n<p>Calling a function executes the code within its body. You call a function by using its name followed by parentheses containing any required arguments.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nfunction_name(arguments);\n<\/pre><\/div>\n\n\n<ul class=\"wp-block-list\">\n<li><b><code>arguments<\/code>:<\/b> These are the actual values you pass to the function. Their order and types must match the parameters in the function definition.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example-of-calling-a-function\">Example of Calling a Function:<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n#include &amp;lt;iostream&gt;\n\nint addNumbers(int a, int b) {\n    return a + b;\n}\n\nint main() {\n    int num1 = 10;\n    int num2 = 20;\n    int sum = addNumbers(num1, num2); \/\/ Call addNumbers function\n    std::cout &amp;lt;&amp;lt; &quot;Sum: &quot; &amp;lt;&amp;lt; sum &amp;lt;&amp;lt; std::endl; \/\/ Output: Sum: 30\n    return 0;\n}\n<\/pre><\/div>\n\n\n<p>In this example, <code>addNumbers(num1, num2)<\/code> calls the <code>addNumbers<\/code> function. The values of <code>num1<\/code> (10) and <code>num2<\/code> (20) are passed as arguments. The function returns their sum, which is then stored in the <code>sum<\/code> variable.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"function-parameters-and-arguments\">Function Parameters and Arguments<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><b>Parameters:<\/b> These are variables declared in the function definition. They act as placeholders for the values the function expects to receive.<\/li>\n\n\n\n<li><b>Arguments:<\/b> These are the actual values you pass to the function when you call it. The arguments are copied into the parameters.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example\">Example:<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nvoid greet(std::string name) { \/\/ &#039;name&#039; is a parameter\n    std::cout &amp;lt;&amp;lt; &quot;Hello, &quot; &amp;lt;&amp;lt; name &amp;lt;&amp;lt; &quot;!&quot; &amp;lt;&amp;lt; std::endl;\n}\n\nint main() {\n    greet(&quot;Alice&quot;); \/\/ &quot;Alice&quot; is an argument\n    greet(&quot;Bob&quot;);   \/\/ &quot;Bob&quot; is an argument\n    return 0;\n}\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"passing-arguments-to-functions\">Passing Arguments to Functions<\/h2>\n\n\n\n<p>C++ offers different ways to pass arguments to functions:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"pass-by-value\">Pass by Value:<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>This is the default method.<\/li>\n\n\n\n<li>The function receives a copy of the argument's value.<\/li>\n\n\n\n<li>Changes made to the parameter inside the function do not affect the original variable outside the function.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example-pass-by-value\">Example (Pass by Value):<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n#include &amp;lt;iostream&gt;\n\nvoid increment(int num) {\n    num = num + 1; \/\/ Changes only the copy\n    std::cout &amp;lt;&amp;lt; &quot;Inside function: &quot; &amp;lt;&amp;lt; num &amp;lt;&amp;lt; std::endl;\n}\n\nint main() {\n    int myNum = 5;\n    increment(myNum);\n    std::cout &amp;lt;&amp;lt; &quot;Outside function: &quot; &amp;lt;&amp;lt; myNum &amp;lt;&amp;lt; std::endl; \/\/ myNum is still 5\n    return 0;\n}\n<\/pre><\/div>\n\n\n<p>Output:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nInside function: 6\nOutside function: 5\n<\/pre><\/div>\n\n\n<p>Here, <code>myNum<\/code> remains 5 because <code>increment<\/code> works on a copy.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"pass-by-reference\">Pass by Reference:<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The function receives a reference (an alias) to the original variable.<\/li>\n\n\n\n<li>Changes made to the parameter inside the function directly affect the original variable.<\/li>\n\n\n\n<li>Use the <code>&amp;<\/code> operator to define a reference parameter.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example-pass-by-reference\">Example (Pass by Reference):<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n#include &amp;lt;iostream&gt;\n\nvoid incrementByRef(int &amp;amp;num) {\n    num = num + 1; \/\/ Changes the original variable\n    std::cout &amp;lt;&amp;lt; &quot;Inside function: &quot; &amp;lt;&amp;lt; num &amp;lt;&amp;lt; std::endl;\n}\n\nint main() {\n    int myNum = 5;\n    incrementByRef(myNum);\n    std::cout &amp;lt;&amp;lt; &quot;Outside function: &quot; &amp;lt;&amp;lt; myNum &amp;lt;&amp;lt; std::endl; \/\/ myNum is now 6\n    return 0;\n}\n<\/pre><\/div>\n\n\n<p>Output:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nInside function: 6\nOutside function: 6\n<\/pre><\/div>\n\n\n<p>Here, <code>myNum<\/code> becomes 6 because <code>incrementByRef<\/code> works on the original variable.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"pass-by-pointer\">Pass by Pointer:<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The function receives the memory address of the original variable.<\/li>\n\n\n\n<li>You use the dereference operator (<code>*<\/code>) to access the value at that address.<\/li>\n\n\n\n<li>Changes made through the pointer affect the original variable.<\/li>\n\n\n\n<li>Use the <code>*<\/code> operator to define a pointer parameter.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example-pass-by-pointer\">Example (Pass by Pointer):<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n#include &amp;lt;iostream&gt;\n\nvoid incrementByPtr(int *numPtr) {\n    (*numPtr) = (*numPtr) + 1; \/\/ Changes the value at the address\n    std::cout &amp;lt;&amp;lt; &quot;Inside function: &quot; &amp;lt;&amp;lt; *numPtr &amp;lt;&amp;lt; std::endl;\n}\n\nint main() {\n    int myNum = 5;\n    incrementByPtr(&amp;amp;myNum); \/\/ Pass the address of myNum\n    std::cout &amp;lt;&amp;lt; &quot;Outside function: &quot; &amp;lt;&amp;lt; myNum &amp;lt;&amp;lt; std::endl; \/\/ myNum is now 6\n    return 0;\n}\n<\/pre><\/div>\n\n\n<p>Output:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nInside function: 6\nOutside function: 6\n<\/pre><\/div>\n\n\n<p>Similar to pass by reference, pass by pointer modifies the original variable. Pass by reference is generally preferred for simpler syntax and safety unless you need pointer arithmetic or <code>nullptr<\/code> checks.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"void-functions\"><code>void<\/code> Functions<\/h2>\n\n\n\n<p>When a function does not return any value, you declare its return type as <code>void<\/code>. You do not use a <code>return<\/code> statement in a <code>void<\/code> function unless it's to exit the function early.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example-of-a-void-function\">Example of a <code>void<\/code> Function:<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n#include &amp;lt;iostream&gt;\n\nvoid printMessage() {\n    std::cout &amp;lt;&amp;lt; &quot;This is a message from a void function.&quot; &amp;lt;&amp;lt; std::endl;\n}\n\nint main() {\n    printMessage(); \/\/ Call the void function\n    return 0;\n}\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"function-overloading\">Function Overloading<\/h2>\n\n\n\n<p>Function overloading allows you to define multiple functions with the same name but different parameter lists. The compiler determines which function to call based on the arguments you provide.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"rules-for-function-overloading\">Rules for Function Overloading:<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><b>Different Number of Parameters:<\/b> Functions can have the same name but take a different number of arguments.<\/li>\n\n\n\n<li><b>Different Types of Parameters:<\/b> Functions can have the same name and number of arguments, but the types of arguments must differ.<\/li>\n\n\n\n<li><b>Order of Parameters:<\/b> The order of parameter types also matters for differentiation.<\/li>\n\n\n\n<li><b>Return Type Alone is Not Enough:<\/b> You cannot overload functions based only on their return type.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example-of-function-overloading\">Example of Function Overloading:<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n#include &amp;lt;iostream&gt;\n#include &amp;lt;string&gt;\n\n\/\/ Function to add two integers\nint add(int a, int b) {\n    return a + b;\n}\n\n\/\/ Function to add two doubles\ndouble add(double a, double b) {\n    return a + b;\n}\n\n\/\/ Function to concatenate two strings\nstd::string add(std::string s1, std::string s2) {\n    return s1 + s2;\n}\n\nint main() {\n    std::cout &amp;lt;&amp;lt; &quot;Sum of integers: &quot; &amp;lt;&amp;lt; add(5, 10) &amp;lt;&amp;lt; std::endl; \/\/ Calls int add(int, int)\n    std::cout &amp;lt;&amp;lt; &quot;Sum of doubles: &quot; &amp;lt;&amp;lt; add(5.5, 10.2) &amp;lt;&amp;lt; std::endl; \/\/ Calls double add(double, double)\n    std::cout &amp;lt;&amp;lt; &quot;Concatenated strings: &quot; &amp;lt;&amp;lt; add(&quot;Hello &quot;, &quot;World!&quot;) &amp;lt;&amp;lt; std::endl; \/\/ Calls std::string add(std::string, std::string)\n    return 0;\n}\n<\/pre><\/div>\n\n\n<p>In this example, the <code>add<\/code> function is overloaded to handle integers, doubles, and strings. The compiler automatically picks the correct version based on the argument types.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"default-arguments-in-functions\">Default Arguments in Functions<\/h2>\n\n\n\n<p>You can provide default values for function parameters. If you call the function without providing arguments for these parameters, the default values are used. If you provide arguments, the default values are overridden.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Default arguments must be placed at the end of the parameter list.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example-of-default-arguments\">Example of Default Arguments:<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n#include &amp;lt;iostream&gt;\n#include &amp;lt;string&gt;\n\nvoid displayMessage(std::string message = &quot;Default message.&quot;, int count = 1) {\n    for (int i = 0; i &amp;lt; count; ++i) {\n        std::cout &amp;lt;&amp;lt; message &amp;lt;&amp;lt; std::endl;\n    }\n}\n\nint main() {\n    displayMessage();               \/\/ Uses default values: &quot;Default message.&quot;, 1\n    displayMessage(&quot;Custom message.&quot;); \/\/ Uses &quot;Custom message.&quot;, default count (1)\n    displayMessage(&quot;Another message.&quot;, 3); \/\/ Uses &quot;Another message.&quot;, 3\n    return 0;\n}\n<\/pre><\/div>\n\n\n<p>Output:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nDefault message.\nCustom message.\nAnother message.\nAnother message.\nAnother message.\n<\/pre><\/div>\n\n\n<p>Here, <code>displayMessage()<\/code> has default values for both <code>message<\/code> and <code>count<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"inline-functions\">Inline Functions<\/h2>\n\n\n\n<p>An <b>inline function<\/b> is a hint to the compiler to insert the function's code directly at the point of each function call. This can reduce the overhead of function calls, potentially leading to faster execution.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use the <code>inline<\/code> keyword before the function's return type.<\/li>\n\n\n\n<li>The compiler decides whether to actually inline the function. It often does so for small, frequently called functions. Large functions are rarely inlined.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example-of-an-inline-function\">Example of an Inline Function:<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n#include &amp;lt;iostream&gt;\n\ninline int square(int x) {\n    return x * x;\n}\n\nint main() {\n    int num = 5;\n    int result = square(num); \/\/ Compiler might inline this call\n    std::cout &amp;lt;&amp;lt; &quot;Square of &quot; &amp;lt;&amp;lt; num &amp;lt;&amp;lt; &quot;: &quot; &amp;lt;&amp;lt; result &amp;lt;&amp;lt; std::endl;\n    return 0;\n}\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"recursion-in-functions\">Recursion in Functions<\/h2>\n\n\n\n<p>A <b>recursive function<\/b> is a function that calls itself. This technique is useful for problems that can be broken down into smaller, similar sub-problems. Every recursive function must have a base case to stop the recursion and prevent infinite loops.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example-of-a-recursive-function-factorial\">Example of a Recursive Function (Factorial):<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n#include &amp;lt;iostream&gt;\n\nlong long factorial(int n) {\n    if (n == 0) { \/\/ Base case: factorial of 0 is 1\n        return 1;\n    } else {\n        return n * factorial(n - 1); \/\/ Recursive call\n    }\n}\n\nint main() {\n    int num = 5;\n    std::cout &amp;lt;&amp;lt; &quot;Factorial of &quot; &amp;lt;&amp;lt; num &amp;lt;&amp;lt; &quot;: &quot; &amp;lt;&amp;lt; factorial(num) &amp;lt;&amp;lt; std::endl; \/\/ Output: 120\n    return 0;\n}\n<\/pre><\/div>\n\n\n<p>Here, <code>factorial(n)<\/code> calls <code>factorial(n-1)<\/code> until <code>n<\/code> becomes 0.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"function-pointers\">Function Pointers<\/h2>\n\n\n\n<p>A <b>function pointer<\/b> is a variable that stores the memory address of a function. You can use it to call the function indirectly. This is useful for implementing callbacks or creating generic algorithms.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"syntax-for-function-pointer-declaration\">Syntax for Function Pointer Declaration:<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nreturn_type (*pointer_name)(parameter_list);\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"example-of-function-pointers\">Example of Function Pointers:<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n#include &amp;lt;iostream&gt;\n\nint add(int a, int b) {\n    return a + b;\n}\n\nint subtract(int a, int b) {\n    return a - b;\n}\n\nint main() {\n    \/\/ Declare a function pointer that points to a function\n    \/\/ taking two ints and returning an int.\n    int (*operation)(int, int);\n\n    operation = add; \/\/ Assign the &#039;add&#039; function to the pointer\n    std::cout &amp;lt;&amp;lt; &quot;Addition result: &quot; &amp;lt;&amp;lt; operation(10, 5) &amp;lt;&amp;lt; std::endl; \/\/ Calls add(10, 5)\n\n    operation = subtract; \/\/ Assign the &#039;subtract&#039; function to the pointer\n    std::cout &amp;lt;&amp;lt; &quot;Subtraction result: &quot; &amp;lt;&amp;lt; operation(10, 5) &amp;lt;&amp;lt; std::endl; \/\/ Calls subtract(10, 5)\n    return 0;\n}\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"lambda-functions-c11-and-later\">Lambda Functions (C++11 and later)<\/h2>\n\n\n\n<p><b>Lambda functions<\/b> (or lambdas) are anonymous functions you can define and use inline, often passed as arguments to other functions. They are concise and powerful for short, specific tasks.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"syntax-for-lambda-functions\">Syntax for Lambda Functions:<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&#x5B;captures](parameters) -&gt; return_type { function_body }\n<\/pre><\/div>\n\n\n<ul class=\"wp-block-list\">\n<li><b><code>[captures]<\/code>:<\/b> This is the capture list. It specifies external variables the lambda can access.\n<ul class=\"wp-block-list\">\n<li><code>[]<\/code>: No variables captured.<\/li>\n\n\n\n<li><code>[var]<\/code>: Captures <code>var<\/code> by value.<\/li>\n\n\n\n<li><code>[&amp;var]<\/code>: Captures <code>var<\/code> by reference.<\/li>\n\n\n\n<li><code>[=]<\/code>: Captures all used variables by value.<\/li>\n\n\n\n<li><code>[&amp;]<\/code>: Captures all used variables by reference.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><b><code>(parameters)<\/code>:<\/b> The parameters the lambda takes.<\/li>\n\n\n\n<li><b><code>-&gt; return_type<\/code>:<\/b> The optional return type. If omitted, the compiler infers it.<\/li>\n\n\n\n<li><b><code>{ function_body }<\/code>:<\/b> The code the lambda executes.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example-of-lambda-functions\">Example of Lambda Functions:<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n#include &amp;lt;iostream&gt;\n#include &amp;lt;vector&gt;\n#include &amp;lt;algorithm&gt;\n\nint main() {\n    std::vector&amp;lt;int&gt; numbers = {1, 2, 3, 4, 5};\n    int factor = 10;\n\n    \/\/ Lambda to print numbers multiplied by &#039;factor&#039;\n    std::for_each(numbers.begin(), numbers.end(), &#x5B;factor](int n) {\n        std::cout &amp;lt;&amp;lt; n * factor &amp;lt;&amp;lt; &quot; &quot;;\n    });\n    std::cout &amp;lt;&amp;lt; std::endl;\n\n    \/\/ Lambda to add two numbers\n    auto sum = &#x5B;](int a, int b) { return a + b; };\n    std::cout &amp;lt;&amp;lt; &quot;Sum: &quot; &amp;lt;&amp;lt; sum(20, 30) &amp;lt;&amp;lt; std::endl;\n    return 0;\n}\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"best-practices-for-c-functions\">Best Practices for C++ Functions<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><b>Single Responsibility Principle:<\/b> Each function should do one thing and do it well.<\/li>\n\n\n\n<li><b>Clear Naming:<\/b> Use descriptive names for functions and parameters (e.g., <code>calculateAverage<\/code>, <code>isValidInput<\/code>).<\/li>\n\n\n\n<li><b>Keep Functions Small:<\/b> Short functions are easier to read, test, and debug.<\/li>\n\n\n\n<li><b>Avoid Global Variables:<\/b> Pass data as parameters or return values instead of relying on global variables. This makes functions more independent and easier to reuse.<\/li>\n\n\n\n<li><b>Add Comments:<\/b> Explain what complex functions do, their purpose, and their parameters.<\/li>\n\n\n\n<li><b>Use <code>const<\/code> for Read-Only Parameters:<\/b> If a function receives a parameter that it should not modify, declare it <code>const<\/code> (e.g., <code>void printData(const std::vector&lt;int&gt;&amp; data)<\/code>). This improves safety and clarifies intent.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>A C++ function is a block of code that performs a specific task. It helps you organize your code, make it reusable, and simplify complex programs. Functions let you build modular and efficient C++ applications. This guide will help you master C++ functions in a few steps.<\/p>\n","protected":false},"author":41,"featured_media":108764,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"set","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[25860],"tags":[36801],"content_type":[],"class_list":["post-31053","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-software","tag-c-programming"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.3 (Yoast SEO v27.3) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>C++ Functions: Definition, Types, and Best Practices<\/title>\n<meta name=\"description\" content=\"Learn C++ functions: definition, types, syntax, and best practices to enhance your programming skills.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.mygreatlearning.com\/blog\/function-in-c\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"C++ Functions\" \/>\n<meta property=\"og:description\" content=\"Learn C++ functions: definition, types, syntax, and best practices to enhance your programming skills.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.mygreatlearning.com\/blog\/function-in-c\/\" \/>\n<meta property=\"og:site_name\" content=\"Great Learning Blog: Free Resources what Matters to shape your Career!\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/GreatLearningOfficial\/\" \/>\n<meta property=\"article:published_time\" content=\"2021-04-16T06:23:47+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-06-20T14:16:22+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/cpp-functions.webp\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"619\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/webp\" \/>\n<meta name=\"author\" content=\"Great Learning Editorial Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/Great_Learning\" \/>\n<meta name=\"twitter:site\" content=\"@Great_Learning\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Great Learning Editorial Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/function-in-c\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/function-in-c\\\/\"},\"author\":{\"name\":\"Great Learning Editorial Team\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/person\\\/6f993d1be4c584a335951e836f2656ad\"},\"headline\":\"C++ Functions\",\"datePublished\":\"2021-04-16T06:23:47+00:00\",\"dateModified\":\"2025-06-20T14:16:22+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/function-in-c\\\/\"},\"wordCount\":1362,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/function-in-c\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2021\\\/04\\\/cpp-functions.webp\",\"keywords\":[\"C++ Programming\"],\"articleSection\":[\"IT\\\/Software Development\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/function-in-c\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/function-in-c\\\/\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/function-in-c\\\/\",\"name\":\"C++ Functions: Definition, Types, and Best Practices\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/function-in-c\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/function-in-c\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2021\\\/04\\\/cpp-functions.webp\",\"datePublished\":\"2021-04-16T06:23:47+00:00\",\"dateModified\":\"2025-06-20T14:16:22+00:00\",\"description\":\"Learn C++ functions: definition, types, syntax, and best practices to enhance your programming skills.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/function-in-c\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/function-in-c\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/function-in-c\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2021\\\/04\\\/cpp-functions.webp\",\"contentUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2021\\\/04\\\/cpp-functions.webp\",\"width\":1200,\"height\":619},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/function-in-c\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Blog\",\"item\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"IT\\\/Software Development\",\"item\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/software\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"C++ Functions\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/\",\"name\":\"Great Learning Blog\",\"description\":\"Learn, Upskill &amp; Career Development Guide and Resources\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#organization\"},\"alternateName\":\"Great Learning\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#organization\",\"name\":\"Great Learning\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/GL-Logo.jpg\",\"contentUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/GL-Logo.jpg\",\"width\":900,\"height\":900,\"caption\":\"Great Learning\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/GreatLearningOfficial\\\/\",\"https:\\\/\\\/x.com\\\/Great_Learning\",\"https:\\\/\\\/www.instagram.com\\\/greatlearningofficial\\\/\",\"https:\\\/\\\/www.linkedin.com\\\/school\\\/great-learning\\\/\",\"https:\\\/\\\/in.pinterest.com\\\/greatlearning12\\\/\",\"https:\\\/\\\/www.youtube.com\\\/user\\\/beaconelearning\\\/\"],\"description\":\"Great Learning is a leading global ed-tech company for professional training and higher education. It offers comprehensive, industry-relevant, hands-on learning programs across various business, technology, and interdisciplinary domains driving the digital economy. These programs are developed and offered in collaboration with the world's foremost academic institutions.\",\"email\":\"info@mygreatlearning.com\",\"legalName\":\"Great Learning Education Services Pvt. Ltd\",\"foundingDate\":\"2013-11-29\",\"numberOfEmployees\":{\"@type\":\"QuantitativeValue\",\"minValue\":\"1001\",\"maxValue\":\"5000\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/person\\\/6f993d1be4c584a335951e836f2656ad\",\"name\":\"Great Learning Editorial Team\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/unnamed.webp\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/unnamed.webp\",\"contentUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/unnamed.webp\",\"caption\":\"Great Learning Editorial Team\"},\"description\":\"The Great Learning Editorial Staff includes a dynamic team of subject matter experts, instructors, and education professionals who combine their deep industry knowledge with innovative teaching methods. Their mission is to provide learners with the skills and insights needed to excel in their careers, whether through upskilling, reskilling, or transitioning into new fields.\",\"sameAs\":[\"https:\\\/\\\/www.mygreatlearning.com\\\/\",\"https:\\\/\\\/in.linkedin.com\\\/school\\\/great-learning\\\/\",\"https:\\\/\\\/x.com\\\/https:\\\/\\\/twitter.com\\\/Great_Learning\",\"https:\\\/\\\/www.youtube.com\\\/channel\\\/UCObs0kLIrDjX2LLSybqNaEA\"],\"award\":[\"Best EdTech Company of the Year 2024\",\"Education Economictimes Outstanding Education\\\/Edtech Solution Provider of the Year 2024\",\"Leading E-learning Platform 2024\"],\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/author\\\/greatlearning\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"C++ Functions: Definition, Types, and Best Practices","description":"Learn C++ functions: definition, types, syntax, and best practices to enhance your programming skills.","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:\/\/www.mygreatlearning.com\/blog\/function-in-c\/","og_locale":"en_US","og_type":"article","og_title":"C++ Functions","og_description":"Learn C++ functions: definition, types, syntax, and best practices to enhance your programming skills.","og_url":"https:\/\/www.mygreatlearning.com\/blog\/function-in-c\/","og_site_name":"Great Learning Blog: Free Resources what Matters to shape your Career!","article_publisher":"https:\/\/www.facebook.com\/GreatLearningOfficial\/","article_published_time":"2021-04-16T06:23:47+00:00","article_modified_time":"2025-06-20T14:16:22+00:00","og_image":[{"width":1200,"height":619,"url":"http:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/cpp-functions.webp","type":"image\/webp"}],"author":"Great Learning Editorial Team","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/Great_Learning","twitter_site":"@Great_Learning","twitter_misc":{"Written by":"Great Learning Editorial Team","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.mygreatlearning.com\/blog\/function-in-c\/#article","isPartOf":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/function-in-c\/"},"author":{"name":"Great Learning Editorial Team","@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/person\/6f993d1be4c584a335951e836f2656ad"},"headline":"C++ Functions","datePublished":"2021-04-16T06:23:47+00:00","dateModified":"2025-06-20T14:16:22+00:00","mainEntityOfPage":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/function-in-c\/"},"wordCount":1362,"commentCount":0,"publisher":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#organization"},"image":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/function-in-c\/#primaryimage"},"thumbnailUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/cpp-functions.webp","keywords":["C++ Programming"],"articleSection":["IT\/Software Development"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.mygreatlearning.com\/blog\/function-in-c\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.mygreatlearning.com\/blog\/function-in-c\/","url":"https:\/\/www.mygreatlearning.com\/blog\/function-in-c\/","name":"C++ Functions: Definition, Types, and Best Practices","isPartOf":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/function-in-c\/#primaryimage"},"image":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/function-in-c\/#primaryimage"},"thumbnailUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/cpp-functions.webp","datePublished":"2021-04-16T06:23:47+00:00","dateModified":"2025-06-20T14:16:22+00:00","description":"Learn C++ functions: definition, types, syntax, and best practices to enhance your programming skills.","breadcrumb":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/function-in-c\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.mygreatlearning.com\/blog\/function-in-c\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mygreatlearning.com\/blog\/function-in-c\/#primaryimage","url":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/cpp-functions.webp","contentUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/cpp-functions.webp","width":1200,"height":619},{"@type":"BreadcrumbList","@id":"https:\/\/www.mygreatlearning.com\/blog\/function-in-c\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog","item":"https:\/\/www.mygreatlearning.com\/blog\/"},{"@type":"ListItem","position":2,"name":"IT\/Software Development","item":"https:\/\/www.mygreatlearning.com\/blog\/software\/"},{"@type":"ListItem","position":3,"name":"C++ Functions"}]},{"@type":"WebSite","@id":"https:\/\/www.mygreatlearning.com\/blog\/#website","url":"https:\/\/www.mygreatlearning.com\/blog\/","name":"Great Learning Blog","description":"Learn, Upskill &amp; Career Development Guide and Resources","publisher":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#organization"},"alternateName":"Great Learning","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.mygreatlearning.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.mygreatlearning.com\/blog\/#organization","name":"Great Learning","url":"https:\/\/www.mygreatlearning.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/06\/GL-Logo.jpg","contentUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/06\/GL-Logo.jpg","width":900,"height":900,"caption":"Great Learning"},"image":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/GreatLearningOfficial\/","https:\/\/x.com\/Great_Learning","https:\/\/www.instagram.com\/greatlearningofficial\/","https:\/\/www.linkedin.com\/school\/great-learning\/","https:\/\/in.pinterest.com\/greatlearning12\/","https:\/\/www.youtube.com\/user\/beaconelearning\/"],"description":"Great Learning is a leading global ed-tech company for professional training and higher education. It offers comprehensive, industry-relevant, hands-on learning programs across various business, technology, and interdisciplinary domains driving the digital economy. These programs are developed and offered in collaboration with the world's foremost academic institutions.","email":"info@mygreatlearning.com","legalName":"Great Learning Education Services Pvt. Ltd","foundingDate":"2013-11-29","numberOfEmployees":{"@type":"QuantitativeValue","minValue":"1001","maxValue":"5000"}},{"@type":"Person","@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/person\/6f993d1be4c584a335951e836f2656ad","name":"Great Learning Editorial Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/02\/unnamed.webp","url":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/02\/unnamed.webp","contentUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/02\/unnamed.webp","caption":"Great Learning Editorial Team"},"description":"The Great Learning Editorial Staff includes a dynamic team of subject matter experts, instructors, and education professionals who combine their deep industry knowledge with innovative teaching methods. Their mission is to provide learners with the skills and insights needed to excel in their careers, whether through upskilling, reskilling, or transitioning into new fields.","sameAs":["https:\/\/www.mygreatlearning.com\/","https:\/\/in.linkedin.com\/school\/great-learning\/","https:\/\/x.com\/https:\/\/twitter.com\/Great_Learning","https:\/\/www.youtube.com\/channel\/UCObs0kLIrDjX2LLSybqNaEA"],"award":["Best EdTech Company of the Year 2024","Education Economictimes Outstanding Education\/Edtech Solution Provider of the Year 2024","Leading E-learning Platform 2024"],"url":"https:\/\/www.mygreatlearning.com\/blog\/author\/greatlearning\/"}]}},"uagb_featured_image_src":{"full":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/cpp-functions.webp",1200,619,false],"thumbnail":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/cpp-functions-150x150.webp",150,150,true],"medium":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/cpp-functions-300x155.webp",300,155,true],"medium_large":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/cpp-functions-768x396.webp",768,396,true],"large":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/cpp-functions-1024x528.webp",1024,528,true],"1536x1536":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/cpp-functions.webp",1200,619,false],"2048x2048":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/cpp-functions.webp",1200,619,false],"web-stories-poster-portrait":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/cpp-functions-640x619.webp",640,619,true],"web-stories-publisher-logo":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/cpp-functions-96x96.webp",96,96,true],"web-stories-thumbnail":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/cpp-functions-150x77.webp",150,77,true]},"uagb_author_info":{"display_name":"Great Learning Editorial Team","author_link":"https:\/\/www.mygreatlearning.com\/blog\/author\/greatlearning\/"},"uagb_comment_info":0,"uagb_excerpt":"A C++ function is a block of code that performs a specific task. It helps you organize your code, make it reusable, and simplify complex programs. Functions let you build modular and efficient C++ applications. This guide will help you master C++ functions in a few steps.","_links":{"self":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts\/31053","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/users\/41"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/comments?post=31053"}],"version-history":[{"count":9,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts\/31053\/revisions"}],"predecessor-version":[{"id":110705,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts\/31053\/revisions\/110705"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/media\/108764"}],"wp:attachment":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/media?parent=31053"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/categories?post=31053"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/tags?post=31053"},{"taxonomy":"content_type","embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/content_type?post=31053"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}