Friday, December 4, 2020

Function in C++ language.

 Learn programming for free


Function in C++ language.



Function is basically a block of statement which is used to perform some specific task.


In C++  language we have 4 type of function.When we declare a function then it tells the compiler about return type of function,parameter of function and it's function name.


In C++ language we divide large program into blocks and call them as and when they required.

We will discuss function in depth in this blog.


Types of function.


  1. Function without parameter and without return type.
  2. Function without parameter and with return type.
  3. Function with parameter and without return type.
  4. Function with parameter and with return type.

We will discuss each of them with very basic example:


1.Function without parameter and without return type:


#include<iostream.h>

using namespace std;

void sum()
{
int a,b,sum=0;
cout<<"Enter first number:";
cin>>a;
        cout<<"Enter second number:";
cin>>b;
sum = a+b;
cout<<sum;
}

int main()
sum();
return 0; 
}


2.Function without parameter and with return type:


#include<iostream.h>

using namespace std;

int sum()
{
int a,b,sum=0;
cout<<"Enter first number:";
cin>>a;
cout<<"Enter second number:";
cin>>b;
sum = a+b;
return sum;
}
int main()
int x = sum();
cout<<x;
return 0; 
}


3.Function with parameter and without return type:


#include<iostream.h>

using namespace std;

void sum(int a,int b)
{
int sum = a+b;
cout<<sum;
}
int main()
int a,b;
        cout<<"Enter first number:";
cin>>a;
cout<<"Enter second number:";
cin>>b;

sum(a,b);
return 0; 
}

4.Function with parameter and with return type:

#include<iostream.h>

using namespace std;

int sum(int a,int b)
{
int sum = a+b;
return sum;
}
int main()
int a,b,x = 0;
         cout<<"Enter first number:";
cin>>a;
cout<<"Enter second number:";
cin>>b;
x = sum(a,b);

cout<<x;
return 0; 
}

Share this

0 Comment to "Function in C++ language."

Post a Comment

If you have any doubts then let me know.