What is a Method (function)?

In object oriented programming (OOP), we create methods/functions to carry out our coding tasks. By definition, a method refers to a function that is encased within a class. All modern programming languages have the ability to do this. C#, Java, CF and even JavaScript. [Side note: JS handles things a tad differently in that it uses a keyword, prototype, to mark classes. Perhaps I will cover that in a future post.]

So how do functions work? I will give a JS example, but the basic idea applies to a lot of languages.

So, imagine I might build a simple function as follows:


// function is distinguished by the function keyword...it tells JS, "Hey, here comes a function!" 

// The function keyword is followed by the name of the function...helloWord() 

// Notice that we use what is called camel case. In camel case, everything is lower case except the first letter of the second word. 

function helloWorld() {

   // return - a keyword that stops the function from running and returns data to the place from where the function has been called.

   return 'Hello, World!'; 

} 

// Now that we have the function, we can call it from other places in our code to have it run... 

// For example, I can place it in an onClick event (more on events in a future post). 

// When the end user clicks on the item, the function will run and, in this case, it will return the test, "Hello. World!" 

<button onclick="helloWorld();">Click me</button> 

Now, notice the empty parentheses after the function. The aforementioned function takes no arguments/parameters. We can build functions that pass data to our functions that we can then use within our functions. Let’s rewrite our previous function to take a name.


// We are going to be passing an argument called userName to our function. This is an argument to hold data for our function. 

// This argument returns a string greeting and puts a name in the return output. 

function helloWorld(userName) {

   // return - a keyword that stops the function from running and returns data to the place from where the function has been called.

   return 'Hello, ' + userName + '!'; 

} 

// Here we have the same event, but we are passing the name, Clay to the function. 

// The data, 'Clay', gets placed within the function argument entitled, userName. 

<button onclick="helloWorld('Clay');">Click me</button>

As you can see we can build functions that will take data we send it and we can then use that data within our function. This allows us to build methods/functions that can be used over and over again. This is an OOP principle known as ‘DRY’, or Don’t Repeat Yourself. Without the use of functions, we would probably have to repeat our code several times in our applications leading to mistakes and maintenance issues.

So use functions/methods…they are a great way to encapsulate and organize your code.

Happy Coding!

Clay

Leave a Reply

Your email address will not be published. Required fields are marked *