Posts Tagged ‘ helper methods ’

Creating and calling simple helper methods

Methods are named blocks of code. That means, you can call methods by name and their code blocks will execute. But how can I call them? And can I pass values into a method or retrieve values from a method? Why would I even want more than one block of code?

First thought coming into my mind is that if I find myself repeating some code, I want to make that code easier. I don’t want to type the same code twice or even more. So I put that in a block of code, a method. Having less code makes it easier to find mistakes (assuming I make mistakes), because it is well-arranged.

An example for a simple helper method is a button click event handler, that triggers the code inside that block whenever a button is clicked.

As you can see I have my Click Event Handler, in which I reference and call my method (superFormula). It only executes after calling it..

Let’s say, you want to be addressed directly, instead of reading just the words “Hello World”. By putting a certain variable, a parameter in your method, that can be achieved. In my example that parameter would be “myName”. I am assigning the value of that variable to ”Andrea”:

Of course, this doesn’t look too exciting, but think about it. You could set that value to any name, and no additional coding would be necessary.

To be continued…

Advertisements
%d bloggers like this: