0
Methods with parameters and return Vs. void()???
Is there any benefits about using methods with parameters (arguments) and return? It seems more complicated to me than using void() without returning anything and in both ways we can get the desired result. Am I missing something?
1 Réponse
+ 2
Imagine that you have a program which generates multiple number of objects, let’s say rectangular prisms. Each prism has its parameters (side lengths a, b and c) and volume a*b*c. During the runtime of the program you want to take any 2 prisms and compare it’s volumes and decide which is bigger. Hence you will have class method Volume which will return you the volume of each prism.
You can define the volume as a parameter of the prism but you have to take into consideration the memory management during the runtime and if you will have 2 millions of prisms its better to calculate the volume only when needed and not to have them all stored in the memory.