.NET
  Home arrow .NET arrow The Delphi Language, Part 2
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
.NET

The Delphi Language, Part 2
By: Xavier Pacheco
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 14
    2004-07-13

    Table of Contents:
  • The Delphi Language, Part 2
  • Out Parameters, Constant Parameters, and Open Array Parameters
  • Scope and Units and Namespaces
  • The uses Clause and Circular Unit References
  • Object-Oriented Programming
  • Using Delphi Objects
  • Methods
  • Class References and Properties
  • Events
  • Visibility Specifiers, Friend Classes and Class Helpers
  • Nested Types, Operator Overloading and Attributes
  • Interfaces
  • Using Interfaces, SEH
  • Exception Classes

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    The Delphi Language, Part 2


    (Page 1 of 14 )

    In this second half of the chapter (Delphi for .NET Developer's Guide, Sams, ISBN: 0-672-32443-1, 2004), author Xavier Pacheco talks about how Delphi enables you to pass parameters by value or by reference to functions and procedures, provides an explanation of units, using Delphi objects and more. (Here's the link to part 1 for reference.)

    delphiProcedures and Functions

    As a programmer, you should already be familiar with the basics of procedures and functions. A procedure is a discrete program part that performs some particular task when it's called and then returns to the calling part of your code. A function works the same except that a function returns a value after its exit to the calling part of the program.

    Listing 5.1 demonstrates a short Delphi program with a procedure and a function.

    Listing 5.1 An Example of a Function and a Procedure

    1:  program FuncProc;
    2:
    3:  {$APPTYPE CONSOLE}
    4:
    5:  procedure BiggerThanTen(I: Integer);
    6:  { writes something to the screen if I is greater than 10  }
    7:  begin
    8:    if I > 10 then
    9:    writeln('Funky.');
    10: end;
    11:
    12: function IsPositive(I: Integer): Boolean;
    13: { Returns True if I is 0 or positive, False if I is negative }
    14: begin
    15:   Result := I >= 0;
    16: end;
    17:
    18: var
    19:   Num: Integer;
    20: begin
    21:   Num := 23;
    22:   BiggerThanTen(Num);
    23:   if IsPositive(Num) then
    24:     writeln(Num, ' Is positive.')
    25:   else
    26:   writeln(Num, ' Is negative.');
    27: end.

    Result - The local variable Result in the IsPositive() function deserves special attention. Every Delphi function has an implicit local variable called Result that contains the return value of the function. Note that unlike C#'s return statement, the function doesn't terminate as soon as a value is assigned to Result.

    If you want to duplicate the behavior of C#'s return statement, you can call Exit immediately after assigning to Result. The Exit statement immediately exits the current routine.

    You also can return a value from a function by assigning the name of a function to a value inside the function's code. This is standard Delphi syntax and a holdover from previous versions of Borland Pascal. If you choose to use the function name within the body, be careful to note that there is a huge difference between using the function name on the left side of an assignment operator and using it somewhere else in your code. If on the left, you are assigning the function return value. If somewhere else in your code, you are calling the function recursively!

    Note that the implicit Result variable isn't allowed when the compiler's Extended Syntax option is disabled in the Project, Options, Compiler dialog box or when you're using the {$EXTENDEDSYNTAX OFF} (or {$X-}) directive.

    Passing Parameters

    Delphi enables you to pass parameters by value or by reference to functions and procedures. The parameters you pass can be of any basic or user-defined type or an open array. (Open arrays are discussed later in this chapter.) Parameters also can be constant if their values will not change in the procedure or function.

    Value Parameters

    Value parameters are the default mode of parameter passing. When a parameter is passed by value, it means that a local copy of that variable is created, and the function or procedure operates on the copy. Consider the following example:

    procedure Foo(I: Integer);

    When you call a procedure in this way, a copy of Integer I will be made, and Foo() will operate on the local copy of I. This means that you can change the value of I without having any effect on the variable passed into Foo().

    Reference Parameters

    Delphi enables you to pass variables to functions and procedures by reference; parameters passed by reference are also called variable parameters. Passing by reference means that the function or procedure receiving the variable can modify the value of that variable. To pass a variable by reference, use the keyword var in the procedure's or function's parameter list:

    procedure ChangeMe(var x: longint); 
    begin
     
    x := 2; { x is now changed in the calling procedure }
    end;

    Instead of making a copy of x, the var keyword causes the address of the parameter to be copied so that its value can be directly modified.

    Using var parameters is equivalent to passing variables by reference in C# using the ref keyword or in Visual Basic .NET using the ByRef directive.

    This chapter is from Delphi for .NET Developer's Guide, by Xavier Pacheco (Sams, 2004, ISBN: 0-672-32443-1). Check it out at your favorite bookstore today.

    Buy this book now.

    More .NET Articles
    More By Xavier Pacheco


     

    .NET ARTICLES

    - Using CrystalReportViewer to Display Crystal...
    - Creating Summary .Net Crystal Reports
    - More on Commands, Input and the WPF
    - Grouping and Aggregating When Querying LINQ ...
    - Commands, Input and the WPF
    - Keyboard and Ink Input with WPF
    - Mouse Input and the WPF
    - Input with Windows Presentation Foundation
    - Introducing LINQ with XML and Databases
    - An Introduction to LINQ
    - Querying LINQ to SQL: Basics
    - Completing a Simple Storefront with LINQ
    - Knowing Your Environment: the System.Environ...
    - Creating the Home Page for a Simple Storefro...
    - LINQ Quickly with Language Integrated Queries

     
    Best Practices for Windows Vista Migration Presentation
    Dell and Microsoft recently held a series of face-to-face seminars entitled, &qu....

     
    Creating a Culture for Code Reuse
    If you oversee development teams you know that like it or not proprietary and ex....

     
    Keys to Web Application Acceleration: Advances in Delivery Systems
    Accelerate Web apps by up to 5x. Ensure significantly faster access to the Web a....

     
    Optimizing Application Monitoring
    Tired of finding out from your customers that you're offline? This white paper e....

     
    Solaris to Solaris Migration -- Migrating applications from Sun SPARC to Dell PowerEdge R900
    This comprehensive Migration Guide reviews the approach that Principled Technolo....

     




    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
    Stay green...Green IT