# Dice Function

## Recommended Posts

Roberts91    135
I am having a problem with the following exercise: Write a dice function: that is, a function that returns two random numbers, both in the range [1, 6]. Implement the function two times: once using references, and second time using pointers. Your function declarations should look like this: void Dice(int& die1, int& die2) not gonna write the other one as of right now I just wanna make one Dice Function game just using references here is what I have so far.
#include <iostream>
#include <cstdlib>

using namespace std;

void Dice(int& die1, int& die2)
{

}

int RandInt(int low, int high)
{
return rand()%(high-low+1)+low;
}

int main()
{

}

write now im trying to figure out how I can say: void Dice(int& die1 = low, int& die2 = high) of course my Dice function can't recognize my RandInt function so how would I go about referencing them to being able to indetify them?

##### Share on other sites
You mean you can't call the RandInt function from the dice one? That would be because it's below it in the file. You can either move it so that it's above the Dice function or you can use a function declaration (not sure if that's the right terminology) and do something like :

int RandInt(int low, int high);

right below the includes.

##### Share on other sites
Dancin_Fool    785
#include <iostream>#include <cstdlib>using namespace std;int RandInt(int low, int high);void Dice(int& die1, int& die2){	 }int RandInt(int low, int high){	return rand()%(high-low+1)+low;}int main(){	}

Just have to declare it beforehand.

##### Share on other sites
rip-off    10979
Quote:
 Original post by Dancin_FoolJust have to declare it beforehand.

Don't let Zahlman hear you say that!

The easiest solution is to physically move the entire function above the Dice function. This does two things, firstly it removes the need to keep the function declaration and definition in sync and second it allows us to detect circular function calls (not always a bad thing, but should be noted because they could potentially form a loop):
#include <iostream>#include <cstdlib>using namespace std;int RandInt(int low, int high){	return rand() % (high - low + 1) + low;}void Dice(int& die1, int& die2){}int main(){}

Of course these benefits are lost once we move to multiple files, but even still it helps when writing small helper functions (the kind that like hiding in anonymous namespaces).

[edit: in other words, the first thing that young Guybrush said [smile]]

##### Share on other sites
Quote:
Original post by rip-off
Quote:
 Original post by Dancin_FoolJust have to declare it beforehand.

Don't let Zahlman hear you say that!

The easiest solution is to physically move the entire function above the Dice function. This does two things, firstly it removes the need to keep the function declaration and definition in sync and second it allows us to detect circular function calls (not always a bad thing, but should be noted because they could potentially form a loop):
*** Source Snippet Removed ***

I think its good to get people in the habit of using headers and function prototypes at an early stage. It confuses people a lot later on.

##### Share on other sites
Dancin_Fool    785
Quote:
 Original post by rip-offDon't let Zahlman hear you say that!

Hmmm I'm not sure I understand the problem of using function prototypes here.

##### Share on other sites
rip-off    10979
It isn't a problem, the program will compile either way. Call it style, or whatever. You are putting in an *extra line of code*, which you will have to *manually update if you change the function declaration*, compared to the alternative of moving the function and doing neither of those things. To me, this seems the most sensible solution.

##### Share on other sites
Dancin_Fool    785
True enough, but no different from seperating functions in a class from implementation and declaration. You still have to update both in that case as well.
I agree though in this simple case that the easiest solution would be to move the function, but if the OP has a lot of functions its easier to just declare them all at the top and not worry about the order in which he creates the implementation.
Of course this is a very c procedural way of doing things and it would be better to create an object oriented approach if they're going to use c++.

##### Share on other sites
You will understand at the point when using function prototypes becomes a necessity, such as if you are using more than two code files. Its good for beginner programmers to learn how it works, and how to use function prototypes properly. Re-ordering the functions constantly is one solution, of course, but the habit of re-ordering everything when something doesn't compile, such as reordering includes, eventually gets people into a big mess.

##### Share on other sites
Zahlman    1682
Quote:
 Original post by Dancin_FoolTrue enough, but no different from seperating functions in a class from implementation and declaration. You still have to update both in that case as well.

Yes, but that's something that's forced upon you, and becomes a good idea in order to publish an interface to other modules. Within the same translation unit, you already know what functions can be called, so the publishing is not valuable; and why do the work when it *isn't* forced upon you, and isn't giving you a benefit?

Quote:
 Of course this is a very c procedural way of doing things and it would be better to create an object oriented approach if they're going to use c++.

Er, I don't see how that's relevant. Also, idiomatic C++ usage is multi-paradigm.

Anyway, I'm not trying to advocate constant re-ordering; I'm trying to advocate thinking about it first, and getting it right the first time. :) Also, keep in mind that it is common to not mention every function in the header; this is how you keep non-member functions "private".