C++ User-defined Function Types

For better understanding of arguments and return values in functions, user-defined functions can be in various forms like:


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() is void so 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 string to say_hello(string).
  • The return type of say_hello() is void so 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 int type to check_prime().
  • The return type of check_prime() is bool so it returns either true or false.

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:

Did you find this article helpful?

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges