Sunday, October 20, 2019
Method Overloading Default Parameters in Delphi
Method Overloading Default Parameters in Delphi Functions and procedures are an important part of the Delphi language. Starting with Delphi 4, Delphi allows us to work with functions and procedures that support default parameters (making the parameters optional), and permits two or more routines to have an identical nameà but operate as completely different routines. Lets see how Overloading and default parameters can help you code better. Overloading Simply put, overloading is declaring more than one routine with the same name. Overloading allows us to have multiple routines that share the same name, but with a different number of parameters and types. As an example, lets consider the following two functions: {Overloaded routines must be declared with the overload directive} function SumAsStr(a, b :integer): string; overload; begin à à Result : IntToStr(a b) ; end; function SumAsStr(a, b : extended; Digits:integer): string; overload; begin à à Result : FloatToStrF(a b, ffFixed, 18, Digits) ; end; These declarations create two functions, both called SumAsStr, that take a different number of parameters and are of two different types. When we call an overloaded routine, the compiler must be able to tell which routine we want to call. For example, SumAsStr(6, 3) calls the first SumAsStr function, because its arguments are integer-valued. Note: Delphi will help you pick the right implementation with the help of code completion and code insight. On the other hand, consider if we try to call the SumAsStr function as follows: SomeString : SumAsStr(6.0,3.0) Well get an error that reads: there is no overloaded version of SumAsStr that can be called with these arguments. This means that we should also include the Digits parameter used to specify the number of digits after the decimal point. Note: There is only one rule when writing overloaded routines, and that is that anà overloaded routine must differ in at least one parameter type. The return type, instead, cannot be used to distinguish among two routines. Two Units - One Routine Lets say we have one routine in unit A, and unit B uses unit A, but declares a routine with the same name. The declaration in unit B does not need the overload directive - we should use unit As name to qualify calls to As version of the routine from unit B. Consider something like this: unit B; ... uses A; ... procedure RoutineName; begin à Result : A.RoutineName; end; An alternative to using overloaded routines is to use default parameters, which usually results in less code to write and maintain. Default/Optional Parameters In order to simplify some statements, we can give a default value for the parameter of a function or procedure, and we can call the routine with or without the parameter, making it optional. To provide a default value, end the parameter declaration with the equal () symbol followed by a constant expression. For example, given the declaration function SumAsStr (a,b : extended; Digits : integer 2) : string; the following function calls are equivalent. SumAsStr(6.0, 3.0) SumAsStr(6.0, 3.0, 2) Note:à Parameters with default values must occur at the end of the parameter list, and must be passed by value or as const. A reference (var) parameter cannot have a default value. When calling routines with more than one default parameter, we cannot skip parameters (like in VB): function SkipDefParams(var A:string; B:integer5, C:booleanFalse):boolean; ... //this call generates an error message CantBe : SkipDefParams(delphi, , True) ; Overloading With Default Parameters When using both function or procedure overloading and default parameters, dont introduce ambiguous routine declarations. Consider the following declarations: procedure DoIt(A:extended; B:integer 0) ; overload; procedure DoIt(A:extended) ; overload; The call to DoIt procedure like DoIt(5.0), does not compile. Because of the default parameter in the first procedure, this statement might call both procedures, because it is impossible to tell which procedure is meant to be called.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.