Bits & Bytes

Posts Tagged ‘constants’

Arguments and Parameters

The terms argument and parameter are frequently used interchangeably, and there is often confusion about what these two terms mean. Arguments and parameters are two different things, but they are closely related. By definition, an argument is a value or variable that is passed into a function, and a parameter is value or variable that is used inside of a function. For illustration, look at the program below.

#include <iostream>

int Sum(int iP1, int iP2) {
    return iP1 + iP2;
}

int main () {
    int iA1 = 48;
    int iA2 = 24;

    std::cout << Sum(iA1, iA2) << std::endl;

    return 0;
}

The program contains takes in two int values and returns an int that is the sum of them. The two int values that the function takes in are called arguments, while the two values that are used inside the function are called parameters. To clarify this, we have named the variables that we used for the arguments, iA1 and iA2, and the two variables that we used for the parameters, iP1 and iP2. Correspondingly, "iA1, iA2" inside the function call is called the argument list and "int iP1, int iP2" inside the function definition is called the parameter list.

In the example above, the difference between the arguments and paramters is clear. The arguments and parameters refer to totally different memory locations because the arguments are both passed by value. In the program below, we have replaced the variables with the constant literals 48 and 24. In this program, these literals are the arguments.

#include <iostream>

int Sum(int iP1, int iP2) {
	return iP1 + iP2;
}

int main () {

    std::cout << Sum(48, 24) << std::endl;

    return 0;
}

In our last example below, we changed the function a bit; we pass the first argument by reference and use a default argument for the second. So, the arguments are iA and 45. The parameters are still iP1 and iP2. However, the first parameter is only a reference so changing its value changes the value of iA, as we would expect. Passing values by reference is probably one of the sources of confusion between arguments and parameters, but it is easy to understand if we remember that the parameter is only a reference; in fact, we could use any valid C++ data type as a parameter, including pointers, constants, etc.

#include <iostream>

void AddTo(int& iP1, int iP2 = 45) {
    iP1 += iP2;
}

int main () {
	int iA = 16;
	AddTo(iA);
	std::cout << iA << std::endl;

    return 0;
}

Declaring Variables and Constants in Actionscript with Fundamental Types

The basic format for declaring variables in actionscript looks like this:

var <variable name>:<variable type>;

We begin with var to indicate that we are declaring a variable. Then, we put a space, the variable name, a colon, the variable type, and, finally, a semi-colon to end the line. So, for example, we could declare a variable named MyNum of the type Number like this:

var dMyNum:Number;

A Number is used for floating-point or decimal numbers. We can initialize our variables in the declaration like this:

var dMyNum:Number = 3.14159;

Additionally, we can declare constants, which should be initialized, like this:

const kdMyNum:Number = 3.14159;

Actionscript has few a fundamental types to hold common data elements. These types are String, Boolean, Number, int, and uint. Strings are used to hold text. Booleans are used to hold the logical values “true” or “false”. Numbers are used to hold any number, including non-integer numbers. ints are used to hold integers only. uints are used to hold unsigned integers only. An example declaration and initialization of each of these types is shown here:

var sMyString:String = "XoaX.net";
var bMyBool:Boolean = true;
var dMyNum:Number = 3.14159;
var iMyInt:int = -67;
var uiMyUint:uint = 34;

Our first variable declaration did not contain an initial value. Variables that are declared with no initial value assigned to them take the following default values, according to their type:

String: null
Boolean: false
Number: NaN or not a number
int: 0
uint: 0