Declaration, initialization, and Body
To write and use any of the function in C++, you first need to declare it inside your program, to declare a function use following syntax.
Return_type name_of_function(parameter1, parameter2,….);
//Functions body statements
a: Return_type is the data type or value returned by the function.
b: Name_of_function is the desired name of your function.
c: Parameter1, parameter2 … are the input parameters that you need to pass to the function while calling it.
d: Inside braces you have to write the set of instructions, you need to execute every time.
Once you are done with declaring the function and all of its body statements, you can call it from anywhere inside your program.
The variables declared inside a function are local to that function only and no one can access them outside that function. But variables declared above the main function of your program are global variables and they can be accessed by your function.
To demonstrate functions well, I am writing this example function which will calculate sum of two longs every time it is called.
using namespace std;
long add_longs (long no1, long no2)
int main ()
temp = add_longs(a,b);
cout << "Result of addition = " << temp;