Functions · Hàm số
This page needs a recent browser (with SharedArrayBuffer support). Please update Chrome, Edge, Firefox or Safari to the latest version. · Trang này cần trình duyệt gần đây (hỗ trợ SharedArrayBuffer). Vui lòng cập nhật Chrome, Edge, Firefox hoặc Safari lên phiên bản mới nhất.
English
A function returns one value
- A function is like a procedure, and it also hands one value back.
- The definition says the type of that value:
RETURNS INTEGER. RETURNsets the value. It is normally the last line of the function.
Tiếng Việt
Một hàm trả về một giá trị
- Một hàm giống như một thủ tục, và nó cũng trả về một giá trị.
- Phần định nghĩa chỉ rõ kiểu của giá trị đó:
RETURNS INTEGER. RETURNthiết lập giá trị. Nó thường là dòng cuối cùng của hàm.
FUNCTION Square(N : INTEGER) RETURNS INTEGER
RETURN N * N
ENDFUNCTION
OUTPUT Square(6)
English
Do not write CALL
- A function is used inside an expression, where its returned value is needed.
CALLis only for a procedure.- Parameters of a function are passed by value. The guide says not to use
BYREFon a function.
Tiếng Việt
Đừng viết CALL
- Một hàm được sử dụng bên trong biểu thức, nơi giá trị trả về của nó được cần thiết.
CALLchỉ dành cho một thủ tục.- Các tham số của hàm được truyền by value. Hướng dẫn nói không nên dùng
BYREFtrên một hàm.
FUNCTION Max(Number1 : INTEGER, Number2 : INTEGER) RETURNS INTEGER
IF Number1 > Number2 THEN
RETURN Number1
ELSE
RETURN Number2
ENDIF
ENDFUNCTION
OUTPUT Max(10, 24)
English
Common mistakes
RETURNbelongs inside the function. Outside it, there is nothing to return to.- The call
Square(6)is not a complete statement on its own.OUTPUT Square(6)is.
Tiếng Việt
Lỗi thường gặp
RETURNnằm bên trong hàm. Bên ngoài nó, không có gì để trả về cả.- Lời gọi
Square(6)không phải là một câu lệnh hoàn chỉnh nếu đứng một mình.OUTPUT Square(6)thì có.
English
Now you try
- Write a function and use its result in
OUTPUT.
Tiếng Việt
Bây giờ bạn thử
- Viết một hàm và sử dụng kết quả của nó trong
OUTPUT.
Write Square so that OUTPUT Square(6) prints 36. · Viết Square sao cho OUTPUT Square(6) in ra 36.
Click Run to see the output here. · Nhấn Chạy để xem kết quả ở đây.
Write Max to return the larger of two integers. OUTPUT Max(10, 24) should print 24. · Viết Max để trả về số nguyên lớn hơn giữa hai số. OUTPUT Max(10, 24) nên in ra 24.
Click Run to see the output here. · Nhấn Chạy để xem kết quả ở đây.