Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 383
  • Last Modified:

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.
0
boycoder
Asked:
boycoder
1 Solution
 
dbruntonCommented:
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.
0
 
mlmccCommented:
What do you mean integer as a string?

mlmcc
0
 
dbruntonCommented:
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.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
ThommyCommented:
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
 
ThommyCommented:
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
0
 
Ephraim WangoyaCommented:

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
 
systanCommented:
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;
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

0
 
jimyXCommented:
>   "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;

Open in new window


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();

Open in new window


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');

Open in new window


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();

Open in new window


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
 
boycoderAuthor Commented:
Very detailed, clear and perfect!
0

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now