Alexander Smith
2004-01-21 21:42:36 UTC
(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
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.
the file.
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.
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
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 betweensection parts (declaring, initializing etc.) in the lab, or NO empty lines
at all?
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 ofdefining a function at the top before it is called by main() allowed?
the file.
3) how much error checking do you want?
You should do a "reasonable" amount of error checking. Ok, I suppose thatanswer'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.curly brackets or will you accept no brackets?
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 toand 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);
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