Вход/Регистрация
UNIX — универсальная среда программирования
вернуться

Керниган Брайан Уилсон

Шрифт:

if (x < 0) {

 print(y)

} else {

 print(z)

}

.ft

.DE

In the second example, the braces are mandatory:

the newline after the

.I if

would terminate the statement and produce a syntax error were

the brace omitted.

.PP

The syntax and semantics of @hoc@

control flow facilities are basically the same as in C.

The

.I while

and

.I if

statements are just as in C, except there are no @break@ or

@continue@ statements.

.NH

Input and Output: @read@ and @print@

.PP

.ix [hoc] [read]~statement

.ix [hoc] [print]~statement

The input function @read@, like the other built-ins,

takes a single argument. Unlike the built-ins, though, the argument

is not ал expression: it is the name of a variable.

The next number (as defined above) is read from the standard input

and assigned to the named variable.

The return value of @read@ is 1 (true) if a value was read, and 0 (false)

if @read@ encountered end of file or an error.

.PP

Output is generated with the ©print© statement.

The arguments to @print@ are a comma-separated list of expressions

and strings in double quotes, as in C.

Newlines must be supplied;

they are never provided automatically by @print@.

.PP

Note that @read@ is a special built-in function, and therefore takes

a single parenthesized argument, while @print@ is a statement that takes

a comma-separated, unparenthesized list:

.DS

.ft CW

while (read(x)) {

 print "value is ", x, "\n"

}

.ft

.DE

.NH

Functions and Procedures

.PP

Functions and procedures are distinct in @hoc@,

although they are defined by the same mechanism.

This distinction is simply for run-time error checking:

it is an error for a procedure to return a value,

and for a function @not@ to return one.

.PP

The definition syntax is:

.ix [hoc] function~definition

.ix [hoc] procedure~definition

.DS

.I

.ta 1i

function: func name stmt

procedure: proc name stmt

.R

.DE

.I name

may be the name of any variable \(em built-in functions are excluded.

The definition, up to the opening brace or statement,

must be on one line, as with the

.I if

statements above.

.PP

Unlike C,

the body of a function or procedure may be any statement, not

necessarily a compound (brace-enclosed) statement.

Since semicolons have no meaning in @hoc@,

a null procedure body is formed by an empty pair of braces.

.PP

Functions and procedures may take arguments, separated by commas,

when invoked. Arguments are referred to as in the shell:

  • Читать дальше
  • 1
  • ...
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • ...

Ебукер (ebooker) – онлайн-библиотека на русском языке. Книги доступны онлайн, без утомительной регистрации. Огромный выбор и удобный дизайн, позволяющий читать без проблем. Добавляйте сайт в закладки! Все произведения загружаются пользователями: если считаете, что ваши авторские права нарушены – используйте форму обратной связи.

Полезные ссылки

  • Моя полка

Контакты

  • chitat.ebooker@gmail.com

Подпишитесь на рассылку: