Sep 17

Concept of Function in c++

Once a function is written, you can call it any number of times. By calling a function, you transfer execution of the program to the function- definition code, which runs until it is finished or until it encounters a  return statement.

Example of Avg Function

void main(){
double a=5;
double b=6;
cout<<"AVG is" << avg(a,b);
double avg(double x, double y){
double v=(x+y)/2;
return v;

Note that only main is guaranteed to be executed. Other functions run only as called. But there are many ways a function can be called. For example, main can call a function A, which in turn calls B and C, which in turn calls D.

Some Recommendation for function

1. At the beginning of program , Declare the function
2. Then define the function.
3. Then other function call the function.

Like above program, first declare the function.

RETURN_TYPE  Function_Name(Argument_List)

Then Define the Function

RETURN_TYPE Function_Name(Argument_List) {

Logical Statement;


Call the Function


Now We can create for prime number function in c++

//Declare function
bool prime(int n);
int main() {
int i;
cout<<"Enter Number (0-exit) and Press ENTER: "; cin>>i;
cout<<i<<"is Prime Number"<<endl;
cout<<i<<"is not Prime Number"<<endl;
return 0;
bool prime(int n)
int i;
for (i=2;i<SQRT(n); i++)
return false;
return true;

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>