Difficulty grasping exactly what a defines a function

While I can usually manage to complete my coding homework, I have a hard time understanding what parts of my program count as a function and which parts don't. This is important because my homework requires me to add the following to my programs:

All functions other than the main ( ) will have a comment box as defined below:

/*************************************************************************
Purpose: What the function does (not how) and any additional notes
needed to understand and use the function
Inputs: Each non-obvious parameter on a separate line with in-line comments.
Assumes: List of each non-obvious external variable, control, and so on.
Returns: Explanation of return value or result of function
Effects: List of each effected external variable, control, file, and so
on and the affect it has (only if this is not obvious)
*************************************************************************/

Here is a program I'm working on - I would greatly appreciate it if someone could point out to me where (at least a couple spots) this would be placed and what each part of the comment box would be.

My instructor hasn't given any examples or instruction on how or where to use this, so I'm at a loss.
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

#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
	srand(static_cast<unsigned int>(time(0)));
	//possible words to jumble
	const int NUM_WORDS = 5;
	// create the array using an initilizer list
	const string WORDS[] =
	{
		"wall",
		"glasses",
		"labored",
		"persistent",
		"jumble"
	};

	//random index number
	int	choice = (rand() % NUM_WORDS);

	//word player must guess
	string secretWord = WORDS[choice];

	//jumbled version of word
	string jumbled = secretWord;

	//num characters in jumbled
	size_t length = jumbled.size();

	//mix up letters in jumbled
	for (size_t i = 0; i < length; ++i)
	{
		//swap letter at index i with letter at random index
		size_t randomIndex = (rand() % length); //rand num, 0 thru length - 1

		char temp = jumbled[i];
		jumbled[i] = jumbled[randomIndex];
		jumbled[randomIndex] = temp;
	}

	//welcome player and explain game
	cout << "\t\t\tWelcome to Word Jumble!";
	cout << endl << endl;
	cout << "Unscramble the letters to make a word.";
	cout << endl;
	cout << "Enter 'quit' to quit the game.";
	cout << endl << endl;
	cout << "The jumble is: " << jumbled;

	//guessing loop
	string guess; //player's guess
	do
	{
		cout << endl << endl << "Your guess: ";
		cin >> guess;

		if ((guess != secretWord) && (guess != "quit"))
		{
			cout << "Sorry, that's not it.";
		}
	} while ((guess != secretWord) && (guess != "quit"));

	//do loop can end without correct guess, so check guess
	if (guess == secretWord)
		cout << endl << "That's it! You guessed it!" << endl;
		cout << endl << "Thanks for playing." << endl;
	return 0;
}
The only function you have is main.

This is an example of a function, just google if you need a written description.


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
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>

using namespace std;

void hello()
{
/*************************************************************************
Purpose: What the function does (not how) and any additional notes 
needed to understand and use the function
Inputs: Each non-obvious parameter on a separate line with in-line comments.
Assumes: List of each non-obvious external variable, control, and so on. 
Returns: Explanation of return value or result of function
Effects: List of each effected external variable, control, file, and so 
on and the affect it has (only if this is not obvious)
*************************************************************************/

cout << "Hello World";

}



int main()
{
// This is the main function
hello(); // This calls your function hello.

return 0;
}
Last edited on
All functions other than the main ( )

In your code, there are none. There is a single function, namely main(), that's all.

See tutorial:
http://www.cplusplus.com/doc/tutorial/functions/
Thank you both for your replies and information.

 
int choice


Is this not a function because it is within the int main function?
Last edited on
int choice

Is this not a function because it is within the int main function?

No, that's a variable.

However, it's an interesting example, because that line in full reads like this:
 
int	choice = (rand() % NUM_WORDS);


There is a function there, at least there is a function call. rand() is a function provided in the standard library, and is made available by including the header <cstdlib>.

You may want to re-read the tutorial page on functions I previously linked.
For more about variables, see tutorial:
http://www.cplusplus.com/doc/tutorial/variables/
To give another example from some of your own code,
http://www.cplusplus.com/forum/general/215631/

Here there are three functions in addition to main(), that is four in total.

They are:
1
2
3
bool isVowel(char ch)
void removeVowels(string userStr)
void welcome() 



Topic archived. No new replies allowed.