Solved

# Functions and Integer

Posted on 2011-05-03
378 Views
Hi

can someone explain to me in a "hello world" format the basic idea of how functions work and also integer as a string. To provide small simple as possible examples.
0
Question by:boycoder

LVL 47

Expert Comment

var
firstnumber, secondnumber : integer;
result : integer;

function add_two_numbers(number1, number2 : integer) : integer;
var
total : integer;
begin
total := number1 + number2;
end;

begin
firstnumber := 10;
secondnumber := 22;

A function is a little box that you drop variables into and do some work on them and get something out.  The something out is expressed by when use the function name.

In the above example when I used add_two_numbers := total that is giving the function output.

Input to the function was here.

add_two_numbers(number1, number2 : integer) : integer;

It expects two integers and will return one integer.
0

LVL 100

Expert Comment

What do you mean integer as a string?

mlmcc
0

LVL 47

Expert Comment

var
intNumber   : Integer;
text        : string;

begin
intNumber   := 123;

// Display these numbers using vanilla 'Str'
Str(intNumber, text);
Writeln(text);

The Str function takes the number and places it into text string.
0

LVL 19

Expert Comment

Delphi Basics: Functions and procedures
http://www.delphibasics.co.uk/Article.asp?Name=Routines

Delphi Basics: Convert an integer into a string
http://www.delphibasics.co.uk/RTL.asp?Name=IntToStr
0

LVL 19

Expert Comment

This is a very good link to learn about functions and procedures...

Understanding and Using Functions and Procedures

Delphi tutorial: Introduction To Subroutines (Procedures and Functions)
http://sheepdogguides.com/dt1d.htm
0

LVL 32

Expert Comment

Here is another good tutorial you can look at.

http://taoyue.com/tutorials/pascal/contents.html

The more you practice, the better you get

Cheers
0

LVL 14

Expert Comment

I think the answer is this;
http://www.experts-exchange.com/Programming/Languages/Pascal/Delphi/A_1335-Delphi-Code-Number-to-Word.html

and this;

if that's not what you mean!
here's a code;
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{\$R *.dfm}

function int2string(numb:integer):string;
begin
result:=inttostr(numb);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
showmessage(int2string(77));
end;

end.

0

LVL 24

Accepted Solution

>   "can someone explain to me in a "hello world" format the basic idea of how functions work"

First of all you need to know that functions and procedures are the same, where they are holding a specific lines of codes (block of code) and given a name which can be called at any point of the project to be executed as per it's scope (scope is the part of the code where your functions, procedures, variables ,... etc can be available to be called at). Functions and procedures are the same in terms of calling and passing parameters. The only difference is that the function returns a value when called but the procedure does not.

The Hello world example will be sum of two integers:
function Sum(Num1,Num2:Integer):Integer;
begin
Result:=Num1+Num2;
end;

1- The function name is Sum.
2- Num1 is the first parameter.
3- Num2 is the second parameter.
4- The returning value is of the datatype Integer as we put that after the parameters "function Sum(Num1,Num2:Integer):Integer;" it could be any datatype, depends on the value you are returning.
5- You return the required value from the functions by assigning that value to "Result" (Note it must be same type as per the type we mentioned earlier at step 4). So if you say the function returns Integer the value you assign to Result should be integer value.

So to use this function "Sum" at any point in your project just put its name and pass two numbers (what you pass to the function must match the datatype declared in the function as well as the number of parameters) so you get the summation of those two numbers at the place where you called the function.

To test it, add a Button and on its OnClick event put this code:
Showmessage(IntToStr(Sum(1,2)));

Please note, it all depends on your requirements, for instance, this example used parameters of integer just because we wanted it to sum integer numbers and also it returns integer because we wanted it to return integer, but it could be any datatype or even without parameters at all, it depends on your requirement. For example let's apply this on your previous question where you wanted to increase the counter when sales is made, and to decrease when refunding:
function IncCounter():String;
begin
Inc(counter);
Result:=IntToStr(counter);
end;

if (DB5.Text= 'Sale ok') and (labelsales.caption = '3 Months Membership') then
begin
form1.label3.Caption :=  IncCounter();
end;

function DecCounter():String;
begin
Dec(counter);
if counter < 0 then
counter := 0
else
Result:=IntToStr(counter);
end;

if (DB5.Text= '3 Months Membership') then
form1.label3.Caption :=  DecCounter();

As you can see we can use the functions even without parameter but we can not use it without returning value. Function without returning value means you must use Procedure.

Another different example:
Let's say you have a project and there will be many places where you want to test if a number is Positive or Negative so you get True if the number you pass to the function is Positive and False if Negative.

So you pass to the function a number and you get a Boolean result:
function IsPositive(Num:Integer):Boolean;
begin
if Num >= 0 then
Result:=True;
if Num < 0 then
Result:=False;
end;

At a button OnClick put this code:
if IsPositive(-1) then
Showmessage('Positive')
else
Showmessage('Negative');

Or pass a variable of the datatype integer. For example let's use these functions to increment and decrement as per your last question:
function IncCounter():String;
begin
Inc(counter);
Result:=IntToStr(counter);
end;

if (DB5.Text= 'Sale ok') and (labelsales.caption = '3 Months Membership') then
begin
form1.label3.Caption :=  IncCounter();
end;

function IsPositive(Num:Integer):Boolean;
begin
if Num >= 0 then
Result:=True;
if Num < 0 then
Result:=False;
end;

function DecCounter():String;
begin
Dec(counter);
if IsPositive(counter) then
Result:=IntToStr(counter)
else
begin
counter := 0;
Result:=IntToStr(counter);
end;
end;

if (DB5.Text= '3 Months Membership') then
form1.label3.Caption :=  DecCounter();

Hope it's clear.

>   "and also integer as a string."

I am not sure what you mean by this. Do you mean you want to know about IntToStr, or how to convert from Integer to String? Please clarify more.
0

Author Closing Comment

Very detailed, clear and perfect!
0

## Featured Post

### Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…