There are three very important terminology related to functions while coding: -
Function Declaration
Function Definition
Function Call
Function Declaration: - This is also known as Function Initialization. We mostly declare a function after declaring all the header files. As soon as we declare the function, the compiler creates its lexical definition, and accordingly segmentation of memory takes place. Thus the moment compiler see the function declaration, it knows how to execute its definition on function call. In Turbo C++ you must have to declare a function whereas in Dev C++ this is not require as Dev C++ compiler takes care of this.
Function Definition: - A function contains definition about the supposed task we have to perform. When a function have parameters passed to it, then it is parametric function. When a function does not contains any parameters then it is non-parametric function.
Function Call: - The function call can take place within a class, a function and main() function. When a function call itself in its body, then such a function is known as recursive function.
/*Program 1
Date : 20 - March - 2019
Program name : Addition of two numbers using functions
Developer : Aditi B*/
#include <iostream>
using namespace std;
int add(int,int); //function declaration
int add (int x, int y)
{
// defining the function
int z;
z = x+y; //add the parameter objects and assign it to variable z.
return z; // function always return a value.
}
int main()
{
int a, b, sum;
cout << "Enter Number 1: \t";
cin >> a;
cout << "Enter Number 2: \t";
cin >> b;
sum = add(a,b); // Initialising the function and passing parameters
cout <<"\n---------------------------------------------------------";
cout << "\n the addition of given two numbers is:\t" << sum;
cout <<"\n---------------------------------------------------------\n\n\n";
return 0;
}
Output