Difference Between Actual And Formal Parameters In PL/SQL

SHARE

A parameter is a list of optional parameters that you create to transmit information into the procedure and send information out of the procedure to the calling application. The argument is another name for a parameter. When you define a parameter, you also describe how it may be used.

There are two types of parameters or arguments:

  • Actual Parameters
  • Formal Parameters

Actual Parameters and Formal Parameters are two often used concepts in relation to functions. Actual Parameters differ from Formal Parameters in that Actual Parameters are values supplied to the function when it is invoked, whereas Formal Parameters are variables declared by the function that get values when the function is called.

What are actual parameters?

Actual parameters, also known as actual arguments, are values or expressions provided when calling or invoking a subprogram, such as a procedure or function, in a programming language like PL/SQL. These values are passed to the subprogram to be used as inputs or arguments within the subprogram’s body. Actual parameters supply the necessary data that the subprogram operates on.

When you call a subprogram, you pass actual parameters to it. These parameters can be literals, variables, expressions, or function calls. The data types of actual parameters must match the data types of the formal parameters (parameters declared in the subprogram’s header) to ensure type safety and compatibility.

The order in which you specify actual parameters during the subprogram call must correspond to the order of formal parameters declared in the subprogram’s header.

Actual parameters are typically read-only within the subprogram. This means that the subprogram can use these values but cannot modify them. Any changes made to actual parameters within the subprogram do not affect the original values outside the subprogram.

The lifetime of actual parameters is limited to the duration of the subprogram call. Once the subprogram execution is complete, the actual parameters cease to exist.

Example

In a PL/SQL procedure that calculates the sum of two numbers, the actual parameters could be the two numbers you want to add. For instance, if you want to find the sum of 5 and 7, you would pass these values as actual parameters when calling the procedure.

Here’s a simplified example in PL/SQL:

In this example, 5 and 7 are the actual parameters passed to the CalculateSum procedure, and they are used within the procedure to perform the calculation.

What are formal parameters?

Formal parameters, also known as formal arguments, are placeholders or variables defined in the header or declaration of a subprogram (such as a procedure or function) in a programming language like PL/SQL. These parameters act as placeholders for values that will be passed to the subprogram when it is called. Formal parameters represent the expected inputs or arguments that the subprogram will use in its computations.

Each formal parameter has a specified data type that indicates the type of value it can accept. The data types of formal parameters help enforce type safety and ensure that the correct types of values are passed to the subprogram.

In some programming languages, formal parameters may have names, which are used within the subprogram’s body to refer to the values that will be passed to them. These names are used like variables within the subprogram.

The order in which formal parameters are declared in the subprogram’s header determines their position in the parameter list. When calling the subprogram, the actual parameters must match the order of the formal parameters.

Formal parameters can be initialized with default values when declared. This allows you to provide a default value that will be used if no corresponding actual parameter is provided during the subprogram call.

Formal parameters have a lifetime equivalent to the execution of the subprogram. They exist as long as the subprogram is running and can be used to hold and manipulate values passed as actual parameters.

Example

In a PL/SQL procedure that calculates the sum of two numbers, the formal parameters could be declared as “a” and “b,” representing the two numbers to be added.

Here’s an example in PL/SQL:

In this example, “a” and “b” are the formal parameters of the CalculateSum procedure. When you call this procedure, you would provide actual parameters (values) that match the data types and order of these formal parameters. The formal parameters “a” and “b” are used within the procedure’s body to perform the addition.

Actual And Formal Parameters: Key Differences

AspectActual ParametersFormal Parameters
DefinitionActual parameters are the values or expressions passed to a subprogram during its invocation or call.Formal parameters are the variables or placeholders defined in the subprogram’s header that receive the values from actual parameters.
Data TypeActual parameters can be literals, variables, expressions, or function calls, and they must match the formal parameter’s data type.Formal parameters have predefined data types that are specified when the subprogram is declared.
ScopeActual parameters are only visible and accessible within the body of the subprogram in which they are declared.Formal parameters are local variables within the subprogram and can be used throughout the subprogram’s body.
ModificationActual parameters are read-only within the subprogram; you cannot modify their values.Formal parameters can be assigned new values within the subprogram, which can affect their original values outside the subprogram.
InitializationActual parameters need not be explicitly initialized before passing them to a subprogram.Formal parameters should be initialized in the subprogram’s header, either with default values or through incoming actual parameters.
NumberThe number of actual parameters must match the number of formal parameters in the subprogram’s signature.The number of formal parameters is defined when the subprogram is declared, and it remains constant for all invocations.
OrderThe order of actual parameters must correspond to the order of formal parameters in the subprogram’s parameter list.The order of formal parameters is determined by their declaration sequence in the subprogram’s header.
UsageActual parameters are used to pass values from the calling program to the subprogram, providing input data.Formal parameters are used within the subprogram to receive and manipulate the values provided by actual parameters.
OverloadingOverloading based on parameter types is determined by the data types of actual parameters in the subprogram call.Overloading is determined by the number and data types of formal parameters in the subprogram’s declaration.
LifetimeActual parameters have a lifetime limited to the duration of the subprogram call.Formal parameters have a lifetime equivalent to the execution of the subprogram and persist as long as the subprogram is executing.

Key Takeaways

  • Actual Parameters are the values that are passed to the function when it is invoked while Formal Parameters are the variables defined by the function that receives values when the function is called.
  • Formal Parameters can be treated as local variables of a function in which they are used in the function header. Actual Parameters can be constant values or variable names.
  • The parameters written in function definition are known as formal parameters. The parameters written in function call are known as actual parameters.
  • Actual parameters are the variables or expressions referenced in the parameter list of a subprogram call. Formal parameters are the variables or expressions referenced in the parameter list of a subprogram specification.
  • When a method is called, the formal parameter is temporarily “bound” to the actual parameter. The method uses the formal parameter to stand for the actual value that the caller wants to be used.
  • Both Parameters are included inside the parenthesis.
  • Each parameter (actual and formal) is separated by a comma.