Generally speaking we use the term procedure to refer to a routine, like the ones above, that simply carries out some task (in C++ its definition begins with void). A function is like a procedure but it returns a value; its definition begins with a type name, e.g. int or double indicating the type of value it returns. Procedure calls are statements that get executed, whereas function calls are expressions that get evaluated.
A simple program to show the difference as follows:
//Program tested on Microsoft Visual Studio 2008 - Zahid Ghadialy
//This program shows difference between functions and procedures
#include<iostream>
using namespace std;
//function
bool checkIfPositive(int x)
{
if(x >= 0)
return true;
return false;
}
//procedure
void printIfPositive(int x)
{
bool isPositive = checkIfPositive(x);
if(isPositive)
cout<<"x is positive and its value is "<<x<<endl;
}
int main()
{
printIfPositive(3);
printIfPositive(-54);
printIfPositive(710);
return 0;
}
The output is as follows:
thanks
ReplyDelete