What is a Method (function)?

Share This Post

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:

[code lang=”js”]
// 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>
[/code]

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.

[code lang=”js”]
// 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>
[/code]

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

Subscribe To Our Newsletter

Get updates and learn from the best

More To Explore

Code

Curriculum Innovation or Confusion

It is an early Monday morning just prior to the beginning of the semester.  A faculty member walks into the institution where he teaches.  He

Code

Creating a Promise

Promises are used for asynchronous code. It is a temporary holder for a value that gets returned by an asynchronous call. For example, a call

Do You Want To Boost Your Business?

drop us a line and keep in touch

Questions?Have questions? Ask away!
Scroll to Top
small_c_popup.png

Need help?

Let's have a chat...


Login

Jump Back In!

Here at Webolution Designs, we love to learn. This includes sharing things we have learned with you. 

Register

Begin Your Learning Journey Today!

Come back inside to continue your learning journey.