Last baby steps
Save the best for last! Time for the real awesome stuff: methods (aaahh..)! The last post of baby steps :o
A method is an certain order of statements that you yourself have defined. Methods can take arguments and also return argument. Like a recipe! Here's how you can make a method:
Modifier (optional): Google it :D
Return type: the data type of the value your method should return
Method name: (aduh) it's also important to give your methods good names!
Parameter list (optional): the arguments your method should take, arguments should be separated using commas and the whole list of arguments should be inside (), if you don't use any arguments than you should still put ()
Method body: an order of statements that the method will execute, this order should be between { and }
returnType methodName (parameterList)
{
// Method Body
}
Example:
int addOneToGivenValue(int givenValue)
{
givenValue++; return givenValue;
}
The return statement returns a value. It also ends the method so if you would write any statements underneath it they wouldn't execute (there are some exceptions like when your return statement is inside a special statement but you'll find out about those later).
Now an example in which the above method is used.
int numberOne = 1; int numberTwo;
numberTwo = addOneToGivenValue(numberOne);
But wait, what if I'm not feeling generous and I don't want to return anything in my method? Then you should declare your method with the Return Type void. Here's another example method:
void sayHello()
{
Console.WriteLine("Hello");
}
You can use methods as often as you like. You can even call method inside methods.
In the method body I used //.
// stands for comments. You can write anything after this line and your program will ignore it. You can also use */ */.
// ignores this but not this
/* ignores all of this */ but not this
Use comments to make your code understandable, to remind yourself of stuff to do or other useful stuff!
Now that you know all this go put it into practice and combination with Console.WriteLine(); !!!!
That was the last post of baby steps! If this has kept your interest so far than go find tutorials online or buy yourself some books about programming and learn about loops, if statements, switch statements, arrays, classes etcetera! There are more than 150 skills to acquire :D









