Discussion:
Style questions
(too old to reply)
Alexander Smith
2004-01-21 21:42:36 UTC
Permalink
(This may be a duplicate post. I apologize if it is. I forgot to add a
subject line to the last one, and I'm not sure if it'll get posted
correctly because of that.)


The following are some questions I was asked about lab 1, relating mostly
to style. Because the answers are probably useful to everyone, I have
1)Do you want lines separating the different functions, and the different
section parts (declaring, initializing etc.) in the lab, or NO empty lines
at all?
Yes, blank lines are a good idea. Leave one or two blank lines between
functions. It is also a good idea to use blank space inside your functions
to improve clarity to group related bits of code together, etc. Take a
look at numint_main.c and guess_main.c for examples.
2) do you want header declarations for the helper functions I write, or is
defining a function at the top before it is called by main() allowed?
Yes. All of your helper functions should have declarations near the top of
the file.
3) how much error checking do you want?
You should do a "reasonable" amount of error checking. Ok, I suppose that
answer's not very helpful. More to the point, you will never be penalized
for too much error checking. On the other hand, for lab 1, you can assume
valid input. See my newsgroup post "Re: Input from automarking", Jan 21
for more details.
4) if I have an if statement with ONLY ONE line inside it do I need to use
curly brackets or will you accept no brackets?
It is ok to omit the curly braces in this case.
5) is it alright if I name my non main function parameters x, y for example,
and in the main function also create x, and y variables, and pass them into^Z
different names?
ie... in Main function: int x=0,y=0;
evaluate(x,y);
I'm not sure I understand the question. Function parameters do not have to
have the same name as variables passed in, but it's also ok if they do.
There's really no relation between function parameter names, and the names
of variables in other functions. For example, the following is fine:

void evaluate(int x, int y)
{
/* Do something here... */
}

int main(void)
{
int x, y, a, b;
/* Assign some values to x, y, a, and b. */
evaluate(x, y);
evaluate(y, x);
evaluate(a, b);
evaluate(b, 123);

return 0;
}

Whatever value you pass into the first parameter of the function
"evaluate" is going to be called "x" inside the evaluate() function.
Similarly, whatever you pass into the second parameter is going to be in
"y" inside evaluate(). The "x" and "y" in the evaluate() funciton have
absolutely no relation to the "x" and "y" in main() - they're two
different functions.


Alexander
Diego Huang
2004-01-23 23:22:27 UTC
Permalink
Post by Alexander Smith
2) do you want header declarations for the helper functions I write, or is
defining a function at the top before it is called by main() allowed?
Yes. All of your helper functions should have declarations near the top of
the file.
What about files that don't contain the main function? Like, numint.c. If
I make a pow() function myself, I should write the prototype in the
numint.h file. But since we don't have to submit the .h files for this
lab, do I still need to write the prototypes in the numint.c file or can I
just implement the function pow before I use it in numint.c file?

Thanks,

Diego
Moayad Fahim Ali
2004-01-24 07:17:05 UTC
Permalink
You can declare the functions you're implementing and using in numint.c
near the top of the file (numint.c). You can not edit numint.h.
Post by Diego Huang
Post by Alexander Smith
2) do you want header declarations for the helper functions I write, or is
defining a function at the top before it is called by main() allowed?
Yes. All of your helper functions should have declarations near the top of
the file.
What about files that don't contain the main function? Like, numint.c. If
I make a pow() function myself, I should write the prototype in the
numint.h file. But since we don't have to submit the .h files for this
lab, do I still need to write the prototypes in the numint.c file or can I
just implement the function pow before I use it in numint.c file?
Thanks,
Diego
Continue reading on narkive:
Loading...