Files
TransPyC/TransPyV/Test/App/func_test.py
2026-07-18 19:25:40 +08:00

69 lines
1.6 KiB
Python
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.
import stdio
import t, c
def add(a: int, b: int) -> int:
return a + b
def mul(a: int, b: int) -> int:
return a * b
def square(x: int) -> int:
return mul(x, x)
def factorial(n: int) -> int:
if n <= 1:
return 1
return n * factorial(n - 1)
def greet(greeting: str, name: str, times: int) -> int:
i: int = 0
while i < times:
stdio.printf("%s, %s!\n", greeting, name)
i = i + 1
return times
def sub(a: int, b: int) -> int:
return a - b
def func_test() -> int:
r1: int = add(3, 4)
stdio.printf("add(3,4)=%d\n", r1)
r2: int = mul(5, 6)
stdio.printf("mul(5,6)=%d\n", r2)
r3: int = square(7)
stdio.printf("square(7)=%d\n", r3)
r5: int = factorial(5)
stdio.printf("factorial(5)=%d\n", r5)
# ============================================================
# 测试: 函数乱序传参(关键字参数)
# ============================================================
# 乱序传参b=4, a=3 等价于 add(3, 4)
r6: int = add(b=4, a=3)
stdio.printf("add(b=4,a=3)=%d\n", r6)
# 乱序传参a=10, b=3 等价于 sub(10, 3)
r7: int = sub(b=3, a=10)
stdio.printf("sub(b=3,a=10)=%d\n", r7)
# 混合传参:位置参数 + 关键字参数
# greeting="Hello" 是位置参数name="World" 和 times=2 是关键字
r8: int = greet("Hello", name="World", times=2)
stdio.printf("greet mixed: returned=%d\n", r8)
# 全关键字乱序传参
r9: int = greet(times=1, name="TransPyC", greeting="Hi")
stdio.printf("greet kwargs: returned=%d\n", r9)
return 0