Presentation is loading. Please wait.

Presentation is loading. Please wait.

Variables Title slide variables.

Similar presentations


Presentation on theme: "Variables Title slide variables."— Presentation transcript:

1 Variables Title slide variables

2 Content What is a variable? Data types
Mutability (constant vs variable) Scope and visibility Preallocation Content of the presentation: What is a variable? Data types Mutability (constant vs variable) Scope and visibility Preallocation

3 What is a variable Location where a value is stored Variable: a
Address Value 000 NULL 001 5 002 8 003 Variable: a A variable is the location of where a value is stored. So for the computer the variable represent a physical address in the memory, in which the value (that we associate with the variable) is stored.

4 What is a variable Location where a value is stored
Every variable has: Type (statically typed languages) Variable name (identifier) Value Every variable has an associated type (in statically typed languages), a variable name and a value. So in the java example of ‘int a = 5;’, int is the type, a is the variable name and 5 is the value.

5 Data types Integer (int) Floating point number (float)
Name Shorthand What Integer int Whole numbers Floating-point number float 32-bit number with digits after decimal place Double precision float double 64-bit number with digits after decimal place Boolean (logical in MATLAB) bool (logical in MATLAB) Value that’s either true or false (0 or 1) Array (1D is a vector) ~ A list of variables, depending on the language this can be further divided into lists, arrays, vectors, queues, etc. Character char A single (ASCII) character String string or str A list or array of characters Integer (int) Floating point number (float) Double precision float (double) There are many different types, this slide lists the most important ones: Integer (int): whole numbers Floating-point number (float): 32-bit number with digits after decimal place Double precision float (double): 64-bit number with digits after decimal place Boolean (bool; logical in MATLAB): Value that’s either true or false (0 or 1) Array (1D usually called a vector, sometimes list): A list of variables, depending on the language this can be further divided into lists, arrays, vectors, queues, etc. Character (char): A single (ASCII) character String (string or str): A list or array of characters

6 Typing Statically typing Languages: C (Igor), Java, PEARL
Variable has one type Type preassigned Programming languages can roughly be divided into two: statistically typed languages and dynamically typed languages Statistically typed languages, such as C, Java and PEARL have the type associated with the variable. The type is preassigned, usually explicitly in the language (etc. int a = 5; (Java), where a is associated with the type int)

7 Typing Statically typing Dynamically typing
Languages: C (Igor), Java, PEARL Variable has one type Type preassigned Dynamically typing Languages: MATLAB, Python, R, Perl Value has one type Type of variable may change Secondly there are dynamically typed languages, such as MATLAB, Python, R and Perl. They have the type associated with the value, not the variable, so the type of the variable may change.

8 Type casting Explicitly converting type of a variable or value
Only in dynamically typed languages Especially relevant for numerical types Reasons for casting: Increase precision/memory Reduce precision Cast base type to derived type Type casting is the act of transforming the type of a variable or value. The type of a variable can only be changed in dynamically typed languages. I find this especially useful for numerical types, for example if I want to increase my precision (cast from int to double, or double to float) or decrease my precision (double to int). Another application is to cast base types to derived types (object-oriented programming)

9 Mutability Variable Constant Value dynamically assigned
Value may change during run-time Constant Value assigned at initialization Value cannot be changed In explicit languages, different levels of mutability exist for variables. A variable is dynamically assigned, and thus the value may change during run-time. An constant is assigned at initialization and cannot be changed during run-time. An identifier shows the mutability (i.e. const float PI = 3.14; is a constant).

10 Mutability Depending on the language more subtypes available
Other languages don’t have constant modifiers Workarounds if needed Use naming conventions: ALL_CAPS Some languages offer more subtypes of mutability (e.g. java has protected). Other languages don’t allow constants. There are usually workarounds, but otherwise you could use the naming convention of using ALL_CAPS for constants.

11 Mutability: why use constants?
Users are stupid! Reminder for yourself Physical constants (pi, N of Avogadro, etc) Why would you use constants? If your program is going to be used by users, they will find a way to mess up variables. It is also good as a reminder for yourself that certain variables should not be changed. Lastly, it is often used for physical constants such as pi, the number of Avogadro, etc.

12 Scope Lifetime of a variable MATLAB is an exception
Important for memory allocations The scope determines the lifetime of a variable; or another way to look at it, were the computer can read the variable. Matlab is an exception and will have its own slide. Scope is important for memory allocations, and it’s good to keep an eye on it.

13 Scope Output: ? In this Java example, a for-loop is shown where the println displays the value of i. The question is what the output in each case is. For(int I = 0; I < 10; i++) { System.out.println(i); } Output: ?

14 What is happening? Memory cleared:
Memory for an int allocated Initialization: i is created Memory cleared: Space in memory available for new assignments In the same example, it shows that I is created within the for-loop in the line ‘int I = 0;, at which point memory is allocated for it. After the loop has ran, this memory will be cleared, so I no longer exists in the second println. The scope of I is the for-loop, in only exists inside the forloop, not outside of it. i no longer exists

15 Function example Output: ? Output: ?
The same goes for funcions. In this example a function myFunction is declared, which takes an integer and adds 100 to it, after which it returns it. If I make a variable int a = 100 and apply the function to it: res = myFunction(a);, what is the value of res and the value of a afterwards? Res will be 200, while a will remain 100.

16 Levels of scope Blocks (not all languages!) Functions Module/Class
Global Different levels of scope exist. Some languages have block scope, where every block of code (usually denoted using curly brackets), has its own scope. Functions have their own scope (variables within a function cannot be accessed unless they are exported. Modules and classes usually have their own scope. Lastly the global scope means a variable is always available.

17 Scope modifiers Most language allow you to make a variable global
Automatic if outside function Only use in dire need! In most languages there are ways to make the variables global, or define the scope of a variable. Usually you do not want to make a variable global, so only use it in dire need. This is matlab example using the function: function addValue(v) global i; i = i + v; end When executing the lines: i = 0; addValue(5); disp(i) addValue(3); What values of I will be displayed? The values will be added to i, because it is global, so this global I will be modified. It will thus be 5 and then 8.

18 MATLAB Workspace shows scope
Variables don’t get deleted after for-loop Each function has it’s own workspace Matlab is a special case, because the scope is explicit in the workspace. It furthermore doesn’t delete variables after a loop. However, it does have function scope, where each function has its own workspace, showing variables in its scope (access using debug function). Variables initiated, so available in scope

19 A word on preallocation
You might have gotten this error (MATLAB): What’s going on here? A word on preallocation. You might have gotten a MATLAB warning that you have a variable that changes size on every loop iteration. This warning suggests you should preallocate.

20 Allocation example Allocate space for empty list (say 8 bit)
Add int to the allocated space, so need 8 bits more Memory not always linear! An example of allocation in matlab: newArray = []; For I = 1:10 newArray = [newArray, i]; End In this case, the size of newArray changes on each iteration. After initialization it’s an empty list (roughly 8 bits). On each iteration, the computer has to find an extra 8 bits for the value that is added. Since memory is not always linear, this can take a lot of overhead. On small iterations like this, it’s not a big problem, but it will be on very big iterations (e.g. a million times).

21 What to do? No problem if small array
Preallocate the largest possible number of values What can you do about this? As said, it’s not a big problem for smaller arrays and smaller iterations, so usually it’s fine to leave it. However, a better practice is the allocate the largest amount of values you think will be added to the array, e.g.: newArray = nan(10,1);

22 What to do? No problem if small array
Preallocate the largest possible number of values Preallocate in chunks (i.e. every 1000 values add an extra values) Another solution is to preallocate in chunks. So instead of letting the computer find memory on each iteration, preallocate say a 1000 values, and each time you hit this limit, preallocate another 1000.

23 Summary What is a variable? Data types
Location where a value is stored Data types Statically vs dynamically Type casting Mutability (constant vs variable) Single value for a variable Scope and visibility Memory allocation MATLAB: use functions! Preallocation May speed up your code Summary, things we discussed: What is a variable? Location where a value is stored Data types Statically vs dynamically Type casting Mutability (constant vs variable) Single value for a variable Scope and visibility Memory allocation MATLAB: use functions! Preallocation May speed up your code


Download ppt "Variables Title slide variables."

Similar presentations


Ads by Google