Modularizing

This is my first time trying to modularize in C++. I'm confused as to what value returning functions are compared to void functions, and when to properly use one instead of the other. I have read about both types of functions, but am still kind of iffy about the whole return thing. Apparently modularizing in C++ and Java are not the same...but this is my attempt, and its throwing errors to my function prototype list, all of my cases in my switch statement, both of my inputs in my inputData function, and an error to my sortingAsc(const int size, ...etc) line. I'm sure I've completely messed up my program trying to modularize it, but what am I not doing right?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;

//function prototype
inputData();
sortingAsc();
sortingDesc();
printing();
searching();
ending();


main()
{
    const int size = 10; //declaring a constant int 'size' as 10
    string name[size]; //declaring an array 'name'
    int number[size] = {0}; //declaring an array 'number'
    int menuchoice = 0; //declaring int menuchoice
    cout << "---Menu--- \n"; //output 
    cout << "1. Input data";
    cout << "2. Sort data Ascending";
    cout << "3. Sort data Descending";
    cout << "4. Print all data";
    cout << "5. Search for an individual in the data"; 
    cout << "6. End program"; 
    cout << "Please type a choice number."; 
    cin >> menuchoice;
    switch (menuchoice)
    {
        case 1:
            inputData();
            break;
        case 2:
            sortingAsc();
            break;
        case 3:
            sortingDesc();
            break;
        case 4:
            printing();
            break;
        case 5:
            searching();
            break;
        case 6:
            ending();
            break;
    }
}

inputData(const int size, string name[size], int number[size])
{
    cout << "Please input 10 names and their corresponding numbers\n"; //output
    for(int i = 0; i < size; i++ ) //for
    {
        cout << "Name: " << i+1 << "? \n"; //output
        cin >> name[i]; //populating array name with names
        cout << "Number:\n"; //output
        cin >> number[i]; //populating array number with numbers
    } //end for
    return name[size], number[size];
    main();
}
    
sortingAsc(const int size, string name[size], int number[size])
{
    for(int i = 0; i < size; i++) //for statement for sorting
    {
        for(int j = i+1; j < size; j++) //for statement for sorting
        {
            if(number[j] < number[i]) // if statement
            {
                const int tempnum = number[i]; //sorting
                number[i] = number[j];
                number[j] = tempnum;
                
                const string tempstring = name[i]; //sorting
                name[i] = name[j];
                name[j] = tempstring;
            } // end if
        } // end for
    } // end for
    printing();
}

sortingDesc(const int size, string name[size], int number[size])
{
    for(int i = 0; i < size; i++) //for statement for sorting
    {
        for(int j = i+1; j < size; j++) //for statement for sorting
        {
            if(number[j] < number[i]) // if statement
            {
                const int tempnum = number[j]; //sorting
                number[j] = number[i];
                number[i] = tempnum;
                
                const string tempstring = name[j]; //sorting
                name[j] = name[i];
                name[i] = tempstring;
            } // end if
        } // end for
    } // end for
    printing();
}

printing(const int size, string name[size], int number[size])
{
    for(int i = 0; i < 10; i++)
    {
        cout << name[i] << ": " << number[i] << "\n";
    }
}

printing2(const int size, string name[size], int number[size], int rowName)
{
    cout << name[rowName] << ": " << number[rowName];
    main();
}

searching(const int size, string name[size], int number[size])
{
    cout << "Names and numbers \n";
    for(int i = 0; i < size; i++) //for
    {
        cout << name[i] << ": " << number[i] << '\n'; //output
    }
    string searchname;
    cout << "Search for a name \n";
    cout << "Type 9999 to cancel search \n";
    cin >> searchname;
    while(searchname != "9999")
    {
        int rowName = 0;
        
        while(rowName <=10 && name[rowName] != searchname)
        {
            rowName++;
        }
        if(rowName >=10)
        {
            cout << "That is an invalid search.";
            break;
        }
        printing();
        break;
    }
} //end searching

ending()
{
    break;//system("close"); is giving errors? so I just put that there.
}
You should never ever call main. Never ever. If you think you need to, what you need to use is some kind of loop.

The reasons to ever call system are very few. You almost certainly shouldn't be thinking about system("close"). The program will finish when it gets to the end of the main function.

Your function prototypes make no sense. Every function will either return a value, or not return a value.

Functions that do not return a value simply execute their code. Functions that return a value execute their code AND give back a value. Let's look at an example.

1
2
3
4
5
6
int add(first_number, second_number)
{
  return ( first_number + second_number);
}

int x = add( 3 , 4);


Here, add is a function. The function accepts two parameters, and it returns an int. This code has the meaning:

Make a variable named x, and set its value to the returnd valued from executing the function add(3, 4). So by looking at this code, can you tell me what value x will have?

That's the simplest example I can think of. What about it do you not understand?
So by that logic, should all of my functions be void? I just have trouble wrapping my mind around different kind of functions, like should this return a function? I'm used to programming in Java and not worrying about if I have to return anything. Stuck in that way of thinking, I guess. Also, I changed main to menu, because I do not want the program to end until the user chooses the option to close

*edit*
I'm used to always using voids in Java since that is what I was taught, and never knew there were anything else because nobody ever mentioned other types of functions until now. Got stuck in the mine frame that every function had void in it.
Last edited on
I'm used to programming in Java and not worrying about if I have to return anything

OP, Java's functions are exactly the same in this respect. Either they return void (i.e., nothing) or they return a value (i.e., something). If you can, relate the concept of "function" which returns a value to the concept of a function (i.e., a mapping) in mathematics.

To this end, the names of functions should not be participles or gerunds, but rather verbs or verb phrases. I mean "sort", not "sorting", "print", not "printing", and so on. If I had to guess, you're treating function names merely as labels for blocks of code, which is indicative of a conceptual error. Start by renaming them.

I'm extrapolating a bit here, but I guess you have got into the habit of avoiding returning values with the help of global state - that is, side-effects. This is not a scalable practice outside of tiny programs like this, and indeed the use of global state is strongly discouraged (in both Java and C++) for a number of good reasons.

At any rate, the first thing to do is understand what it means to return a value. Maybe give the tutorial a read:
http://www.cplusplus.com/doc/tutorial/functions/
Topic archived. No new replies allowed.