For better understanding of arguments and return values in functions, user-defined functions can be in various forms like:
- 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
No Arguments Passed and No Return Value
#include <iostream>
using namespace std;
// declare a function
// with no arguments and no return value
void say_hello();
int main() {
// no argument is passed to the function
say_hello();
return 0;
}
void say_hello() {
cout << "Hello!";
}
Output
Hello!
Here,
- We haven't passed any argument to
say_hello(). - The return type of
say_hello()isvoidso it doesn't return any value.
No Arguments Passed but a Return Value
#include <iostream>
using namespace std;
// declare a function with no arguments
// and return type string
string get_username();
int main() {
// call prime and assign return value to is_prime
string user_name = get_username();
cout << "Hello, " << user_name;
}
string get_username() {
string name;
cout << "Enter your user name\n";
cin >> name;
return name;
}
Output
Enter your user name : John Hello, John
Here,
- We haven't passed any argument to
get_username(). - The return type of
get_username()is string so it returns the name input by the user.
The get_username() function takes user input for a name, and returns the name.
The main() function gives the output based on the value returned by get_username().
Arguments Passed but no Return Value
#include <iostream>
using namespace std;
void say_hello(string name);
int main() {
cout << "Enter your name: ";
string name;
cin >> name;
// pass argument num function prime()
say_hello(name);
}
void say_hello(string name) {
cout << "Hello " << name ;
}
Output
Enter your name: John Hello John
Here,
- We have passed one argument of type
stringtosay_hello(string). - The return type of
say_hello()isvoidso it doesn't return any value.
The main() function calls say_hello() with an argument name.
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) {
// 0 and 1 are not prime numbers
if (n == 0 || n == 1) {
return false;
}
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
inttype tocheck_prime(). - The return type of
check_prime()isboolso it returns eithertrueorfalse.
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: