This is usually a very basic task for beginner programmers. Find out the smaller from two numbers.
There are many basic problems that a beginner programmer has to solve. One of the most popular is finding the smallest number out of two.
Luckily, Free Pascal has a ready-made function for that. The function is Min() and can be found under the math
unit. The function is very easy. Even you can write it yourself. Just see the code:
1 | // Copied from the math unit. |
The function pasted here is for Extended
numbers. But it has declarations for Integer
, Int64
and Extended
. Writing functions for all these types will be cumbersome for you. So, it is nicely packed in the math unit.
Basically, what it does is return the number which is smaller between the two provided.
The syntax
The syntax of Min()
function is very simple. Just input two numbers. They can be Integer
, Int64
or Extended
. So it pretty much covers every number you can think of:
1 | function Min( |
Example Console Program
Start Lazarus.
Now add the math
unit to the uses
clause:
1 | uses |
Now add a var
clause before the begin
block:
1 | var |
(You can change this to single
, double
, extended
according to needs.)
Now paste the following code in the begin...end
block:
1 | begin |
We are basically taking two numbers through the ReadLine
statements then passing the values to the Min()
function. Min
function then returns the minimum number.
The resulting code is as follows:
1 | program proj_min_num; |
Now Run the Project (Run -> Run or press F9).
Test it with two numbers on each line. You should get the minimum number between the two inputs.
Ref:
http://www.freepascal.org/docs-html/rtl/math/min.html
Downloads
You can download the source code for the tutorial project and executable/EXE files from the links below: