Homework 4 (Gradanator) FAQ

Q: How do I use input? How do I do returns?

A: Look at the examples in the lecture slides. The BMI example is particularly good for understanding returns, with its get_bmi and bmi_for functions (and examining how they are called and used from main).

Q: I'm returning a variable, but I get a "cannot find symbol" error in main when I try to use the variable. Why can't main see my variable?

A: Just returning a variable from your function doesn't mean main now can refer to that variable. The variable's value is returned, not its name. The main function must store the result into a variable and then examine that variable. Instead of:

my_function_that_returns(param1, param2)

You may need to say something like:

result = my_function_that_returns(param1, param2)

Q: How do I return two things from a function?

A: You don't! You need to organize your program so you only need to return one thing from each function.

Q: But I have a function that reads / computes two important values. How do I get those values over to the other parts of my program?

A: There are several ways. One would be to have your function call the other function, passing the two important values as parameters, rather than trying to return the two values. Beware that this could lead to "chaining" of functions if you consider too many values as being "important". Maybe you don't need to return both the values; maybe one of them is more important than the other, so you can just return the more important one. Third, if the function is short, you might consider just putting that code in main and then having main pass the two important values to the rest of your program.

Q: How do I round a number to a certain number of decimal places?

A: See the 2nd page of the homework write-up. You may use the custom round function we used in several lecture examples.

Q: Can my main function contain print statements? Can it contain an if statement? How many lines long can it be? ...

A: You should not have print statements or read user input directly inside the main function. main should be short, but it should summarize the overall program and should not contain too large a share of your overall code. The main function should represent a reasonable summary of the overall program's execution.