Scope

Created

Scope (local vs global variables)


Data and Memory

When a variable is declared, the computer sets aside memory in which to store the associated data. We can read and alter the data in this memory location during the course of the program.

  • How long is this memory devoted for storing this data? Some data is needed for long-term, while other data is only required during the course of computing some intermediate result.

Scope

Each variable has a context in which it is meaningful. Outside of this context, it cannot be referred to or used.

  • A variable declared in a script m-file has a scope on the "level" at which the script is executed.
  • A funciton paramemter is in local scope from the beginning of the function body until the end of the function.

Local Scope Functions of a local scope.

  • Only variables within the function that are input, output, or explicitly declared inside the function are accessible.
    • This make function definitions self-contained, meaning they do not depend on the details of what is going on outside the function.
    • These are said to be in local scope from the beginning of the function body or its first use to the end of the function.

Life time is the span of time during which a particular data object can be accessed. At he end, the data is lost and memory is returned for use.

Pass-by-values and variable scope For a function call, function[out1, out2] = function_name(in1, in2), the values to the right of the function's name in the call are copied into new variables with names given in the function definition. These new variables are local, meaning they have different memory locations.

  • Changing the values in the input variables do not affect the caller

Circumventing Scope and Persistent Variables Persistent variables stay around from one call of a function to the next, but are limited in scope to that function (similar to static). They are not destroyed once a function ends.

function x = count()
	persistent num = [] % this is empty
	if isempty(num) % used to instantiate 
		num = 1
	else 
		num = num + 1
	end
	x = num;
end

Global variable

NOT recommended unless you are passing large amounts of data that you do not want to be updated. Global memory is available in all fucntions where the global declaration is included. It persists after the function ends.

Subfunctions

In a single m-file, more than one function can be included. Only the top function, with the name matching the file name, is callable from the outside.

  • This is the primary function.
  • All other functions all called subfunctions, which can only be called by the functions defined in this file.
function functionone(x)
.
.
end

function functiontwo(y)
.
.
end

function functionthree(z)
.
.
end

If each function is saved in a single m-file,

Nested Functions

Nested functions occur entirely within the definition of another function, called its host function. It is different than a subfunction because it can access all the variables of its host function.

  • A nested function can be called from its host or its host's subfunctions.
  • Terminate every function with the end statemnent.

functionA.m

function functionA(x) % define primary  
.  
.

	function functionB(y) 
	.  
	.  
	end

	function functionC(z) 
	.  
	end

end

Naming

It is possible to have multiple functions with the same name. In these cases, the order in which various locations are checked to find the function determines which one is called. The order:

  1. Nested Functions
  2. Subfunctions
  3. Current Directory