- 2
Please explain this entire question to me
I'm lost here
2 ответов
+ 2
Methods are re-callable chunks of code. Arguments are things we pass into the method to have processed.
public int add(int x, int y){
return x+y;
}
This is a simple method. int x and int y are the arguments. when you call this method you send with it 2 integers. it assigns them to the variables x and y and runs the code accordingly. for instance
add(1,4);
This calls the add function, gives x a value of 1, gives y a value of 4. it returns x+y or in this case 1+4. so add(1,4); returns 5.
Arguments and return type are both optional. Here is another method that has no arguments or return type.
public void yell(){
Console.WriteLine("AAAHHHH");
}
notice it has void instead of a data type. This tells it not to expect a return. it also has no arguments. It is simply called with
yell();
That would print AAAHHH to the console. Same method WITH an argument..
public void yell(String x){
Console.WriteLine(x);
}
This one would call with a string argument.
yell("HELP!");
This would call the yell method, give string x a value of HELP!.. When the method ran it would print HELP! to the console
Does this help?
0
Methods...optional and named arguments question 1