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>

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>

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!


More To Explore


Demystifying Scrum User Stories Confirmation: Ensuring Quality and Collaboration

One of the key elements of Scrum is the use of user stories to define the requirements of a system or feature from the perspective of the end user. As teams work through the product backlog, it becomes crucial to confirm the user stories to ensure they meet the desired criteria and are ready for development. In this blog post, we’ll explore the concept of Scrum user stories confirmation and its significance in delivering high-quality products.


The Power of Conversations in Scrum User Stories

At the heart of Scrum lies the concept of user stories, which serve as a means of capturing requirements from the perspective of end-users. However, the true power of user stories lies not just in their written form but also in the conversations that take place around them. In this blog post, we will explore the significance of conversations in Scrum user stories and how they contribute to the success of Agile projects.

Do You Want To Boost Your Business?

drop us a line and keep in touch

Scroll to Top

Need help?

Let's have a chat...


Jump Back In!

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


Begin Your Learning Journey Today!

Come back inside to continue your learning journey.