Creating functions in C++<\/p>\n
Functions allow to structure programs in segments of code to perform individual tasks.<\/p>\n
In C++, a function is a group of statements that is given a name, and which can be called from some point of the program. The most common syntax to define a function is:<\/p>\n
<\/p>\ntype name ( parameter1, parameter2, ...) { statements }
<\/code>
Where:
– type<\/code> is the type of the value returned by the function.
– name<\/code> is the identifier by which the function can be called.
– parameters<\/code> (as many as needed): Each parameter consists of a type followed by an identifier, with each parameter being separated from the next by a comma. Each parameter looks very much like a regular variable declaration (for example:
int x<\/code>), and in fact acts within the function as a regular variable which is local to the function. The purpose of parameters is to allow passing arguments to the function from the location where it is called from.
– statements<\/code> is the function’s body. It is a block of statements surrounded by braces { } that specify what the function actually does.<\/p>\n<\/section>\n
<\/h1>\n