官术网_书友最值得收藏!

Functions

Opa is a functional language. One of its features is that functions are regular values, which means a function may be passed as a parameter or returned as a result. As such, they follow the same naming rules as any other value.

function f(x,y){      // function f with the two parameters x and y
  x + y + 1
}
function int f(x,y){  // explicitly indicates the return type
  x + y + 1
}

Last expression return

You may notice that there is no return inside the body of a function. That's because Opa uses last expression return, which means the last expression of a function is the return value. For example:

function max(x,y){
  if(x >= y) x else y
}

If x is greater than or equal to y, then x is the last expression and x will be returned; if y is greater than x, then y is the last expression and y will be returned.

Modules

Functionalities are usually regrouped into modules; for example:

module  M {
  x = 1
  y = x
  function test(){ jlog("testing") }
}

We can access the content of a module by using the dot operator (.); for instance, M.x, M.y, and M.test. Actually, the content of a module is not field definitions, but bindings. In this example, we bind integer 1 to variable x, and bind the value of variable x to variable y.

主站蜘蛛池模板: 乌拉特中旗| 抚顺市| 舟曲县| 洱源县| 英德市| 万全县| 璧山县| 赣榆县| 吴忠市| 侯马市| 施甸县| 奇台县| 文水县| 颍上县| 瑞金市| 万年县| 郧西县| 聂拉木县| 无锡市| 内丘县| 云安县| 东乡县| 瓮安县| 山西省| 仁化县| 满城县| 兴城市| 织金县| 班戈县| 九江市| 仁怀市| 贡嘎县| 芦溪县| 大足县| 景洪市| 安岳县| 泾源县| 武鸣县| 扶绥县| 涞水县| 新化县|