Link to home
Start Free TrialLog in
Avatar of boycoder
boycoder

asked on

Functions and Integer

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.
Avatar of dbrunton
dbrunton
Flag of New Zealand image

var
  firstnumber, secondnumber : integer;
  result : integer;


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

begin
  firstnumber := 10;
 secondnumber := 22;
 result := add_two_numbers(firstnumber, secondnumber);

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.
Avatar of Mike McCracken
Mike McCracken

What do you mean integer as a string?

mlmcc
adapted from http://www.delphibasics.co.uk/RTL.asp?Name=Str

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.
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
This is a very good link to learn about functions and procedures...

Understanding and Using Functions and Procedures
http://delphi.about.com/od/beginners/a/subroutines.htm



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

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
I think the answer is this;
https://www.experts-exchange.com/Programming/Languages/Pascal/Delphi/A_1335-Delphi-Code-Number-to-Word.html

and this;
http://delphi.about.com/od/objectpascalide/a/curr2words.htm

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.

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of jimyX
jimyX

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of boycoder

ASKER

Very detailed, clear and perfect!