Internet of Things
TinkerforgeCode in ActionAbout Me
  • Course Outline
  • 1 - Getting Started
    • Overview
    • Connect to the LED
    • Getting Started
      • Glitch
      • The Application Template
    • Concepts in Programming
      • What is Programming?
      • Variables
      • Functions and Commands
      • Control Structures
      • Loops
      • Objects and Libraries
    • Programming Simple Web Apps
    • Exercises
      • 1.1 Buttons and Inputs
  • 2 - Internet of Things
    • Overview
    • IoT in our Apps
      • Getting Started
        • Hardware Kit
        • Brick Viewer and Daemon
      • Connect to the Devices
        • The Tinkerforge Device Manager
      • Program the Devices
        • RGB LED
        • RGB LED Button
        • OLED Display
        • Sensors
          • Humidity Sensor
          • Ambient Light Sensor
    • Components and Use Cases
    • Exercises
      • 2.1 Lights and Buttons
      • 2.2 Sensors
      • 2.3 Display
  • 3 - Artificial Intelligence
    • Overview
    • AI in our Apps
      • Google's Teachable Machine
      • Face Recognition
      • Training a Custom Model
    • Rules vs. Learning
    • Learning from Data
    • Use Cases
      • Computer Vision
        • Image Classification
        • Handwriting Recognition
    • Machine Learning Algorithms
      • Artificial Neural Networks
      • Decision Trees
      • Logistic Regression
    • Exercises
      • 3.1 Rules vs. Learning
      • 3.2 Fruits and Vegetables
      • 3.3 Face Recognition
      • 3.4 A Classifier for Iris
  • 4 - Cloud & APIs
    • Overview
    • APIs in our Apps
    • Cloud and APIs
      • Weather API
      • NASA Open APIs
      • EDAMAM Nutrition and Recipes API
    • Push Notifications
    • Exercises
  • 5 - App Project
    • Overview
    • Summer 2021
    • Summer 2022
  • Appendix
    • Other Devices
      • Motorized Linear Poti
      • Sound Pressure Sensor
      • NFC Reader
      • Motion Detector
    • UI Features
      • Realtime Charts
      • Countdown Timer
    • Digital Computers
      • Overview
      • The Binary System
      • Code Systems
      • Logic Gates
      • Binary Addition
      • From Analog to Digital
    • Cheat Sheets
    • Projects
      • IoT @ Pickup-Boxes
Powered by GitBook
On this page
  • Goals
  • Why define functions?
  • How to define a function?
  • How to execute a function?
  • Commands are functions from somewhere else

Was this helpful?

  1. 1 - Getting Started
  2. Concepts in Programming

Functions and Commands

PreviousVariablesNextControl Structures

Last updated 4 years ago

Was this helpful?

Goals

  • You know what a function is and why they are useful.

  • You can define a function with arguments and return a value.

  • You know that a command is nothing else than a call to a function.

Why define functions?

In some cases, we want to define functions that we can reuse multiple times in our program. Why would we want to do that? There can be multiple reasons:

  • We want to keep our code as lean as possible. Functions allow us to remove redundant code.

  • We want to keep our code maintainable. If we make changes to a specific part of our code, we want to avoid making these changes in many places. A change to a function affects all occurrences where the function is used.

  • We want to structure our code. Functions are the first step to introducing a structure to our code. Most code editors can collapse functions to improve the code's readability.

How to define a function?

Consider the function definition from our program below:

function showInstructions() {
    log("Please connect your IoT development kit to your computer via USB and hit 'Establish Connection'!");
}

The keyword function tells our program that we want to define a function. The keyword is followed by a name that we can choose. This is very similar to the declaration of a variable. Following the name, we can specify one or more arguments for our function within the opening and closing brackets. In the example above, we do not specify any arguments. After the list of arguments we start the code block that makes up the function with an opening curly brace. To add a visual aid, we slightly indent the code that belongs to a block. This makes it easier to read and comprehend. Finally, we indicate the end of our function with the closing curly brace.

How to execute a function?

Once defined, we can type the name of the function and append opening and closing brackets to execute it. We could call the function from above like this:

showInstructions();

Our program looks up the code execute for that particular functions and runs it. In this case, it is only one line to print the instructions to the console. But the function could be much more complex and contain a complex program made up of many variable declarations, commands, loops, control structures, and even nested functions.

Read more about JavaScript functions under this link:

Commands are functions from somewhere else

We learned that the writeLog() command is actually a function. A function in turn is a block of code with a name. Optionally, a function can have one or more arguments that we can pass with every execution of the function. In most cases, a function returns a result when done executing. That said, there are functions that don't return a result but rather only execute a sequence of steps.

Let's look at the following line of our program from above:

writeLog("Welcome to Lesson 1.1: Connect to Devices. Follow the instructions below.");

When you look at the code in action, you notice that this line produces an output on the console (the text area on the website) with the exact content that is specified within the brackets and double quotes. We could substitute the text with anything else, and as a result, we'd see the new content being printed to the console. The writeLog() command allows us to print any arbitrary text to the console, and we can specify the text by passing it as an argument of the function. The general form of the command looks like this:

writeLog(text)

The argument text must be replaced with a specific value when using the command. As you can see by looking at the whole program above, we are using the writeLog() command in many places in our code. In all the cases, the value we print is different. However, the command is the same.

The writeLog() command is provided for us as a function from an external , and we don't need to know how the function behind it looks like (although we could look it up). We are only interested in what the command does for us: print something to the console.

Behind the scenes, the writeLog() command is a JavaScript : a block of code that does something for us, optionally based on an input that we can specify as an argument. We assign this code block a name (here: writeLog), and we can then call it when we need it, and it runs the code block.

library
function
Functions — reusable blocks of code - Learn web development | MDN
Functions — reusable blocks of code - Learn web development | MDN
Code editors such as Glitch and Visual Studio Code let the user collapse functions and hide their content.
Logo
Logo