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
C++ program to multiply two numbers using function
Java program to multiply two numbers using method
Python program to multiply two numbers using function
C# program to multiply two numbers using function
PHP program to multiply two numbers using function
Data types and variable in Java
Find product of two numbers using recursion in Java
Data types and variable in Java language
Arithmetic function in C language
User defined function in C language
C# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…
Program to count vowels, consonants, words, characters and space in Java In this article, we…
How to print multiplication table using Array in C++ language In this post, we will…
C Program to multiplication table using Array In this tutorial , we will discuss about…
Java program to check odd or even using recursion In this tutorial, we discuss a…