Go Down

Topic: WAY OFF TOPIC, but I would like some help getting my head around this. (Read 2 times) previous topic - next topic

jraskell

As Graynomad mentioned, this is the biggest hurdle in converting requirements into code.  Requirements can be of varying levels of ambiguity.  There is zero ambiguity in code.

In this case, the phrase 'into the forest' needs to be more specifically defined.  Without that definition,  any answer is just an interpretation that may or may not be correct.  It could be half way, it could be all the way.  It could be something else entirely if 'into the forest' has some non-intuitive meaning.  For example, it could just as easily mean a single step if 'into the forest' is simply a binary state.

Graynomad

#11
Sep 28, 2012, 04:41 pm Last Edit: Sep 28, 2012, 04:44 pm by Graynomad Reason: 1
Re declaration and definition, I think you'll have to hit the books but I found this on the web.

Quote
In The Definition of a variable space is reserved and some initial value is given to it, whereas a declaration only identifies the type of the variable for the function. Thus definition is the place where the variable is created or assigned storage whereas declaration refers to places where the nature of the variable is stated but no storage is allocated.


I admit I get them confused as well but the following is right I think

Code: [Select]
void myFunc(void);    // declaration of myFunc() so the compiler knows what to expect if the function is referenced before it is defined
//
// lots of other code
//
void myFunc(void) {   // definition if myFunc, this is the bit that actually has the code
   // some code
}


You need the declaration if you have a forward reference to a function for example

Code: [Select]
loop () {
   myFunc();
}

void myFunc(void) {   
   // some code
}


Will cause an error because the compiler sees the reference to myFunc() before it knows what the heck it is. One way to fix this is to declare the function first.

Code: [Select]
void myFunc(void);   // this is also called a "prototype"

loop () {
   myFunc();
}

void myFunc(void) {   
   // some code
}


The declaration (AKA prototype) tells the compiler what myFunc() is so when it sees the reference in the loop() function it's happy in the knowledge that myFunc() exists, the call is correct WRT parameters and return type, and that at some point myFunc() will be defined.

You can also fix this by simply placing myFunc() before any code that references it but often this is not practical.

The Arduino IDE pulls a trick to help beginners with this. It scans the program for forward references and (I think) inserts declarations for you.

______
Rob
Rob Gray aka the GRAYnomad www.robgray.com

AWOL

And if a tree falls and there's only the dog in there, does it make a sound?
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

lost_and_confused

Ah, the fun of programming.

Ok, again and more off topic, here is an e-mail which I just received which is sort of quaint and long these lines:



There was a bit of confusion at the Sporting goods store this morning. When I was ready to pay for my purchases of gun powder and bullets the cashier said, "Strip down, facing me."
Making a mental note to complain to Harper about the gun registry people running amok, I did just as she had instructed.
When the hysterical shrieking and alarms finally subsided, I found out that she was referring to my credit card.
I have been asked to shop elsewhere in the future.
They need to make their instructions to us seniors a little clearer!


Ya gotta laugh.

Coz if you didn't you would cry.

Graynomad

Probably most of English comedy is based on assumptions and ambiguities. As a programmer I tend to take what someone says literally, as a normal person I usually interpret but I have a habit of doing literally what my missus says just for fun.

For example she may hand me some rubbish and say "Take this out to the bin and throw it out". 3 minutes later I return with the rubbish having thrown the bin out.

She laughs and laughs...sometimes.

______
Rob

Rob Gray aka the GRAYnomad www.robgray.com

Go Up