In this tutorial, we will discuss Type of user defined function in C++ language
Already, we would learn about Type of user-defined function in C language.
Now we get to know Type of user-defined function in C++ language.
The user defines functions are categorized are:
Examples
#include <iostream> #include <conio.h> using namespace std; void add(); int main() { add(); getch(); return 0; } void add()//No arguments are passed to this function {//void keyword never return value from function cout << "Function with no argument and no return value "<<"\n" << endl; int num1,num2; cout<<"Enter the number for num1: "; cin>>num1; //get input from user for num1 cout<<"Enter the number for num2: "; cin>>num2; //get input from user for num2 int sum=num1+num2; //find total value cout<<"Total value is: "<<sum<< endl; }
When the above code is compiled and executed, it produces the following results
Function with no argument and no return value Enter the number for num1: 67 Enter the number for num2: 54 Total value is : 121
In the above program, initially, two integer value is got from the user and stored in the variable num1,num2
Then, num1, num2 values passed to the function add().
add() is a user-defined function for add two numbers. it is called from the main function without arguments
#include <iostream> #include <conio.h> using namespace std; int sub(); int main() { cout<<"Function with no argument but return value"<<"\n"<<endl; int a,b; int result=a-b; cout<<"Different is :"<<sub(); getch(); return 0; } int sub()//no arguments are passed to this function { //int keyword returns value int num1,num2; cout<<"Enter the number for num1 :"; cin>>num1;//get input from user for num1 cout<<"Enter the number for num2 :"; cin>>num2; //get input from user for num2 return num1-num2; }
When the above code is compiled and executed, it produces the following results
Function with no argument but return value Enter the number for num1: 67 Enter the number for num2: 45 Different is: 22
In the above program, initially, two integer value is got from the user and stored in the variable num1,num2
Then, num1, num2 values passed to the function sub().
sub() a user-defined function for subtraction two numbers. it is called from the main function without arguments but return a value
#include <iostream> #include <conio.h> using namespace std; void mul(int a,int b); int main() { int num1; int num2; int multi; cout << "Function with argument no return value" << endl; cout<<"Enter the number for num1: "; cin>>num1; //get input from user for num1 cout<<"Enter the number for num2: "; cin>>num2; //get input from user for num2 mul(num1,num2); //passing argument to mul() function getch(); return 0; } void mul(int a, int b)//arguments are passed this function { cout<<"Multiply is: "<<a*b<<endl; //void no return value }
When the above code is compiled and executed, it produces the following results
Function with an argument no return value Enter the number for num1: 12 Enter the number for num2: 23 multiply is: 276
In the above program, initially, two integer value is got from the user and stored in the variable num1,num2
Then, num1, num2 values passed to the function mul().
mul() a user-defined function for multiply two numbers. it is called from the main function with arguments and no return value
#include <iostream> #include <conio.h> using namespace std; int addition(int a,int b); int main() { int num1; int num2; int add; cout << "Function with argument return value" << endl; cout<<"Enter the number for num1: "; cin>>num1; //get input from user for num1 cout<<"Enter the number for num2: "; cin>>num2; //get input from user for num2 add=addition(num1,num2);//find total value cout<<"Addition is: "<<add<<endl; getch(); return 0; } int addition(int a, int b)//arguments are passed this function { return(a+b); //return value }
When the above code is compiled and executed, it produces the following results
Function with argument and return value Enter the number for num1: 45 Enter the number for num1: 56 Addition is: 101
In the above program, initially, two integer value is got from the user and stored in the variable num1,num2
Then, num1, num2 values passed to the function add().
add() is a user-defined function for add two numbers. it is called from the main function with arguments and return value
Suggested for you
String function in C language
PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…
PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…
5 methods to add two numbers in Java In this tutorial, we will discuss the…
Python full Pyramid star pattern program In this tutorial, we will discuss the concept of…
Write a function or method to convert C into F -Entered by the user In…
How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…