Pascal - Sử dụng Procedure viết chương trình tính n!

Pascal - Sử dụng Procedure viết chương trình tính n!

Viết chương trình tính n! với yêu cầu sử dụng hàm để tính giai thừa

>> Pascal - Tìm số lớn thứ nhì trong n số được nhập từ bàn phím

>> Pascal - Viết CT cho phép nhập hai số vào hai biến, đổi giá trị của hai biến cho nhau

Mã chương trình

Program Giai_thua_Ham;

uses crt;

var n: longint;

Function Giai_Thua(n:longint):longint;

Var GT:Longint;

begin

     GT:=1;

     while n > 0 do

     begin

     GT:=GT * n;

     n:=n-1;

     end;

     Giai_thua:=GT;

end;

begin

    clrscr;

    write('Nhap n: '); readln(n);

    write(n,'!=',Giai_thua(n));

    readln

end.

Nhận xét:

Hãy so sánh sự khác nhau khi Giai_thua được viết dưới hai dạng Function và Procedure.

- Khi dùng Procedure cần một biến (toàn cục) để lưu giữa giá trị của n!. Biến này được truyền cho tham biến trong Procedure. Sau khi gọi nó cần lệnh để in n!

- Khi dùng Function, có thể sử dụng nó như là một biểu thức.

Bạn thấy bài viết này như thế nào?: 
Average: 3.8 (12 votes)
Ảnh của Tommy Tran

Tommy owner Express Magazine

Drupal Developer having 9+ year experience, implementation and having strong knowledge of technical specifications, workflow development. Ability to perform effectively and efficiently in team and individually. Always enthusiastic and interseted to study new technologies

  • Skype ID: tthanhthuy

Advertisement

 

jobsora

Dich vu khu trung tphcm

Dich vu diet chuot tphcm

Dich vu diet con trung

Quảng Cáo Bài Viết

 
Vì sao một tên tội phạm có thể sở hữu CLB bóng đá tại Anh

Vì sao một tên tội phạm có thể sở hữu CLB bóng đá tại Anh

Trong một căn phòng sang trọng tại một khách sạn ở London, một người đàn ông có biệt danh "Phù thuỷ" ngồi tựa ra sau trên ghế sofa, giơ cánh tay phải lên trước mặt một phóng viên ngầm của Al Jazeera.

Book: Cracking Drupal, A Drop in the Bucket

Download sách Drupal: Cracking Drupal, A Drop in the Bucket

The first book to reveal the vulnerabilities and security issues that exist in the sites that have been built with Drupal?and how to prevent them from continuing Drupal is an open source framework and content management system that allows users to create and organize content, customize presentation, automate tasks, and manage site visitors and contributors. 

Hướng dẫn tạo Drupal 8 Theme sử dụng Bootstrap framework

Hướng dẫn tạo Drupal 8 Theme sử dụng Bootstrap framework

Bootstrap is one of the most popular digital (HTML, CSS and JS) frameworks used today. Because it makes front-end web development faster for any device and for projects of all sizes

Công ty diệt chuột T&C

 

Diet con trung