Academic Tutorials



English | French | Portugese | German | Italian
Home Advertise Payments Recommended Websites Interview Questions FAQs
News Source Codes E-Books Downloads Jobs Web Hosting
Chats

Delphi
Introduction to Delphi
Data Types
Programming Logic
Case statements
Delphi-loops
Overview of Subroutine
Delphi Exception handling
Delphi Date Time
Delphi Filest
Delphi Binary files
Delphi Pointers
Delphi Printing text and Graphics

HTML Tutorials
HTML Tutorial
XHTML Tutorial
CSS Tutorial
TCP/IP Tutorial
CSS 1.0
CSS 2.0
HLML
XML Tutorials
XML Tutorial
XSL Tutorial
XSLT Tutorial
DTD Tutorial
Schema Tutorial
XForms Tutorial
XSL-FO Tutorial
XML DOM Tutorial
XLink Tutorial
XQuery Tutorial
XPath Tutorial
XPointer Tutorial
RDF Tutorial
SOAP Tutorial
WSDL Tutorial
RSS Tutorial
WAP Tutorial
Web Services Tutorial
Browser Scripting
JavaScript Tutorial
VBScript Tutorial
DHTML Tutorial
HTML DOM Tutorial
WMLScript Tutorial
E4X Tutorial
Server Scripting
ASP Tutorial
PERL Tutorial
SQL Tutorial
ADO Tutorial
CVS
Python
Apple Script
PL/SQL Tutorial
SQL Server
PHP
.NET (dotnet)
Microsoft.Net
ASP.Net
.Net Mobile
C# : C Sharp
ADO.NET
VB.NET
VC++
Multimedia
SVG Tutorial
Flash Tutorial
Media Tutorial
SMIL Tutorial
Photoshop Tutorial
Gimp Tutorial
Matlab
Gnuplot Programming
GIF Animation Tutorial
Scientific Visualization Tutorial
Graphics
Web Building
Web Browsers
Web Hosting
W3C Tutorial
Web Building
Web Quality
Web Semantic
Web Careers
Weblogic Tutorial
SEO
Web Site Hosting
Domain Name
Java Tutorials
Java Tutorial
JSP Tutorial
Servlets Tutorial
Struts Tutorial
EJB Tutorial
JMS Tutorial
JMX Tutorial
Eclipse
J2ME
JBOSS
Programming Langauges
C Tutorial
C++ Tutorial
Visual Basic Tutorial
Data Structures Using C
Cobol
Assembly Language
Mainframe
Forth Programming
Lisp Programming
Pascal
Delphi
Fortran
OOPs
Data Warehousing
CGI Programming
Emacs Tutorial
Gnome
ILU
Soft Skills
Communication Skills
Time Management
Project Management
Team Work
Leadership Skills
Corporate Communication
Negotiation Skills
Database Tutorials
Oracle
MySQL
Operating System
BSD
Symbian
Unix
Internet
IP-Masquerading
IPC
MIDI
Software Testing
Testing
Firewalls
SAP Module
ERP
ABAP
Business Warehousing
SAP Basis
Material Management
Sales & Distribution
Human Resource
Netweaver
Customer Relationship Management
Production and Planning
Networking Programming
Corba Tutorial
Networking Tutorial
Microsoft Office
Microsoft Word
Microsoft Outlook
Microsoft PowerPoint
Microsoft Publisher
Microsoft Excel
Microsoft Front Page
Microsoft InfoPath
Microsoft Access
Accounting
Financial Accounting
Managerial Accounting
Network Sites


An overview of subroutine


Previoushome Next






An overview of subroutine



A subroutine is like a sub-program. It not only helps divide your code up into sensible, manageable chunks, but it also allows these chunks to be used (called) by different parts of your program. Each subroutine contains one of more statements.
A D V E R T I S E M E N T

In common with other languages, Delphi provides 2 types of subroutine - Procedures and Functions. Functions are the same as procedures except that they return a value in addition to executing statements. A Function, as its name suggests, is like a little program that calculates something, returning the value to the caller. On the other hand, a procedure is like a little routine that performs something, and then just finishes.

Parameters to subroutinesBoth functions and procedures can be defined to operate without any data being passed. For example, you might have a function that simply returns a random number (like the Delphi Random function). It needs no data to get it going.
Likewise, you can have a procedure that carries out some task without the need for data to dictate its operations. For example, you might have a procedure that draws a square on the screen. The same square every time it is called.
Often, however, you will pass data, called parameters, to a subroutine. (Note that the definition of a subroutine refers to parameters as arguments - they are parameters when passed to the subroutine).

Some simple function and procedure examplesThe following code illustrates simple function and procedure definitions:
A procedure without parameters
procedure ShowTime;// A procedure with no parameters
begin
// Display the current date and time
ShowMessage('Date and time is '+DateTimeToStr
end;

// Let us call this procedure
ShowTime;

Date and time is 12/12/2002 15:30:45

Notice that we are using some Delphi run time library functions, marked in blue, in the above code. Click on any to read more.
A procedure with parameters
procedure ShowTime(dateTime : TDateTime);// With parameters
begin
// Display the date and time passed to the routine
ShowMessage('Date and time is '+DateTimeToStr(dateTime));
end;

// Let us call this procedure
ShowTime(Yesterday);

Date and time is 11/12/2002

function RandomChar : char;
var
i : integer;
begin
// Get a random number from 65 to 90
// (These numbers equate to characters 'A' to 'Z'
i := RandomRange(65, 90);

// Return this value as a char type in the return variable, Result
Result := Chr(i);
end;

// Let us call this function
ShowMessage('Char chosen is : '+RandomChar);

Char chosen is : A

It is important to note that we return the value from a function in a special variable called Result that Delphi secretly defines for us to be the same type as the return type of the function. We can assign to it at any point in the function. When the function ends, the value then held in Result is then returned to the caller.
A function with parameters
function Average(a, b, c : Extended) : Extended;
begin
// return the average of the 3 passed numbers
Result := Mean(a, b, c);
end;

// Let us call this function
ShowMessageFmt('Average of 2, 13 and 56 = %f',[Average(2,13,56)]);

Average of 2, 13 and 56 = 23.67


Interfaces versus ImplementationIn the above examples, we have shown the subroutines and the calling code in one sequence. In practice, even the calling code will be in a subroutine, for a very good reason. Let us show complete Unit code to clarify this:
// Full Unit code.
// -----------------------------------------------------------
// You must store this code in a unit called Unit1 with a form
// called Form1 that has an OnCreate event called FormCreate.

unit Unit1;

interface

uses
Forms, Dialogs;

type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
end;

var
Form1: TForm1;

implementation
{$R *.dfm} // Include form definitions

// A small procedure
procedure InLineProc;
begin
ShowMessage('Hello World');
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
// Call our little in line procedure
InLineProc;
end;

end.

The following is displayed in a little message dialog:

Hello World

The InLineProc we have defined above is literally that - an in-line subroutine. It must be defined before it is called.
The TForm1.OnCreate procedure is quite different. The TForm1 qualifier gives a clue. This procedure, along with out InLineProc procedure, is defined in what is called the Implementation section of the Unit. Looking earlier in the code, you will see a one line declaration of OnCreate in the Interface part of the Unit. It is part of the class definition for the form (TForm1) that the Unit and program use as the main screen .
Any subroutine defined in the Interface section must defined in the Implementation section. Our InLineProc was not, so it needs no advance declaration.

Data local to a subroutineIn the RandomChar example above, we declared an integer variable for use in a calculation by the function. Subroutines can have their own types, constants and variables, and these remain local to the routine. Variable values are reset every time the routine is called (use a class object to hold onto data across routine calls). Here is an illustration of this local variable action:
procedure DoIt(A : Integer);
begin
A := A * 2;
ShowMessageFmt('A in the procedure= %d',[A]);
end;

procedure TForm1.FormCreate(Sender: TObject);
var
A : Integer;
begin
A := 22;
ShowMessageFmt('A in program before call = %d',[A]);
// Call the procedure
DoIt(A);
ShowMessageFmt('A in program now = %d',[A]);
end;

in program before call = 22
A in the procedure = 44
A in program now = 22

The procedure is passed A, updates it and displays it. The caller then displays the A that it passed to the procedure. It is unchanged. The procedure sees this A as if it were defined as a local variable. Like local variables, when the procedure ends, their value is lost.

Passing data by referenceThe default was of passing data is by what is called by value. Literally, the parameter value is passed to the subroutine argument. reference to the argument is then to this copy of the variable value.
Passing by reference means that the subroutine actually refers to the passed variable rather than its value. Any changes to the value will affect the caller variable. We declare a variable to be passed by reference with the var prefix. Rewriting the above code to use by reference changes matters:
procedure DoIt
begin
A := A * 2;
ShowMessageFmt('A in the procedure= %d',[A]);
end;

procedure TForm1.FormCreate(Sender: TObject);
var
A : Integer;
begin
A := 22;
ShowMessageFmt('A in program before call = %d',[A]);
// Call the procedure
DoIt(A);
ShowMessageFmt('A in program now = %d',[A]);
end;

A in program before call = 22
A in the procedure = 44
A in program now = 44

Now the caller A variable is updated by the procedure.
This is a very useful way of returning data from a procedure, as used by, for example, the Delphi Insert routine. It also allows us to return more than one value from a subroutine.

Output only parametersWe can go further, and define parameters that we can update, but which are there for update only - output from our subroutine. They should not be read by the subroutine, the caller not responsible for any starting value they might contain.
procedure DoIt;
begin
A := 123;
ShowMessageFmt('A in the procedure= %d',[A]);
end;

procedure TForm1.FormCreate(Sender: TObject);
var
A : Integer;
begin
ShowMessage('A before the call is unknown');
// Call the procedure
DoIt(A);
ShowMessageFmt('A in program now = %d',[A]);
end;

A before the call is unknown
A in the procedure = 123
A in program now = 123


Constant value parametersFor code clarity, and performance, it is often wise to declare arguments that are only ever read by a subroutine as constants. This is done with the const prefix. It can be used even when a non-constant parameter is passed. It simply means that the parameter is only ever read by the subroutine.
procedure DoIt: Integer; Out B : Integer);
begin
B := A * 2;
end;

procedure TForm1.FormCreate(Sender: TObject);
var
A, B : Integer;
begin
A := 22;
// Call the procedure
DoIt(A, B);
ShowMessageFmt('B has been set to = %d',[B]);
end;

B has been set to 44

Notice that when defining two argument types, the arguments are separated with a Same routine, different parametersOne of the benefits of Object Oriented programming is that some of the rigidity of procedural languages was relaxed. This has spilled over into non object orientation subroutines (as opposed to class methods).
One of the benefits is that we can define two or more subroutines that have exactly the same name. Delphi is able to tell them apart by the different number or types of parameters.
The example below illustrates this with two versions of the DoIt procedure.
procedure DoIt; overload;
begin
ShowMessage('DoIt with no parameters called');
end;

procedure DoIt(msg : String); overload;
begin
ShowMessage('DoIt called with parameter : '+msg);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
// Call the procedure using no parameters
DoIt;
// Now call the procedure using one parameter
DoIt('Hi there');
end;

vDoIt with no parameters called
DoIt called with parameter : Hi There


Be the first one to comment on this page.




  Delphi eBooks
More Links » »
 
 Delphi FAQs
More Links » »
 
 Delphi Interview Questions
More Links » »
 
 Delphi Articles

No Delphi Articles could be found as of now.

 
 Delphi News

No News on Delphi could be found as of now.

 
 Delphi Jobs

No Delphi Articles could be found as of now.


Share And Enjoy:These icons link to social bookmarking sites where readers can share and discover new web pages.
  • blinkbits
  • BlinkList
  • blogmarks
  • co.mments
  • connotea
  • del.icio.us
  • De.lirio.us
  • digg
  • Fark
  • feedmelinks
  • Furl
  • LinkaGoGo
  • Ma.gnolia
  • NewsVine
  • Netvouz
  • RawSugar
  • Reddit
  • scuttle
  • Shadows
  • Simpy
  • Smarking
  • Spurl
  • TailRank
  • Wists
  • YahooMyWeb

Previoushome Next

Keywords: delphi pointers,delphi programming,delphi files,delphi datatypes,delphi text,delphi graphics,delphi records,delphi text printing

HTML Quizzes
HTML Quiz
XHTML Quiz
CSS Quiz
TCP/IP Quiz
CSS 1.0 Quiz
CSS 2.0 Quiz
HLML Quiz
XML Quizzes
XML Quiz
XSL Quiz
XSLT Quiz
DTD Quiz
Schema Quiz
XForms Quiz
XSL-FO Quiz
XML DOM Quiz
XLink Quiz
XQuery Quiz
XPath Quiz
XPointer Quiz
RDF Quiz
SOAP Quiz
WSDL Quiz
RSS Quiz
WAP Quiz
Web Services Quiz
Browser Scripting Quizzes
JavaScript Quiz
VBScript Quiz
DHTML Quiz
HTML DOM Quiz
WMLScript Quiz
E4X Quiz
Server Scripting Quizzes
ASP Quiz
PERL Quiz
SQL Quiz
ADO Quiz
CVS Quiz
Python Quiz
Apple Script Quiz
PL/SQL Quiz
SQL Server Quiz
PHP Quiz
.NET (dotnet) Quizzes
Microsoft.Net Quiz
ASP.Net Quiz
.Net Mobile Quiz
C# : C Sharp Quiz
ADO.NET Quiz
VB.NET Quiz
VC++ Quiz
Multimedia Quizzes
SVG Quiz
Flash Quiz
Media Quiz
SMIL Quiz
Photoshop Quiz
Gimp Quiz
Matlab Quiz
Gnuplot Programming Quiz
GIF Animation Quiz
Scientific Visualization Quiz
Graphics Quiz
Web Building Quizzes
Web Browsers Quiz
Web Hosting Quiz
W3C Quiz
Web Building Quiz
Web Quality Quiz
Web Semantic Quiz
Web Careers Quiz
Weblogic Quiz
SEO Quiz
Web Site Hosting Quiz
Domain Name Quiz
Java Quizzes
Java Quiz
JSP Quiz
Servlets Quiz
Struts Quiz
EJB Quiz
JMS Quiz
JMX Quiz
Eclipse Quiz
J2ME Quiz
JBOSS Quiz
Programming Langauges Quizzes
C Quiz
C++ Quiz
Visual Basic Quiz
Data Structures Using C Quiz
Cobol Quiz
Assembly Language Quiz
Mainframe Quiz
Forth Programming Quiz
Lisp Programming Quiz
Pascal Quiz
Delphi Quiz
Fortran Quiz
OOPs Quiz
Data Warehousing Quiz
CGI Programming Quiz
Emacs Quiz
Gnome Quiz
ILU Quiz
Soft Skills Quizzes
Communication Skills Quiz
Time Management Quiz
Project Management Quiz
Team Work Quiz
Leadership Skills Quiz
Corporate Communication Quiz
Negotiation Skills Quiz
Database Quizzes
Oracle Quiz
MySQL Quiz
Operating System Quizzes
BSD Quiz
Symbian Quiz
Unix Quiz
Internet Quiz
IP-Masquerading Quiz
IPC Quiz
MIDI Quiz
Software Testing Quizzes
Testing Quiz
Firewalls Quiz
SAP Module Quizzes
ERP Quiz
ABAP Quiz
Business Warehousing Quiz
SAP Basis Quiz
Material Management Quiz
Sales & Distribution Quiz
Human Resource Quiz
Netweaver Quiz
Customer Relationship Management Quiz
Production and Planning Quiz
Networking Programming Quizzes
Corba Quiz
Networking Quiz
Microsoft Office Quizzes
Microsoft Word Quiz
Microsoft Outlook Quiz
Microsoft PowerPoint Quiz
Microsoft Publisher Quiz
Microsoft Excel Quiz
Microsoft Front Page Quiz
Microsoft InfoPath Quiz
Microsoft Access Quiz
Accounting Quizzes
Financial Accounting Quiz
Managerial Accounting Quiz
Testimonials | Contact Us | Link to Us | Site Map
Copyright 2008. Academic Tutorials.com. All rights reserved Privacy Policies | About Us
Our Portals : Academic Tutorials | Best eBooksworld | Beyond Stats | City Details | Interview Questions | Discussions World | Excellent Mobiles | Free Bangalore | Give Me The Code | Gog Logo | Indian Free Ads | Jobs Assist | New Interview Questions | One Stop FAQs | One Stop GATE | One Stop GRE | One Stop IAS | One Stop MBA | One Stop SAP | One Stop Testing | Webhosting in India | Dedicated Server in India | Sirf Dosti | Source Codes World | Tasty Food | Tech Archive | Testing Interview Questions | Tests World | The Galz | Top Masala | Vyom | Vyom eBooks | Vyom International | Vyom Links | Vyoms | Vyom World
Copyright 2003-2014 Vyom Technosoft Pvt. Ltd., All Rights Reserved.