For better understanding of arguments and return values in functions, user-defined functions can be categorized into various types:
- Function with no argument and no return value
- Function with no argument but return value
- Function with argument but no return value
- Function with argument and return value
Consider a situation in which you have to check prime number. Let's do that by making user-defined functions in 4 different ways as mentioned above.
No Arguments Passed and No Return Value
#include <iostream>
using namespace std;
// declare a function
// with no arguments and no return value
void check_prime();
int main() {
// no argument is passed to prime()
check_prime();
return 0;
}
// function to check if the number is prime
void check_prime() {
int num;
cout << "Enter a positive integer to check: ";
cin >> num;
bool is_prime = true;;
for(int i = 2; i <= num/2; ++i) {
if(num % i == 0) {
is_prime = false;;
break;
}
}
if (is_prime) {
cout << num << " is a prime number.";
}
else {
cout << num << " is not a prime number.";
}
}
Output
Enter a positive integer to check: 7 7 is a prime number.
Here,
- We haven't passed any argument to
check_prime()
. - The return type of
check_prime()
isvoid
so it doesn't return any value.
The check_prime()
function takes user input for a number, checks whether the given number is prime and gives output all by itself.
No Arguments Passed but a Return Value
#include <iostream>
using namespace std;
// declare a function with no arguments
// and return type int
bool check_prime();
int main() {
// call prime and assign return value to is_prime
bool is_prime = check_prime();
if (is_prime) {
cout << "Your number is a prime number.";
}
else {
cout << "Your number is not a prime number.";
}
return 0;
}
// function to check if the number is prime
bool check_prime() {
int num;
cout << "Enter a positive integer to check: ";
cin >> num;
bool is_prime = true;
for (int i = 2; i <= num / 2; ++i) {
if (num % i == 0) {
is_prime = false;
break;
}
}
// return the value
return is_prime;
}
Output
Enter a positive integer to check: 8 Your number is not a prime number.
Here,
- We haven't passed any argument to
check_prime()
. - The return type of
check_prime()
isbool
so it returns the is_prime variable ofbool
type.
The check_prime()
function takes user input for a number, checks whether the given number is prime and returns is_prime.
The main()
function gives the output based on the value returned by check_prime()
.
Arguments Passed but no Return Value
#include <iostream>
using namespace std;
// declare a function with int argument
// and no return value
void check_prime(int n);
int main() {
cout << "Enter a positive integer to check: ";
int num;
cin >> num;
// pass argument num function prime()
check_prime(num);
return 0;
}
// function to check if the number is prime
void check_prime(int n) {
bool is_prime = true;
for (int i = 2; i <= n/2; ++i) {
if (n%i == 0) {
is_prime = false;
break;
}
}
if (is_prime) {
cout << n << " is a prime number.";
}
else {
cout << n << " is not a prime number.";
}
}
Output
Enter a positive integer to check: 37 37 is a prime number.
Here,
- We have passed one argument of
int
type tocheck_prime()
. - The return type of
check_prime()
isvoid
so it doesn't return any value.
The main()
function calls check_prime()
with argument num.
The check_prime()
function checks whether the given number is prime and gives the output.
Arguments Passed and a Return Value
#include <iostream>
using namespace std;
// declare a function
// with int argument and int return type
bool check_prime(int n);
int main() {
int num;
cout << "Enter a positive integer to check: ";
cin >> num;
int is_prime = check_prime(num);
if (is_prime == true)
cout << num << " is a prime number.";
else
cout << num << " is not a prime number.";
return 0;
}
// function to check if the number is prime
bool check_prime(int n) {
for (int i = 2; i <= n/2; ++i) {
if (n % i == 0)
return false;
}
return true;
}
Output
Enter a positive integer to check: 14 14 is not a prime number.
Here,
- We have passed one argument of
int
type tocheck_prime()
. - The return type of
check_prime()
isbool
so it returns eithertrue
orfalse
.
The main()
function calls check_prime()
with argument num.
The check_prime()
returns true
if the number is prime and false
otherwise. Then main()
gives the output based on the value returned by check_prime()
.
Also Read: