Applied Programming/Variables

What are variables?
A variable is a named piece of computer memory, containing some information inside. Think of a variable as a box with a name, where we can "store" something. We create, edit, and delete variables, as much as we need in our tasks.

In the following example, we create a variable with the identifier "my_variable" and store the number 13 within it. We then print out "my_variable" and receive the number 13 in return. my_variable = 13 print(my_variable) >13

How are they used?
Variables are useful when you need to store, modify, or call information during the execution of programs. In essence, variables are the lifeblood of computer programming because they can store inputs and computational results. They allow for more flexibility in design and operation.

Types of variables
Local - A variable whose value is defined inside of a single function, and therefore is not accessible by other functions in the program.

Global - A variable whose value is defined outside of the functions, and therefore is accessible by all functions in the program.

Dynamic - A variable whose memory location is determined when the program runs.

Static - A variable whose memory is reserved for it during compilation.

Constant - A variable? While there are similarities between the two, a constant is markedly different in that its value never changes.

Data types
String - multiple characters that can include letters, numbers or special characters. Integer - a whole number (no decimal or fraction) Float - a number with or without a decimal; 32 bit precision (or accurate to approximately 8 decimal places) Double - a number with or without a decimal; 64 bit precision (or accurate to approximately 16 decimal places)

Boolean - data type who’s value can only be true or false

Character - data type consisting of a single Unicode character Mixed - any combination of the above data types

Declaring and initializing variables
The standard format for declaring and initializing a variable has the name identifier on the left and the value on the right. When you initialize the variable, you normally use the = symbol. It goes in between the name identifier and the value. Single whitespaces are used to separate the elements within the variable's declaration/initialization. This is done to increase code readability and understandability. Here are some examples:

C++, C#, and Java Python PHP It is not always a requirement to initialize or give a value to a variable during the declaration. There may be circumstances when it is preferable to declare a variable without initializing it; perhaps the variable will hold a lot of data and it is wiser to conserve memory resources until necessary. An example in C++:

int veggies;

Different programming languages have their own naming conventions, some of these overlap. Here are some universal tips to keep in mind when creating variables:
 * It is important to use descriptive names for variables.
 * Variable names must begin with a letter, underscore, or non-number character.
 * Do not use a comma for decimal numbers, only the period or point.
 * Every language has reserved words that cannot be used in variable names, for example, Date.

Functions
Functions are structuring elements that simultaneously group and isolate portions of code. Within functions, variables are used for inputting and outputting data. If a function does not specify it, any calculations stored within variables will be lost if the function does not return the value upon conclusion. This is called the return statement. To do this in Python, the last line of the function definition simply states: return something

Subsequent functions can use these return values if stated so within their definition by using a set of parenthesis, known as parameters. The variable holding the return values gets stated within the parameters. Like so:

def subsequent_function(something):

The return values or arguments stored within the variable 'something' can now be used by 'subsequent_function'. Functions are not required to have return values.

The timing or location of a variable's declaration/initialization within a program affect its operation. This applies to the concept of functional scope. For instance, a variable that is declared/initialized outside of the program's function sets becomes a global variable. All of the functions within a program will have access to use the global variables. Here's some food for thought: In this Python example, the chef does not have access to lettuce and tomatoes when making the burrito. Likewise, the chef can't use veggies, rice, and beans for making the taco. These are examples of variables with local scope. The sofritas and guacamole represent global variables; the chef has access to both when making the burrito and the taco.

Activities
1. Create a variable named carname and assign the value Volvo to it.

2. Create a variable named x and assign the value 50 to it.

3. Display the sum of 5 + 10, using two variables: x and y.

4. Create a variable called z, assign x + y to it, and display the result.

5. Remove the illegal characters in the variable name: 2my-first_name = "John"

6. Insert the correct syntax in the boxes to assign the same value to all three variables in one code line. x □ y □ z □ "Orange"

7. Insert the correct keyword in the box to make the variable x belong to the global scope. def myfunc: □ x    x = "fantastic"

Key Terms
Arguments - The actual input expression passed/supplied to a function, procedure, or routine in the invocation/call statement.

Array - A list or group of similar types of data values that are grouped.

Assignment - Sets the value saved in the storage location denoted by a given variable name.

Boolean - A data type having two values, typically denoted true and false.

Constant - A value that cannot be altered by the program during normal execution.

Data type - A classification of data which tells the compiler or interpreter how the programmer intends to use the data.

Declaration - A language construct that specifies the properties of a given identifier.

Double - A computer number format, usually occupying 64 bits in computer memory; it represents a wide dynamic range of numeric values by using a floating radix point.

Dynamic variable - A variable whose address is determined when the program is run.

Expression - A combination of one or more explicit values, constants, variables, operators, and functions that a programming language interprets and computes to produce another value.

Floating point - The formulaic representation that approximates a real number to a fixed amount of significant digits.

Function / Subroutine - A sequence of program instructions that performs a specific task, packaged as a unit.

Global variable - A variable with global scope, meaning that it is visible (hence accessible) throughout the program, unless shadowed.

Identifier - Tokens (also called symbols) which name the language entities (variables, types, labels, subroutines, and packages).

Initialize - The assignment of an initial value for a data object or variable.

Integer - A number that can be written without a fractional component.

Local variable - A variable that is given local scope.

Modulus - The remainder after division of one number by another.

Naming conventions - A set of rules for choosing the character sequence to be used for identifiers which denote variables, types, functions, and other entities in source code and documentation.

Objects - A combination of related variables, constants and other data structures which can be selected and manipulated together.

Operator - A programming language construct that performs a calculation from zero or more input values to an output value.

Order of operations - A collection of rules that reflect conventions about which procedures to perform first in order to evaluate a given mathematical expression.

Parameters - A special kind of variable used in a subroutine to refer to one of the pieces of data (values) provided as input to the subroutine.

Pointer - A variable that contains the address of a location in the memory. The location is the commencing point of an object, such as an element of the array or an integer.

Program - As an organized collection of instructions, which when executed perform a specific task or function. It is processed by the central processing unit (CPU) of the computer before it is executed.

Real number - A value that represents a quantity along a line, including integers, fractions, and irrational numbers.

Reserved word - A word that cannot be used as an identifier, such as the name of a variable, function, or label – it is "reserved from use".

Return statement - Code that causes execution to leave the current subroutine and resume at the point in the code immediately after the instruction which called the subroutine.

Scope - The part of a program where the name binding is valid, that is where the name can be used to refer to the entity.

Statement - The smallest standalone element of an imperative programming language that expresses some action to be carried out.

Static variable - A variable that has been allocated "statically", meaning that its lifetime (or "extent") is the entire run of the program.

String - A sequence of characters, either as a literal constant or as some kind of variable.

Subroutine - A sequence of program instructions that performs a specific task, packaged as a unit.

Variable - A storage location paired with an associated symbolic name (an identifier), which contains some known or unknown quantity of information referred to as a value.

Whitespace character - A character that does not correspond to a visible mark, but typically does occupy an area on a page.