My course instructor told us in a previous lesson not to use one function for two tasks. A few lessons later, the code we are reviewing has functions in it that perform two tasks. I'm brand new to Stack Overflow so I'm hoping this is the right place to ask
Maybe a function can handle two tasks but it isn't a good practice to do so?
Thanks in advance for any advice/help
Obviously I can create another function to handle printing out the output but I didn't know what was proper.
Here's the code
Код: Выделить всё
public static void main(String[] args) {
calculateArea(length, width);
calculatePerimeter(length, width);
}
public static void calculateArea(double param1, double param2) {
// Local variable to store the area (accessible only within this function)
double area = param1 * param2;
System.out.println("Area of the rectangle: " + area);
}
public static void calculatePerimeter(double param1, double param2) {
// Local variable to store the perimeter (accessible only within this function)
double perimeter = 2 * (param1 + param2);
System.out.println("Perimeter of the rectangle: " + perimeter);
}
Код: Выделить всё
public class ReturnValuesNew {
public static void main(String[] args) {
calculateArea(2.3, 3.6);
calculateArea(1.6, 2.4);
calculateArea(2.6, 4.2);
}
public static void calculateArea(double length, double width) {
double area = length * width;
System.out.println("Area: " + area);
}
}
Источник: https://stackoverflow.com/questions/781 ... -two-tasks
Мобильная версия