Confused about module call

This is in digital picture mode, so I can't paste the code here. I can only give screenshots.


http://prntscr.com/biqx2q
http://prntscr.com/biqxaa
http://prntscr.com/biqxfl
http://prntscr.com/biqxjq

My book taught that for the body of a module or the statement of a module to be executed it has to be called--this is generally done in main--though it can be done by other modules besides the main.

In the language champion (the name of this digital book I have open in this tab) it shows getHoursWorked and getPayRate in the function prototype at the top--before the main module is show--but none of the modules call it, so why are they there? How would the code even work?

I reviewed what I knew about modules, but found nothing that would explain how we would use the two modules mentioned if they weren't called.

The only modules that were called were the RegularPay and the PayWithOT.
Last edited on
Given your current code, you are indeed right, the function getHoursWorked is never called. Meaning it is completely useless in the code you currently provided. The program should still compile, the code is completely valid, part of it are just unused, meaning you might as well omit them and it would have the same result.

In C++ you can define all functions you want, it will only be executed if you call it. As an example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>

void func1()
{
    std::cout << "In function 1" << std::endl;
}

void func2()
{
    std::cout << "In function 2" << std::endl;
}

void func3()
{
    std::cout << "In function 3" << std::endl;
}

int main()
{
    func1();
    func3();
    return 0;
}


This example would produce the following output:
In function 1
In function 3


As you can see, I defined a function named func2, but never called it. As a result, it isn't executed at all, which you can see in the output, there's no message saying "In function 2". Even though I defined the function, I'm not forced to call it. I would just have saved myself a few keystrokes by not typing the other function, other than that it is still valid in C++.
Last edited on
Topic archived. No new replies allowed.