< Applied Programming

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.[1]

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 computers to advance beyond the limitations of predefined programs and outputs.

Types of variables

Static - A variable with a value that does not change during the run of the program, and has memory reserved for it upon compilation.

Dynamic - A variable with a value that can change depending on input and other variables in the program, and who’s address is determined upon compilation.

Global - A variable who’s value is defined outside of all functions in a program, and is accessible by all functions in the program.

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

Creating your own - see language examples

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.[2] Here are some examples:

C++, C#, and Java

int veggies = 23;

Python

veggies = int(23)

PHP

$veggies = 23;

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:[3]

  • 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 with 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. To do this in Python, the last line in 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 any 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:

sofritas = 1
guacamole = 1


def make_burrito():
    tortilla = 1
    veggies = 1
    rice = 2
    beans = 2
    burrito = guacamole + veggies + sofritas + rice + beans + tortilla
    return burrito


def make_taco():
    shell = 1
    lettuce = 1
    tomatoes = 1
    taco = guacamole + tomatoes + lettuce + sofritas + shell
    return taco


def make_dinner():
    burrito = make_burrito()
    taco = make_taco()


make_dinner()

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.

Variables as objects

What are objects, and how can variables be used as objects in a class?

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"

[4]

  1. https://en.wikiversity.org/wiki/Types_and_variables
  2. https://launchschool.com/books/ruby/read/variables
  3. https://www.kidscodecs.com/variables/
  4. https://www.w3schools.com/python/python_variables.asp
This article is issued from Wikibooks. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.