Random Numbers (creating functions)

note: Everything below is just what i've learned. I have no prior knowledge of c++ or programming, so i could be wrong.

I had alot of trouble figuring out the basic syntax in DarkGDK, so i've decided that this wiki needs more tutorials.

Goal: This makes a function to produce random numbers, given a seed, using GDK's built in commands. Basically, it demonstrates how to make a function.

Start with: I started out with a basic template, looking like this:

void DarkGDK ( void ) {	return; }
 * 1) include "DarkGDK.h"

If you ran this, it would just exit out. Now, a review of the syntax thats really basic, discluding the DGDK commands "void DarkGDK(void)" it's fairly obvious what void means here. The computer would return a number, or would require input, but since the DarkGDK command needs neither, void is put in it's place. After that, theres opening brackets, then program ends (note the ";" after "return". This will be after every single command, even a variable declaration.

so, we want to make a function now. After reviewing the darkinvaders sample, it was clear that you need a declaration of what type of variable is output throught a function, and what type of variable is input.

Outside of the DarkGDK command, i wrote this:

float random(int seed) {    dbRandomize(seed); rnum=dbRnd(1000)/1000; return rnum; }

Now, the above code has a problem in it. rnum hasn't been declared as a float yet. So, at the top of the page, simply write

float rnum;

also, we have no variable for the results of the function to be stored in. so add

float a;

at the top.

Now, add the function inside the darkGDK brackets, and a command to print the results, and give us some time to view them. so now the program looks like this.

float a; float rnum;


 * 1) include "DarkGDK.h"

void DarkGDK ( void ) {    a=random(4); dbPrint(a); dbWait(10000);

return; }

float random(int seed) {    dbRandomize(seed); rnum=dbRnd(10000)/10000.0;

return rnum; }

But, if you were to run it now, you'd get an error message saying an identifier is missing. So, it's easy to assume what an identifier is, if you look at the dark invaders source code. it identifies that theres a function there, what it returns, and what it's inputs are. So, i clicked project->add new item -> header file. I named the new header "Main.h"

Then, again taking a page out of the dark invaders's header files, i wrote this:


 * 1) pragma once

float random( int seed );

Then switched back over to main.cpp, and wrote at the top of the page. There! Easy!
 * 1) include "Main.h"

Finished program?

Main.cpp

float a; float rnum; // the main entry point for the application is this function void DarkGDK ( void ) {	a=random(4); dbPrint(a); dbWait(10000);
 * 1) include "DarkGDK.h"
 * 2) include "Main.h"

// return back to windows return; }

float random(int seed) {	dbRandomize(seed); rnum=dbRnd(10000)/10000.0; return rnum; }

Main.h float random(int seed)

{

dbRandomize(seed);

rnum=dbRnd(10000)/10000.0;

return rnum;

}

Feel free to add/correct this.