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:

[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

computer, laptop, work place-2982270.jpg

Unlocking Wireless Communication: A Dive into the Bluetooth API

Wireless communication has become an integral part of our daily lives, and Bluetooth technology is at the forefront of this revolution, enabling devices to exchange data over short distances and creating a world more interconnected than ever before. At the heart of this technology lies the Bluetooth Application Programming Interface (API), a powerful tool for developers looking to harness the capabilities of Bluetooth in their applications. In this blog post, we’ll explore what the Bluetooth API is, how it works, and the possibilities it opens up for innovation in wireless communication.

lighthouse, beacon, atlantic-8578318.jpg

Understanding the Beacon API: Simplifying Asynchronous Data Transfers

In today’s data-driven world, web applications often need to send data back to the server. Traditionally, this has been done using AJAX requests or similar methods. However, these techniques can come with a cost, especially when dealing with data that needs to be sent during the unload phase of a document, such as tracking and diagnostic data. This is where the Beacon API shines by allowing developers to send data to a server more reliably and efficiently.

Share This Post


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.