Go (Golang) / Go Basics

Function Usage

This tutorial focuses on functions, which are not a part of HTML itself but are used in JavaScript to add dynamic functionality to HTML pages. You'll learn how to declare and call…

Tutorial 3 of 4 4 resources in this section

Section overview

4 resources

Explores the foundational concepts in Go, including variables, data types, and operators.

1. Introduction

Goal of the Tutorial

In this tutorial, we are going to learn about functions in JavaScript. Functions are one of the fundamental building blocks in JavaScript, allowing us to write reusable code.

Learning Outcomes

By the end of this tutorial, you will be able to:
- Understand what functions are and why they are useful.
- Declare and call functions.
- Understand the concept of function parameters and arguments.
- Write your own custom functions in JavaScript.

Prerequisites

Basic knowledge of JavaScript syntax and HTML would be beneficial.

2. Step-by-Step Guide

What are Functions?

Functions are reusable blocks of code that perform a specific task. They are only executed when 'called' or 'invoked'.

Declaring a Function

You can declare a function using the function keyword, followed by a name you give the function, and then a pair of parentheses (). The code to be executed is placed within curly brackets {}.

function myFunction(){
  // code to be executed
}

Calling a Function

To execute the function, you 'call' it by using its name followed by parentheses.

myFunction();  // This will execute the code in myFunction

Function Parameters and Arguments

Functions can also take inputs, known as parameters. When you call the function, you provide the values (known as arguments) for these parameters.

function myFunction(a, b){   // 'a' and 'b' are parameters
  // code to be executed
}

myFunction(5, 3);   // 5 and 3 are the arguments

Best Practices

  • Give your functions descriptive names.
  • Keep your functions small and focused on a specific task.
  • Always declare your variables with var, let, or const.

3. Code Examples

Let's look at some examples:

Example 1: A Simple Function

Here, we declare a function named greet that logs 'Hello, world!' when called.

// Declaring the function
function greet(){
  console.log('Hello, world!');
}

// Calling the function
greet();   // Outputs: 'Hello, world!'

Example 2: Function with Parameters

This function takes two parameters and logs their sum.

// Declaring the function
function addNumbers(a, b){
  console.log(a + b);
}

// Calling the function
addNumbers(5, 3);   // Outputs: 8

4. Summary

In this tutorial, we learned about functions in JavaScript. We now understand how to declare and call functions, use function parameters and arguments, and best practices when using functions.

Next steps

Consider learning about different types of functions in JavaScript such as arrow functions and self-invoking functions.

Additional resources

5. Practice Exercises

Try these exercises to practice what you've learned:

  1. Write a function that multiplies two numbers and logs the result.
  2. Write a function that accepts a name as a parameter and greets the person.

Solutions

1.

function multiplyNumbers(a, b){
  console.log(a * b);
}

multiplyNumbers(4, 5);  // Outputs: 20

2.

function greetPerson(name){
  console.log('Hello, ' + name + '!');
}

greetPerson('John');  // Outputs: 'Hello, John!'

Keep practicing and exploring more about JavaScript functions. Happy coding!

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

JavaScript Minifier & Beautifier

Minify or beautify JavaScript code.

Use tool

Word Counter

Count words, characters, sentences, and paragraphs in real-time.

Use tool

Timestamp Converter

Convert timestamps to human-readable dates.

Use tool

Backlink Checker

Analyze and validate backlinks.

Use tool

JSON Formatter & Validator

Beautify, minify, and validate JSON data.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI in Public Safety: Predictive Policing and Crime Prevention

In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…

Read article

AI in Mental Health: Assisting with Therapy and Diagnostics

In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…

Read article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help