Go Down

Topic: Structure of code. Need help. (Read 7 times) previous topic - next topic

lost_and_confused

I have asked, and been told/pointed to C sites, but I am not understanding what is written on these sites.

Partly because I think the layout of the code examples.

The lists seem to have the functions BEFORE the actual "program".

Indulge me please:

I don't know if this is helping or not but here are some questions with what I see as the problem/s:

I won't put it in "CODE" format, because it isn't really real code.

The main loop looks like:

VOID ROUTINE_I_WANT_TO_CALL()

To me, that means that the routine is called with no paramaters (why does this show as bad spelling?) and returns nothing.   Though obviously that would be somewhat silly.

ROUTINE_I_WANT_TO_CALL
{
    Things here which do what ever
}

Ok, the { starts the FUNCTION and } ends it.

If I want to send something to the routine - when it doesn't initially accept things, what do I do?
I get I do:
VOID ROUTINE_I_WANT_TO_CALL(what ever it is I want to send)

Now, another question:
I know it has been "explained" but I still can't get my head around it.
What is the difference between:
j = THE_ROUTINE()
and
j THE_ROUTINE()

I don't know if having anything between the ()'s in either would change anything.


AWOL

#1
Jun 09, 2012, 11:35 am Last Edit: Jun 09, 2012, 11:38 am by AWOL Reason: 1
Quote
paramaters (why does this show as bad spelling?) a

Hmm, let me think...

It really would be better if you kept it to actual code examples; your pseudocode is a long way from C syntax, it is hard to understand.

For instance,  it is hard to decide what you mean by
Quote
j THE_ROUTINE()
"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.

majenko

When you create a function you specify both the type of data you want to work with and the name of the variables to store that data.

For instance:

Code: [Select]

void myFunction()
{
 ...
}


creates a function that takes no data and receives no data, and:

Code: [Select]

int myFunction(float myVar)
{
  return myVar*1000;
}


creates a function which takes a single value that is a floating point number, and returns a value which is an integer number.  The value you send to the function is stored in the variable "myVar".  (In this case the function returns the number you sent multiplied by 1000).

When calling the function you have the option of assigning the results of the function (the value "return"ed) into a variable.  For example:

Code: [Select]

int result;

result = myVar(13.442);


Using the second function we defined above, "result" would now contain 13442.

Note that the data type of "result" (int) matches the return type of the function (int).

In C everything happens inside functions (well, not including global variable declarations).  The program always starts by running the "main()" function.  The Arduino system writes this function for you.  That function first calls the "setup()" function, and then repeatedly calls the "loop()" function.

The order the functions are in within your program is of no importance.  Normally it does matter, because a function has to be defined before it can be used, so traditionally all functions go before the "main()" function, but the Arduino compilation system fudges it all around so it doesn't matter what order you write the functions in.
Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

lost_and_confused

AWOL,

This has been said before.

I am learning and now can explain why I can't/don't.

The problem is that I don't always know what is relevant and what isn't.  Yes that contributes to the problem, but from my point of view, it is silly/confusing putting things in which I don't understand.

AND!

People reply with stuff which is WAY beyond me understanding.

I luckily worked out some problems by sheer bashing my head against the wall.

Sometimes there are parts of the code/program which have things I want.  But I don't know how to get those "names" to another part of the code.

That was PARTLY covered by my first couple of examples.

But it is so confusing for me.

I guess there is middle ground but the terms used are too "in the know" for me and how they are formatted.


lost_and_confused

Here is a bit of code on which I am stuck:

Code: [Select]

class alarm_clock
{
public:
  alarm_clock(boolean noo);
  byte run(); // Returns which alarm is triggered, 0-Max_alarms, 255 means no alarm is triggered.
  byte set_alarm(byte alarm_num, byte hr, byte mnt, byte dow); // Returns 255 if parameters are invalid. Returns 0 if valid.
  void turn_on(byte alarm_num);
  void turn_off(byte alarm_num);
  void alarm();
  boolean within(byte a, byte dow);
 
  typedef struct {
  byte hr;
  byte mnt;
  byte dow;
  boolean on_off; // This variable is not used. Instead dow is used to turn the alarm off or on.
  }
  entry;

  entry alarms[Max_alarms];
  boolean alarm_is_on; // Alarm is on. alarm() will be called if this is true.
  byte snooze;
};
#endif



byte run(); // Returns which alarm is triggered, 0-Max_alarms, 255 means no alarm is triggered.


Excuse the "language" but: What the....?

I would like to get a variable with which alarm number is active available in the main program.
I want to put a symbol on the screen when an alarm is set as well as another when an alarm is active.
A number which is unique to the alarm - which the program gives - would be handy.

Go Up