On parameters for methods. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

On parameters for methods.

What's the difference between int Sum (int x, int y){example code} and int Sum () {int x; int y; example code} ? Are "int x" and "int y" being called from another method in the top example and being created in the bottom example? I feel like I'm missing something important.

12th Jul 2016, 5:26 AM
Greg
4 Answers
0
the first example x,y is the parameter of method the second x,y are just variables use them in that code blocks of your method and cannot be used out of it
12th Jul 2016, 7:07 AM
Hamied Amr
Hamied Amr - avatar
0
the first example x,y are the parameters of method the second x,y are just variables use them in that code blocks of your method and cannot be used out of it
12th Jul 2016, 7:08 AM
Hamied Amr
Hamied Amr - avatar
0
Well, I still feel like I'm not understanding how this parameter might restrict what the method is allowed to process (if it does restrict it). But thanks for the answers, I'm sure once I make mistakes and fix them I'll get to know it better.
13th Jul 2016, 12:53 AM
Greg
- 1
with that: int Sum (int x, int y){example code} that means, x and y will be coming from OUTSIDE the method block. while with: int Sum () {int x; int y; example code} that means x and y will only be coming INSIDE the method block. You may use this kind of initializing x and y if they are not really needed outside the method block. This two may produce similar output, depending on how you are going to manipulate that "example code" of yours. 😇
12th Jul 2016, 6:46 AM
Erwin Mesias
Erwin Mesias - avatar