You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
`func` is the key word of function `functionName` is the identifier or name of the function, the following `parameter1, parameter2, ...` is the parameter list of the function,`returnType`is optional, which is the returned type of the function. Function body is a series of sentences surrounded by `{}`. Here’s a example below:
The name of the function is `add`, it have two integer-type parameters `a` and `b`, return value is also integer type. Its function is add the two parameters and return them.
`return` is same as the `return` command in Minecraft, both used to return the returned value of the function. It’s grammar is `return expression;`, `expression` is an expression, it’s value is the returned value of the function.
If a function defined the type of the returned value, then each branch of it must have `return` sentence, so the function must would return a value. And the value that `return` sentence returned must have the same type as the type of return value of the function, or the subtype of the returned value’s type.
If a function haven’t defined the return value’s type, then the default is `void`, which won’t return any value. Now, `return` statement is still available, but its grammar becomes `return;`, without any expression. Its function is stop the function immediately.
In MCFPP, grammar of call function is the same as in C/Java
40
+
, that’s `functionName(parameter1, parameter2, ...);`. In it, the `functionName` is the name of function,`parameter1, parameter2, ...` are the parameters to the function, here’s the example:
40
41
41
42
```mcfpp
42
43
func test(){
43
-
print(add(1, 2)); #上面定义的add函数
44
+
print(add(1, 2)); #the add function defined before
In this example, `test` function called `add` function, and sent two parameters `1` and `2`. `add` function returns `3`, so the `test` function will print `3`.
48
49
49
-
## 函数的传参
50
+
## The parameter passing of function
50
51
51
-
函数中,对参数的修改不会影响到函数外部的变量。例如:
52
+
In the function, the change to the parameter won’t affect the variables out of the function. For example:
0 commit comments