Ask your own question, for FREE!
Computer Science 8 Online
kekeman:

Your task is to write a function that finds the smallest number in a list of numbers. What is the best way to declare a temporary variable and why? Declare it as a local variable because it makes it easier to read the code. Declare it as a local variable because using less memory is preferred. Declare it as a global variable since it is easier to comment and faster to read. Declare it as a global variable since these variables translate easier into machine code.

XxXNessalulbaddieXxX:

do u have a screen shot of this or?

SmokeyBrown:

To answer this question, we want to understand how memory is allocated for Local and Global variables. A Global variable is one that can be accessed throughout an entire program, while a Local variable is one that can only be accessed for a specific function call. Because of this, a Global variable will have to allocate memory as long as the program is running, while a Local variable will only need to take up memory whenever the function it is part of is called. Since the question specifies you are setting up a temporary variable, we know it does not have to exist throughout the entire program. So, we would want to declare a local variable, not a global variable. A global variable *could* still work, but it is not necessary; the reason a local variable is preferable in this case is that you only need the variable for a very specific part of the program, and memory is saved by not needing it to exist through the entire program. I hope that helps!

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!