Programming languages - C++

Back to Course

Lesson Description

Lession - #44 C++ Functions

A function is a gathering of statements that together play out an errand. Each C++ program has somewhere around one function , which is main(>
, and every one of the most inconsequential projects can characterize extra functions.
You can split your code into discrete functions. How you split your code among various functions ultimately depends on you, however legitimately the division normally is to such an extent that each function plays out a particular errand.
A functionsannouncement enlightens the compiler concerning a function's name, return type, and parameters. A function definition gives the genuine body of the function .

Defining a Function

The general form of a C++ function definition is as follows −

return_type function_name( parameter list >
{ body of the function }

A C++ function definition comprises of a capacity header and a function body. Here are the pieces of a function −

  • Return Type − A function might return a value . The return_type is the data type of the value the function returns. A few functions play out the ideal activities without returning a value . For this situation, the return_type is the keyword void.
  • Function Name − This is the genuine name of the function . The function name and the parameter list together comprise the function signature.
  • Parameters − A parameter resembles a placeholder. Whenever a function is summoned, you pass a value to the parameter . This value is alluded to as genuine parameter or argument. The parameter list alludes to the kind, order, and number of the parameters of a function . Parameters are discretionary; that is, a function might contain no parameters.
  • Function Body − The function body contains an assortment of proclamations that characterize what the function does.






// function returning the max between two numbers

int max(int num1, int num2>
{ // local variable declaration int result; if (num1 > num2>
result = num1; else result = num2; return result; }

Calling a Function

While making a C++ function, you give a meaning of what the function needs to do. To utilize a function, you should call or summon that function.

At the point when a program calls a function, program control is moved to the called function. A called function performs characterized undertaking and when it's return statement is executed or when its function finishing shutting support is reached, it gets program control once again to the principal program.

To call a function, you essentially have to pass the necessary parameters alongside function name, and on the off chance that function returns a value, you can store brought value back. For instance −

using namespace std;
// function declaration
int max(int num1, int num2>
; int main (>
{ // local variable declaration: int a = 100; int b = 200; int ret; // calling a function to get max value. ret = max(a, b>
; cout << "Max value is : " << ret << endl; return 0; } // function returning the max between two numbers int max(int num1, int num2>
{ // local variable declaration int result; if (num1 > num2>
result = num1; else result = num2; return result; }





What is an C++ atomic ? Atomic. Atomic types are types that encapsulate a value whose access is guaranteed to not cause data races and can be used to synchronize memory accesses among different threads.
What is upper bound C++? upper_bound(>
is a standard library function in C++ defined in the header .
The keyword mutable c++ is mainly used to allow a particular data member of const object to be modified.
The Arduino C++ IDE but of course the physical environment is limited so not all C/C++ features can be used.