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>
[/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

More To Explore

Code

The Contact Picker API: Fast, private access to a user’s address book

The Contact Picker API lets mobile web apps open the native contacts chooser so users can share exactly the fields you request—like name, phone, email, address, or avatar—without granting blanket access to their entire address book. It’s user-initiated, privacy-preserving, and perfect for speeding up invites and form fills.

Focused software developer debugging code on multiple screens in dark environment
Code

Mastering the Console API: A Developer’s Best Debugging Friend

Ever find yourself relying solely on console.log() for debugging? You’re missing out on the Console API’s full potential. This powerful debugging toolkit offers specialized methods for timing operations, inspecting complex objects, tracing function calls, and organizing your debugging output. In just minutes, you can elevate your troubleshooting skills from basic to professional-grade. Let’s explore how the Console API can transform your development workflow and help you solve bugs more efficiently than ever before.

Share This Post

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.