Files
spl/stage1/test12_functions.spl

56 lines
985 B
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/* ===== 模块6函数 =====
* test12_functions — 函数调用
* 难度2/5
* 验证点:函数定义与调用、参数传递、返回值、多个参数
*/
fn add(a: i32, b: i32) i32 {
ret a + b;
}
fn sub(a: i32, b: i32) i32 {
ret a - b;
}
fn mul(a: i32, b: i32) i32 {
ret a * b;
}
fn identity(x: i32) i32 {
ret x;
}
fn addr(x: *i32) *i32 {
ret x;
}
fn main() i32 {
/* 函数调用 */
var r1 := add(3, 4);
if r1 != 7 { ret 1; }
var r2 := sub(10, 3);
if r2 != 7 { ret 2; }
var r3 := mul(6, 7);
if r3 != 42 { ret 3; }
/* 函数嵌套调用 */
var r4 := add(mul(2, 3), sub(10, 4));
if r4 != 12 { ret 4; }
/* 函数返回值的传递 */
var r5 := identity(99);
if r5 != 99 { ret 5; }
/* 多个参数 */
var r6 := add(add(1, 2), add(3, 4));
if r6 != 10 { ret 6; }
/* 返回地址 */
var r7 := addr(&r6);
if r7 != &r6 { ret 7; }
if r7.* != 10 { ret 8; }
ret 0;
}