16.12.2012 Views

z/OS V1R9.0 UNIX System Services Command ... - Christian Grothoff

z/OS V1R9.0 UNIX System Services Command ... - Christian Grothoff

z/OS V1R9.0 UNIX System Services Command ... - Christian Grothoff

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

c<br />

Each parameter on the first line can be a variable name or an array name. Array<br />

names are indicated by putting square brackets after them. For example, if cmpvec<br />

is a function that compares two vectors, the function definition might start with:<br />

define cmpvec(a[],b[]) {<br />

Parameters do not conflict with arrays or variables of the same name. For example,<br />

you can have a parameter named a inside a function, and a variable named a<br />

outside, and the two are considered entirely separate entities. Assigning a value to<br />

the variable does not change the parameter and vice versa. All parameters are<br />

passed by value. This means that a copy is made of the argument value and is<br />

assigned to the formal parameter. This also applies to arrays. If you pass an array<br />

to a function, a copy is made of the whole array, so any changes made to the array<br />

parameter do not affect the original array.<br />

A function may not need any arguments. In this case, the define line does not have<br />

any parameters inside the parentheses, as in:<br />

define f() {<br />

The auto statement declares a sequence of local variables. When a variable or<br />

array name appears in an auto statement, the current values of those items are<br />

saved and the items are initialized to zero. For the duration of the function, the<br />

items have their new values. When the function ends, the old values of the items<br />

are restored.<br />

However, bc uses dynamic scoping rules, unlike C which uses lexical scoping rules.<br />

See Usage notes for more details.<br />

For example:<br />

define addarr(a[],l) {<br />

auto i, s<br />

for (i=0; i < l; ++i) s += a[i]<br />

return (s)<br />

}<br />

is a function that adds the elements in an array. The argument l stands for the<br />

number of elements in the array. The function uses two local names: a variable<br />

named i and a variable named s. These variables are “local” to the function addarr<br />

and are unrelated to objects of the same name outside the function (or in other<br />

functions). Objects that are named in an auto statement are called autos. Autos are<br />

initialized to 0 each time the function is called. Thus, the sum s is set to zero each<br />

time this function is called. You can also have local arrays, which are specified by<br />

placing square brackets after the array name in the auto statement.<br />

define func_with_local_array() {<br />

auto local_array[];<br />

for(i=0; i

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!