# global

## Purpose

Define global variables.

## Synopsis

    global X Y Z

## Description

`global` `X` `Y` `Z` defines `X`, `Y`, and `Z` as global in scope.

Ordinarily, each MATLAB function, defined by an M-file, has its own local variables, which are separate from those of other functions, and from those of the base workspace and nonfunction scripts. However, if several functions, and possibly the base workspace, all declare a particular name as global, then they all share a single copy of that variable. Any assignment to that variable, in any function, is available to all the other functions declaring it global.

Stylistically, global variables often have long names with all capital letters, but this is not required.

## Examples

Here is the code for the functions `tic` and `toc`, which manipulate a stopwatch-like timer. The global variable `TICTOC` is shared by the two functions, but it is invisible in the base workspace or in any other functions that do not declare it.

    function tic
    %TIC    Start a stopwatch timer.
    %    The sequence of commands
    %        TIC
    %        any stuff
    %        TOC
    %    prints the time required.
    %    See also: TOC, CLOCK.
    global TICTOC
    TICTOC = clock;
    function t = toc
    %TOC    Read the stopwatch timer.
    %    TOC prints the elapsed time since TIC was used.
    %    t = TOC; saves elapsed time in t, does not print.
    %    See also: TIC, ETIME.
    global TICTOC
    if nargout < 1
        elapsed_time = etime(clock,TICTOC)
    else
        t = etime(clock,TICTOC);
    end

## See Also

    clear, isglobal, who

(c) Copyright 1994 by The MathWorks, Inc.