Next / Previous / Index / ITC Help System / Publications / Site map / NM Tech homepage

Using procedures in Icon

Tech Computer Center logo

Suppose you need to compute the factorial function, N! = 1 * 2 * ... * N. Here's a complete Icon program containing a procedure that computes the factorial function, and a main program that prints all the values up to 4:

procedure main ( )
  every  n := 1 to 4  do
    write ( n, "! = ", Factorial ( n ) );
end

procedure Factorial ( k )        # Returns k! = 1*2*...*k
  result  :=  1;                 # Prepare to compute product
  every  result *:= ( 1 to k );  # Multiply by 1, 2, ..., k
  return result;
end
The output looks this:
1! = 1
2! = 2
3! = 6
4! = 24

Next: Using arguments in Icon procedures
See also: A tutorial for the Icon programming language
Previous: Using the ``every'' construct in Icon
Site map
Index: Keyword index to help pages
Help: New Mexico Tech Information Technology and Communications: Help System
ITC Publications
To report a problem: File a ticket
Send mail to the User consultant on duty or call them at 575-835-5437
Home: About New Mexico Tech

John Shipman, john@nmt.edu
Last updated: 1996/01/06 21:12:57 UT
URL: http://www.nmt.edu/tcc/help/lang/icon/proc1.html
QR two-dimensional bar code