Previous page Next page Navigation bar

Calculator Programming Tutorial

Programming Building Blocks I

Subprograms

Subprograms in JavaScript

JavaScript categorizes all subprograms as functions. The syntax for defining a subprogram is:

function functionname (param1, param2, ...) block

where functionname is the name of the function; param1 is the name by which the first parameter will be known in the body of the function, param2 is the name by which the second second parameter will be known in the body of the function, and so forth; and block is a block statement specifying the computations to be performed in the subprogram. In this syntax description, an ellipsis (...) indicates the preceding element may be repeated as many times as necessary. If the function has no parameters, opening and closing parentheses to indicate this follow the function name. The block may contain variable declarations for variables used only within the subprogram.

The syntax for invoking a function is:

functionname (argument1, argument2, ...)

where functionname is the name of the function; and argument1 is the first parameter to the function, argument2 is the second parameter to the function, and so forth. If the function has no parameters, opening and closing parentheses follows the function name.

There is a subtle distinction between the meanings of the word “parameter” at the invoking site and at the definition site — that is, where the function is used and where the function is defined. At the invocation site, the parameter can be any expression. This expression is evaluated in the context of the invocation site, and so can use variables, other functions, and so forth, known at the invocation site. This expression is known as the actual parameter. However, the expression’s value is then assigned to a variable known only within the function, and this is called the function’s formal parameter. The function can treat the formal parameter as a variable, using its value and assigning new values to it. These new values, however, disappear when the function ends — as do values assigned to other variables defined within the function.

In JavaScript, arguments are presented to the subprogram using the parameter with the same position in the argument list. Thus, for example, if a JavaScript function takes several arguments, the order of these arguments is important: the first argument is available to the function as the first parameter, the second argument is available as the second parameter, and so forth.

A value can be returned by a function with the return statement:

return expression;

where expression is the expression yielding the value to return.

The following is a sample absolute value function in JavaScript:

function AbsoluteValue (x)
{
    if (x < 0)
    {
        x = -x;
    }
 
    return x;
}

This example shows using the argument as a local variable, and the return statement, but has little else to recommend it. In particular, the function Math.abs(x) is JavaScript’s standard absolute value function, and should be preferred.

[ Previous page | Top of page | Next page ]

Previous page Top of page Next page Navigation bar

Copyright © 2001 Brian Hetrick
Page last updated 30 December 2001.

Brian’s Casio Calculator Corner

Home

Programs

Tutorial

Preface

Introduction

Fundamentals

Building Blocks I

Introduction

Comments

Data Types

Numbers

Variables

Expressions

Control Flow I

Control Flow II

Subprograms

Introduction

JavaScript

Visual Basic

Calculator

Basic I/O

Algorithms

A First Program

Examples

Exercises

Answers

Modularization

Data Structures I

Recursion

Program Attributes

Building Blocks II

Algorithm Analysis

Structuring

Data Structures II

Abstract Types

Objects

Problem Analysis

Reference Card

References

Puzzles

Site Information

Your Privacy

Site Map

E-mail

Site Technical Data