In this article, you’ll learn about various techniques of C++ User-defined Function to solve a single problem.
C++ User-defined Function can be classified into the following categories to help comprehend parameters and returns in functions:
- No-argument and no-return-value function
- No-argument function that returns a value
- No return value for a function with an argument.
- Return value and argument function
Examples
- void add() // No return without argument
- void sub(int,int)// No return argument
- int mul() // Return without argument
- float div(int ,int) // Return with argument
Function with No arguments passed and no return value
- Each function is independent in this form of function.
- They require data, data values, and the ability to calculate and show the same block.
- These features can be used to display information or they can be totally controlled by the user.
Contents
Example: Function with No arguments passed and no return value
Consider a circumstance where you need to determine the prime number. As noted above, this challenge is overcome by creating user-defined functions in four different methods.
// This program to check Prime number: # include <iostream> using namespace std; void prime(); int main(){ // No argument is passed to prime() prime(); return 0; } // Return type of function is void because value is not returned. void prime(){ int num, i, a = 0; cout << "Enter a positive integer enter to check: "; cin >> num; for(i = 2; i <= num/2; ++i) { if(num % i == 0) { a = 1; break; } } if (a == 1) { cout << num << " is not a prime number."; } else { cout << num << " is a prime number."; } }
Output 1
Enter a positive integer enter to check: 2 , Output: 2 is a prime number.
Output 2
Enter a positive integer enter to check: 4, Output: 4 is not a prime number.
Working of above program
- prime is called with no arguments from main in the above application.
- prime accepts a positive number from the user and determines whether it is a prime number.
- The function returns no value since the return type of prime is void.
Function with No arguments passed but a return value
- The number of arguments is passed from the calling function to the called function in this sort of function, but the called function returns a value.
- The called function variable is declared and initialized in the same called function module. The called function is self-contained.
Example: Function with No arguments passed but a return value
#include <iostream> using namespace std; int prime(); int main() { int number, a, flag = 0; // No argument is passed to prime() number = prime(); for (a = 2; a <= number/2; ++a) { if (number%a == 0) { flag = 1; break; } } if (flag == 1) { cout<<num<<" is not a prime number."; } else { cout<<num<<" is a prime number."; } return 0; } // Return type of function is int int prime() { int i; printf("Enter a positive integer to check: "); cin >> i; return i; }
Output 1
Enter a positive integer enter to check: 2, Output: 2 is a prime number.
Output 2
Enter a positive integer enter to check: 4, Output: 4 is not a prime number.
Working of the above program
- The prime function is called with no arguments from the main method in the above application.
- prime asks the user for a positive integer. Because the function’s return type is an int, it returns the user’s inputted number to the calling main function.
- The number is then tested for primeness in the main and printed on the screen if it is.
Function with Arguments passed but no return value
- This type of function passes inputs to the called function through the calling function but does not return a value.
- Such functions are practically dependent on one another.
Example: Function with Arguments passed but no return value
#include <iostream> using namespace std; void prime(int n); int main() { int number; cout << "Enter a positive integer to check: "; cin >> number; // Argument num is passed to the function prime prime(number); return 0; } // There is no return value to calling function. Hence, return type of function is void. */ void prime(int i) { int n, flag = 0; for (n = 2; n <= i/2; ++n) { if (i%n == 0) { flag = 1; break; } } if (flag == 1) { cout << i << " is not a prime number."; } else { cout << i << " is a prime number."; } }
Output 1
Enter a positive integer enter to check: 3, Output: 3 is a prime number.
Output 2
Enter a positive integer enter to check: 6, Output: 6 is not a prime number.
Working of the above program
- The user is initially prompted for a positive integer, which is then stored in the variable num.
- The user is initially prompted for a positive integer, which is then stored in the variable num.
- The number is supplied to the prime function, which checks whether the number is prime and prints the result.
- The function returns no value since the return type of prime is void.
Function with Arguments passed and a return value.
- This is the greatest kind since it completely isolates the function from its inputs and outputs, leaving only the logic to be described inside the function body.
- Both functions are mutually exclusive.
Example: Function with Arguments passed and a return value
#include <iostream> using namespace std; int prime(int n); int main() { int number, flag = 0; cout << "Enter positive integer to check: "; cin >> number; // Argument num is passed to check() function flag = prime(num); if(flag == 1) cout << number << " is not a prime number."; else cout<< number << " is a prime number."; return 0; } /* This function returns integer value. */ int prime(int n) { int i; for(i = 2; i <= n/2; ++i) { if(n % i == 0) return 1; } return 0; }
Output 1
Enter a positive integer enter to check: 3, Output: 3 is a prime number.
Output 2
Enter a positive integer enter to check: 6, Output: 6 is not a prime number.
Working of the above program
C++ User-defined Function, C++ User-defined Function,C++ User-defined Function, user defined functions in c, what is user defined functions in c++
- A positive integer is requested from the user and stored in the variable num in the above software.
- The number is sent to the prime function, which determines whether or not the number is prime.
- Prime returns 1 or 0 to the main calling function because its return type is an int. 1 is returned if the number is a prime number. Otherwise, 0 is returned.
- The returning 1 or 0 is placed in the variable flag in the main method, and the corresponding text is printed on the screen.
Which method is better?
- All four programs above provide the same result and are technically sound.
- There is no hard and fast rule for deciding which method to use.
- The method you use is determined by the situation and how you wish to tackle the problem.
You may like:
https://theudaipurstore.com/maharana-pratap/
C++ Goto statement with Example | C++ Programming