PageSourceSearch

https://c-cpp-notes.vercel.app/assets/js/12313610.cc6811d4.js

js c-cpp-notes.vercel.app collected 2026-10-03 06:19:53 UTC 29,821 bytes, 1 lines download raw bytes

1"use strict";(self.webpackChunkc_c_notes=self.webpackChunkc_c_notes||[]).push([[2102],{2522:(e,t,n)=>{n.d(t,{A:()=>a});const a="data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiIHN0YW5kYWxvbmU9Im5vIj8+CjxzdmcKICAgeG1sbnM6ZGM9Imh0dHA6Ly9wdXJsLm9yZy9kYy9lbGVtZW50cy8xLjEvIgogICB4bWxuczpjYz0iaHR0cDovL2NyZWF0aXZlY29tbW9ucy5vcmcvbnMjIgogICB4bWxuczpyZGY9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkvMDIvMjItcmRmLXN5bnRheC1ucyMiCiAgIHhtbG5zOnN2Zz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciCiAgIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIKICAgeG1sbnM6c29kaXBvZGk9Imh0dHA6Ly9zb2RpcG9kaS5zb3VyY2Vmb3JnZS5uZXQvRFREL3NvZGlwb2RpLTAuZHRkIgogICB4bWxuczppbmtzY2FwZT0iaHR0cDovL3d3dy5pbmtzY2FwZS5vcmcvbmFtZXNwYWNlcy9pbmtzY2FwZSIKICAgeG1sOnNwYWNlPSJwcmVzZXJ2ZSIKICAgc3R5bGU9ImVuYWJsZS1iYWNrZ3JvdW5kOm5ldyAwIDAgMTY1OS40IDYzMC43OyIKICAgdmlld0JveD0iMCAwIDE2NTkuNCA2MzAuNyIKICAgeT0iMHB4IgogICB4PSIwcHgiCiAgIHNvZGlwb2RpOmRvY25hbWU9ImdlYXJzLXN5c3RlbS5zdmciCiAgIGlua3NjYXBlOnZlcnNpb249IjEuMCAoNDAzNWE0ZmI0OSwgMjAyMC0wNS0wMSkiCiAgIGlkPSJMaX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{n.d(t,{xA:()=>c,yg:()=>d});var a=n(6540);function i(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function o(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var a=Object.getOwnPropertySymbols(e);t&&(a=a.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,a)}return n}function r(e){for(var t=1;t<arguments.length;t++){var n=null!=arguments[t]?arguments[t]:{};t%2?o(Object(n),!0).forEach((function(t){i(e,t,n[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(n)):o(Object(n)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(n,t))}))}return e}function s(e,t){if(null==e)return{};var n,a,i=function(e,t){if(null==e)return{};var n,a,i={},o=Object.keys(e);for(a=0;a<o.length;a++)n=o[a],t.indexOf(n)>=0||(i[n]=e[n]);return i}(e,t);if(Object.getOwnPropertySymbols){var o=Object.getOwnPropertySymbols(e);for(a=0;a<o.length;a++)n=o[a],t.indexOf(n)>=0||Object.prototype.propertyIsEnumerable.call(e,n)&&(i[n]=e[n])}return i}var u=a.createContext({}),l=function(e){var t=a.useContext(u),n=t;return e&&(n="function"==typeof e?e(t):r(r({},t),e)),n},c=function(e){var t=l(e.components);return a.createElement(u.Provider,{value:t},e.children)},y="mdxType",g={inlineCode:"code",wrapper:function(e){var t=e.children;return a.createElement(a.Fragment,{},t)}},p=a.forwardRef((function(e,t){var n=e.components,i=e.mdxType,o=e.originalType,u=e.parentName,c=s(e,["components","mdxType","originalType","parentName"]),y=l(n),p=i,d=y["".concat(u,".").concat(p)]||y[p]||g[p]||o;return n?a.createElement(d,r(r({ref:t},c),{},{components:n}
1)):a.createElement(d,r({ref:t},c))}));function d(e,t){var n=arguments,i=t&&t.mdxType;if("string"==typeof e||i){var o=n.length,r=new Array(o);r[0]=p;var s={};for(var u in t)hasOwnProperty.call(t,u)&&(s[u]=t[u]);s.originalType=e,s[y]="string"==typeof e?e:i,r[1]=s;for(var l=2;l<o;l++)r[l]=n[l];return a.createElement.apply(null,r)}return a.createElement.apply(null,n)}p.displayName="MDXCreateElement"},7095:(e,t,n)=>{n.r(t),n.d(t,{assets:()=>u,contentTitle:()=>r,default:()=>g,frontMatter:()=>o,metadata:()=>s,toc:()=>l});var a=n(8168),i=(n(6540),n(5680));const o={sidebar_position:50,id:"functions",title:"Functions",hide_title:!1,hide_table_of_contents:!1,sidebar_label:"Functions",description:"What are functions and how to use them.",slug:"/cpp/functions/",custom_edit_url:null},r=void 0,s={unversionedId:"cpp/Functions/functions",id:"cpp/Functions/functions",title:"Functions",description:"What are functions and how to use them.",source:"@site/docs/cpp/Functions/functions.md",sourceDirName:"cpp/Functions",slug:"/cpp/functions/",permalink:"/docs/cpp/functions/",draft:!1,editUrl:null,tags:[],version:"current",lastUpdatedAt:1747598548,formattedLastUpdatedAt:"May 18, 2025",sidebarPosition:50,frontMatter:{sidebar_position:50,id:"functions",title:"Functions",hide_title:!1,hide_table_of_contents:!1,sidebar_label:"Functions",description:"What are functions and how to use them.",slug:"/cpp/functions/",custom_edit_url:null},sidebar:"tutorialSidebar",previous:{title:"EXERCISES",permalink:"/docs/cpp/exercises_2"},next:{title:"Passage of values",permalink:"/docs/cpp/passage-of-values"}},u={},l=[{value:"Definition",id:"definition",level:2},{value:"Why use functions",id:"why-use-functions",level:2},{value:"Where to put them",id:"where-to-put-them",level:2},{value:"Parts of a function",id:"parts-of-a-function",level:2},{value:"Analysis",id:"analysis",level:3},{value:"Function types",id:"function-types",level:2},{value:"Classic functions",id:"classic-functions",level:3},{value:"Procedures",id:"procedures",level:3},{value:"Function call (invocation)",id:"function-call-invocation",level:2},{value:"Passage of values",id:"passage-of-values",level:3}],c={toc:l},y="wrapper";function g(e){let{components:t,...o}=e;return(0,i.yg)(y,(0,a.A)({},c,o,{components:t,mdxType:"MDXLayout"}),(0,i.yg)("h2",{id:"definition"},"Definition"),(0,i.yg)("p",null,"A function is a reusable sequence of statements designed to do a particular job."),(0,i.yg)("p",null,"In more general terms, a function is a system that takes an input and returns it as output\nafter doing some calculations inside to perform a specific task."),(0,i.yg)("p",null,(0,i.yg)("img",{alt:"Variable declaration",src:n(2522).A,width:"1659",height:"631"})),(0,i.yg)("figcaption",null,"Fig.1. Abstract representation of a system."),(0,i.yg)("p",null,"If you have a mathematical background, you may be led to associate the functioning\nof mathematical functions with that of programming functions. Although this association is\npartly correct (inputs \u2192 calculations \u2192 output), it is confusing to consider them the same\nthing so we will keep them as separate concepts."),(0,i.yg)("p",null,"We have already used functions: ",(0,i.yg)("inlineCode",{parentName:"p"},"main()")," is a special function that every executable\nprogram must have, and it is present in every one of our programs done so far.\nAlso writing ",(0,i.yg)("inlineCode",{parentName:"p"},"<iostream>")," means having to deal with functions (inside that library):\nalthough it doesn't look like it, every time you ",(0,i.yg)("inlineCode",{parentName:"p"},"<<")," or ",(0,i.yg)("inlineCode",{parentName:"p"},">>")," operator to do\ninput or output, you're using a function provided by the standard library."),(0,i.yg)("p",null,"The ",(0,i.yg)("a",{parentName:"p",href:"https://en.cppreference.com/w/cpp/header"},"C++ standard library")," comes with plenty\nof already-written functions ready to use. But this doesn't mean that we can't write ours!\nFunctions that you write yourself are called ",(0,i.yg)("strong",{parentName:"p"},(0,i.yg)("em",{parentName:"strong"},"user-defined functions")),"."),(0,i.yg)("p",null,"You can pass data, known as parameters, into a function as inputs, but it is not always\nnecessary."),(0,i.yg)("h2",{id:"why-use-functions"},"Why use functions"),(0,i.yg)("p",null,"There are two benefits that come when using functions:"),(0,i.yg)("ol",null,(0,i.yg)("li",{parentName:"ol"},(0,i.yg)("strong",{parentName:"li"},"Reusability"),": once some code is written inside a function, it can then be executed\nas many times as you want. This avoids repetition, redundancy and duplicate code.\nThe principle is similar to loops, but it works in a bit different way."),(0,i.yg)("li",{parentName:"ol"},(0,i.yg)("strong",{parentName:"li"},"Organization"),": as programs start to get longer and longer, putting all the code\ninside the main function becomes increasingly hard to manage: with functions, we can\nsplit our programs into small, modular chunks that are easier to organize, test, use and\nreuse.")),(0,i.yg)("h2",{id:"where-to-put-them"},"Where to put them"),(0,i.yg)("p",null,"Functions need to be written ",(0,i.yg)("strong",{parentName:"p"},(0,i.yg)("em",{parentName:"strong"},"before"))," they are used with an invocation. Therefore, they\nusually stay at the top of the program, before ",(0,i.yg)("inlineCode",{parentName:"p"},"main()")," and after all the ",(0,i.yg)("em",{parentName:"p"},"includes")," and\n",(0,i.yg)("em",{parentName:"p"},"namespaces"),"."),(0,i.yg)("p",null,"Remember that since ",(0,i.yg)("inlineCode",{parentName:"p"},"main()")," is also a function, every other function has to be written\n",(0,i.yg)("strong",{parentName:"p"},"outside")," of it, because, unlike some other programming languages, ",(0,i.yg)("strong",{parentName:"p"},"C/C++ does not support\nnested functions")," (functions inside functions)."),(0,i.yg)("h2",{id:"parts-of-a-function"},"Parts of a function"),(0,i.yg)("p",null,"A function is made up of a signature "),(0,i.yg)("p",null,"To understand how a function is created let'
1s see the generic form:"),(0,i.yg)("pre",null,(0,i.yg)("code",{parentName:"pre",className:"language-cpp"},"// do not compile this!\nreturnType functionName(type parameter1, type parameter2, ...) {\n    instructions;\n    return ...;\n}\n")),(0,i.yg)("p",null,"For example, a simple function that returns the sum of two variables can be written like this:"),(0,i.yg)("pre",null,(0,i.yg)("code",{parentName:"pre",className:"language-cpp"},"int add(int a, int b) {\n    int sum = a+b;\n    return sum;\n}\n")),(0,i.yg)("admonition",{type:"note"},(0,i.yg)("p",{parentName:"admonition"},"Of course, this is not a useful function, but at least it's simple to understand and\nit will help you to grasp the concept of how a function works more easily through the\nfollowing explanations.")),(0,i.yg)("h3",{id:"analysis"},"Analysis"),(0,i.yg)("p",null,"Now let's analyze every part that makes up a function like the one above:"),(0,i.yg)("p",null,(0,i.yg)("img",{alt:"Parts of a function",src:n(8406).A,width:"1659",height:"693"})),(0,i.yg)("figcaption",null,"Fig.2. The various components of a function."),(0,i.yg)("ul",null,(0,i.yg)("li",{parentName:"ul"},(0,i.yg)("strong",{parentName:"li"},"Return type"),": this is the primitive type of the output value that the function will\nreturn."),(0,i.yg)("li",{parentName:"ul"},(0,i.yg)("strong",{parentName:"li"},"Function name"),": as variables, functions require a name in order to be\ndeclared, too. This name is an identifier and it's also called ",(0,i.yg)("em",{parentName:"li"},"header"),". It will be used every\ntime we need to call (refer to) that function, and it identifies it uniquely."),(0,i.yg)("li",{parentName:"ul"},(0,i.yg)("strong",{parentName:"li"},"Parameters"),": they are local variables that take ",(0,i.yg)("em",{parentName:"li"},"arguments")," (values of other\nvariables from outside) in input when the function is called. You work with them inside\nthe body of a function. Parameters can be one, more than one but also zero. In the latter\ncase, we can put the keyword ",(0,i.yg)("inlineCode",{parentName:"li"},"void")," inside the parentheses or simply open and close a pair of\nround parentheses (most recommended) like in ",(0,i.yg)("inlineCode",{parentName:"li"},"main()"),". It's important to remember that you always\nhave to specify the type of ",(0,i.yg)("em",{parentName:"li"},"each")," of the ",(0,i.yg)("strong",{parentName:"li"},(0,i.yg)("em",{parentName:"strong"},"formal parameters"))," (this is how they are called)\ninside a function, but not when you call it (see ",(0,i.yg)("a",{parentName:"li",href:"#function-call-invocation"},"function call"),")."),(0,i.yg)("li",{parentName:"ul"},(0,i.yg)("strong",{parentName:"li"},"Header"),": it's the whole first line of a function and it encloses all the three components\nmentioned above: ",(0,i.yg)("em",{parentName:"li"},"return type"),", ",(0,i.yg)("em",{parentName:"li"},"name")," and ",(0,i.yg)("em",{parentName:"li"},"parameters"),"."),(0,i.yg)("li",{parentName:"ul"},(0,i.yg)("strong",{parentName:"li"},"Body"),": it's the block of statements required to perform a specific task for which\nthat function is built. The instructions are always enclosed in the function's curly\nbrackets ",(0,i.yg)("inlineCode",{parentName:"li"},"{...}"),"."),(0,i.yg)("li",{parentName:"ul"},(0,i.yg)("strong",{parentName:"li"},"Return"),": the ",(0,i.yg)("inlineCode",{parentName:"li"},"return")," instruction always terminates the execution of a function, so\nit must be typed at the end. It can be only 1 for each function, and it's used to ",(0,i.yg)("em",{parentName:"li"},"return"),"\n(provide/give back in output, but ",(0,i.yg)("em",{parentName:"li"},(0,i.yg)("u",null,"not"))," as a text on the console like with ",(0,i.yg)("inlineCode",{parentName:"li"},"cout"),"!)\nthe result of the computation performed in the function itself. What follows the ",(0,i.yg)("em",{parentName:"li"},"return"),"\nkeyword (it can be a value like ",(0,i.yg)("inlineCode",{parentName:"li"},"0"),", a variable like ",(0,i.yg)("inlineCode",{parentName:"li"},"sum")," or an expression like ",(0,i.yg)("inlineCode",{parentName:"li"},"a+b"),") is\nwhat the function gives in output when invocated and executed, and this needs to be stored\nsomewhere if the function is not of type ",(0,i.yg)("inlineCode",{parentName:"li"},"void")," (see ",(0,i.yg)("em",{parentName:"li"},"procedures")," for further explanation).\nWe have already used ",(0,i.yg)("inlineCode",{parentName:"li"},"return")," in the ",(0,i.yg)("inlineCode",{parentName:"li"},"main()")," function.")),(0,i.yg)("admonition",{title:"Difference (Parameter \ud83c\udd9a Argument)",type:"info"},(0,i.yg)("p",{parentName:"admonition"},"When a function is called, the ",(0,i.yg)("em",{parentName:"p"},"actual")," values that are passed during the call are called\n",(0,i.yg)("strong",{parentName:"p"},"arguments"),". So the arguments are the data you externally pass into the function's\nparameters."),(0,i.yg)("p",{parentName:"admonition"},"The values that defined at the time of the function prototype or definition\nof the function are called ",(0,i.yg)("strong",{parentName:"p"},"parameters"),", more precisely ",(0,i.yg)("em",{parentName:"p"},"formal parameters"),". Formal\nparameters are local variables to which the values of the arguments are assigned\nwhen the function is called",(0,i.yg)("sup",{parentName:"p",id:"fnref-1"},(0,i.yg)("a",{parentName:"sup",href:"#fn-1",className:"footnote-ref"},"1")),"."),(0,i.yg)("p",{parentName:"admonition"},"We dig deeper into this difference and learn why it's fundamental in\n",(0,i.yg)("a",{parentName:"p",href:"https://c-cpp-notes.vercel.app/docs/cpp/passage-of-values"},"this lesson"),".")),(0,i.yg)("admonition",{title:"Terminology",type:"note"},(0,i.yg)("p",{parentName:"admonition"},'At the top, I said that the first line of a function is called "header". If you remember,\nthough, "',(0,i.yg)("em",{parentName:"p"},(0,i.yg)("a",{parentName:"em",href:"https://www.geeksforgeeks.org/header-files-in-c-cpp-and-its-uses/"},"header")),'"\nis also the term used with the ',(0,i.yg)("em",{parentName:"p"},"header files")," that are included in a program when we\nwrite the ",(0,i.yg)("em",{parentName:"p"},"preprocessor directive")," ",(0,i.yg)("inlineCode",{parentName:"p"},"#include <...>"),", and it's a file that contains a set\nof predefined standard library functions."),(0,i.yg)("p",{parentName:"admonition"},'Therefore, even if among C/C++ developers the first line of a function is often called\n"header", this is actually a wrong term for the aforementioned reason. A better one could\nbe ',(0,i.yg)("em",{parentName:"p"},"signature"),", but the best are ",(0,i.yg)("strong",{parentName:"p"},(0,i.yg)("em",{parentName:"strong"},"function declaration"))," and ",(0,i.yg)("strong",{parentName:"p"},(0,i.yg)("em",{parentName:"strong"},"prototype"))," (this last\nterm, though, is used in a different case that we will c
1over later in another lesson).\nSee this ",(0,i.yg)("a",{parentName:"p",href:"https://stackoverflow.com/questions/43177284/c-terminology-header-vs-signature"},"Stack Overflow Q&A"),"\nfor more info.")),(0,i.yg)("h2",{id:"function-types"},"Function types"),(0,i.yg)("p",null,"Before creating a function, we need to know about the two types that exist in C++:"),(0,i.yg)("p",null,(0,i.yg)("strong",{parentName:"p"},"1.")," Classic functions ",(0,i.yg)("br",null),"\n",(0,i.yg)("strong",{parentName:"p"},"2.")," Procedures"),(0,i.yg)("p",null,"They can both (optionally) take inputs, but for the outputs the situation is different."),(0,i.yg)("h3",{id:"classic-functions"},"Classic functions"),(0,i.yg)("p",null,"Classic functions are just like the one we saw earlier:"),(0,i.yg)("pre",null,(0,i.yg)("code",{parentName:"pre",className:"language-cpp"},"int add(int a, int b) {\n    int sum = a+b;\n    return sum;\n}\n")),(0,i.yg)("p",null,"They always start with the return type (any primitive type: ",(0,i.yg)("inlineCode",{parentName:"p"},"int"),", ",(0,i.yg)("inlineCode",{parentName:"p"},"bool"),", ",(0,i.yg)("inlineCode",{parentName:"p"},"float"),"\u2026) and\nwhen they are invoked they return a value that has to be stored somewhere, like a variable,\nto work with it."),(0,i.yg)("h3",{id:"procedures"},"Procedures"),(0,i.yg)("p",null,"A procedure is a particular type of function that performs a task but ",(0,i.yg)("strong",{parentName:"p"},"does not produce\ninformation")," as outputs. In other words, they cannot return any numeric value, but\nthey can, for example, print some text on the screen or make calculations."),(0,i.yg)("pre",null,(0,i.yg)("code",{parentName:"pre",className:"language-cpp"},'void add(int a, int b) {\n    int sum = a+b;\n    cout << "The sum is: " << sum << endl;\n    return; // this line is optional\n}\n')),(0,i.yg)("p",null,"Notice the ",(0,i.yg)("inlineCode",{parentName:"p"},"void")," type keyword. It means no return value. If you write ",(0,i.yg)("inlineCode",{parentName:"p"},"return 5")," inside\na procedure, the compiler will throw out an error."),(0,i.yg)("h2",{id:"function-call-invocation"},"Function call (invocation)"),(0,i.yg)("p",null,"Invoking a function means telling the compiler to execute that function. The CPU does\nthat and then, when the function ends, it returns to the place where it has been invoked to\nresume execution. You put the function call whenever you need it, but needless to say, you\ncan also never invoke it."),(0,i.yg)("p",null,"Before invoking a function, we need to know its type."),(0,i.yg)("p",null,"If it's a ",(0,i.yg)("strong",{parentName:"p"},"classic function"),", the invocation is made by storing the return value of that\nfunction inside a variable:"),(0,i.yg)("admonition",{type:"caution"},(0,i.yg)("p",{parentName:"admonition"},"I inverted the main function and user-defined functions for clarity reasons. If you want to use\nthe function correctly in a program you'll have to put it before the main function. There's\nactually another method called \"",(0,i.yg)("em",{parentName:"p"},"forward declaration"),"\" to avoid this and put functions at the\nend of the program, but I'll cover it in a separate lesson.")),(0,i.yg)("pre",null,(0,i.yg)("code",{parentName:"pre",className:"language-cpp",metastring:"{3}","{3}":!0},"// inside main function\nint x=5, y=2;\nint result = add(x,y) // output: 7\n\n// classic function (outside main)\nint add(int a, int b) { // a and b becomes 5 and 2\n    int sum = a+b;\n    return sum;\n}\n")),(0,i.yg)("p",null,"If it's a ",(0,i.yg)("strong",{parentName:"p"},"procedure"),", instead, we don't need to store any value, so it's simply:"),(0,i.yg)("pre",null,(0,i.yg)("code",{parentName:"pre",className:"language-cpp",metastring:"{3}","{3}":!0},"// inside main function\nint x=5, y=2;\nadd(x,y) // output: 7\n\n// procedure (outside main)\nvoid add(int a, int b) { // a and b becomes 5 and 2\n    cout << a+b << endl;\n    return;\n}\n")),(0,i.yg)("p",null,"You can learn more about the differences by reading this ",(0,i.yg)("a",{parentName:"p",href:"https://www.geeksforgeeks.org/difference-between-function-and-procedure/"},"article"),"\nby GeeksForGeeks."),(0,i.yg)("admonition",{title:"Which is better?",type:"note"},(0,i.yg)("p",{parentName:"admonition"},"Which method is better between classic function and procedure? Well, there is no hard and\nstrict rule on which method should be chosen. Technically they are both true."),(0,i.yg)("p",{parentName:"admonition"}
1,"You have to choose a particular method depending on the situation and how you want to solve\na problem.")),(0,i.yg)("p",null,"Note that in neither of these cases a type has been specified inside the function call\n(",(0,i.yg)("inlineCode",{parentName:"p"},"add(x,y)"),") for a parameter passed. You don't have to do that because ",(0,i.yg)("strong",{parentName:"p"},"the type is already\nspecified in the header of the function"),"."),(0,i.yg)("h3",{id:"passage-of-values"},"Passage of values"),(0,i.yg)("p",null,"In both cases, ",(0,i.yg)("inlineCode",{parentName:"p"},"x")," and ",(0,i.yg)("inlineCode",{parentName:"p"},"y")," values are passed and copied inside ",(0,i.yg)("inlineCode",{parentName:"p"},"a")," and ",(0,i.yg)("inlineCode",{parentName:"p"},"b")," respectively. They\nare different variable pairs, but with the function call ",(0,i.yg)("inlineCode",{parentName:"p"},"sum(x,y)")," they have associated and\nmatched with each other in that order. I could have called them both ",(0,i.yg)("inlineCode",{parentName:"p"},"a")," and ",(0,i.yg)("inlineCode",{parentName:"p"},"b")," with any ",(0,i.yg)("inlineCode",{parentName:"p"},"x"),"\nor ",(0,i.yg)("inlineCode",{parentName:"p"},"y")," identifier, but I intentionally made that to briefly explain how the passage of values\nworks."),(0,i.yg)("p",null,"Since this is a tricky topic to explain and understand, but also very important, the next chapter\nis dedicated exactly to that."),(0,i.yg)("div",{className:"footnotes"},(0,i.yg)("hr",{parentName:"div"}),(0,i.yg)("ol",{parentName:"div"},(0,i.yg)("li",{parentName:"ol",id:"fn-1"},(0,i.yg)("a",{parentName:"li",href:"https://www.geeksforgeeks.org/difference-between-argument-and-parameter-in-c-c-with-examples/"},"GeeksforGeeks.org - Difference between Argument and Parameter in C/C++ with Examples"),(0,i.yg)("a",{parentName:"li",href:"#fnref-1",className:"footnote-backref"},"\u21a9")))))}g.isMDXComponent=!0},8406:(e,t,n)=>{n.d(t,{A:()=>a});const a=n.p+"assets/images/function-parts-dedeb9a5713a74350039a816d63774f7.svg"}}]);

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.