Use Void Functions in C++

  1. Use the void Function to Find Which String Is Longer
  2. Use the void Function to Find if Key Exists in a Map
  3. Use the void Function to Sort Elements in Vector

This article will demonstrate multiple methods about how to use void functions in C++.

Telephone Directory Project In C++ ...
Telephone Directory Project In C++ with Source code Free Download 2020 | C++ Projects

Use the void Function to Find Which String Is Longer

The functions that have no return value have the void type specified as the return parameter. In void functions, the implicit return statement is called after the function body’s last statement. Note that the explicit return statement can be placed in the void function body where the control flow needs to be moved to the caller function immediately. In the following example, the isLessString function contains the only conditional statement to print the corresponding strings to the console, and the implicit return is executed after it.

#include <iostream>
#include <algorithm>
#include <iterator>
#include <map>

using std::cout; using std::endl;
using std::string; using std::map;
using std::vector;

void isLessString(string &s1, string &s2)
{
    s1.size() < s2.size() ?
    cout << "string_1 is shorter than string_2" << endl:
    cout << "string_2 is shorter than string_1" << endl;
}

int main() {
    string str1 = "This string has arbitrary contents";
    string str2 = "Another string with random contents";

    isLessString(str1, str2);

    return EXIT_SUCCESS;
}

Output:

string_1 is shorter than string_2

Use the void Function to Find if Key Exists in a Map

In this case, the void function is utilized to implement the key searching function for the std::map container. Note that the result is communicated by printing the corresponding string constant to the cout stream. Although this is not the preferred method for passing the data internally in the program, it can be used to indicate information to the end-user.

#include <iostream>
#include <algorithm>
#include <iterator>
#include <map>

using std::cout; using std::endl;
using std::string; using std::map;
using std::vector;

void keyExistsInMap(map<string, string> &m, const string& key)
{
    m.find(key) != m.end() ?
    cout << "Key exists in a map" << endl :
    cout << "Key does not exist in a map" << endl;
}

int main() {
    map<string, string> veggy_map = {{"a", "Ali",},
                                     {"m", "Malvo",},
                                     {"p", "Pontiac",},
                                     {"s", "Sensi",}};

    keyExistsInMap(veggy_map, "s");
    keyExistsInMap(veggy_map, "x");

    return EXIT_SUCCESS;
}

Output:

Key exists in a map
Key does not exist in a map

Use the void Function to Sort Elements in Vector

Alternatively, one can implement a wrapper function for the std::sort algorithm that takes the vector object as a reference and sorts its elements in ascending order. Note that void functions usually lack the means to communicate the failure to the caller functions, so one should always factor in when designing the solution.

#include <iostream>
#include <algorithm>
#include <iterator>
#include <map>

using std::cout; using std::endl;
using std::string; using std::map;
using std::vector;

void sortVector(vector<string> &vec)
{
    std::sort(vec.begin(), vec.end(),
              [] (const auto &x, const auto &y) {return x < y;});
}

int main() {
    vector<string> arr = { "element", "implementation", "or", "the",
                           "execution", "dependent", "template", "character",
                           "that", "all", "via", "class" };

    sortVector(arr);

    for (const auto &item : arr) {
        cout << item << "; ";
    }
    cout << endl;

    return EXIT_SUCCESS;
}

Output:

all; character; class; dependent; element; execution; implementation; or; template; that; the; via;

Related Article - C++ Function

  • C++ Redefinition of Formal Parameter
  • Redefinition of Default Parameter in C++
  • The shellExecute() Function in C++
  • The const Keyword in Function Declaration of Classes in C++