Functions CS 103. Review A function is a set of code we can execute on command to perform a specific...
-
Upload
marshall-randall -
Category
Documents
-
view
215 -
download
2
Transcript of Functions CS 103. Review A function is a set of code we can execute on command to perform a specific...
Functions
CS 103
Review
A function is a set of code we can execute on command to perform a specific task
When we call a function, we can pass arguments (variables) to and from the function
By default, variables in Matlab are local in scope
In-Class Question
What are the values of x and y in the Command Window?
???
???
In-Class Question
What are the values of x and y in the Command Window?
Scope & Local Variables The scope of a variable defines what parts of a
program can access it
By default, all variables in Matlab are local in scope
Local scope means: Only the function or main program that created the
variable can read or modify that variable. To the rest of Matlab, that variable does not exist!
Local variables created by functions are deleted after the function ends
Local Scope
Command Window
>> A = 13;
>> B = my_function(A);
Function [B] = my_function(A)
B = sqrt(A);
A A B
B
Computer’s Memory
Arguments in a function can have the same name as those in the main program (or another function)….but they’re NOT the same variable.
Calling & Defining Functions The syntax for calling and defining functions takes several forms
depending on the number of input and output arguments
Calling a function: [output1, output2, …] = function_name(inputs)[output1, output2, …] = function_nameoutput = function_name(inputs)output = function_namefunction_name(inputs)function_name
Defining a function: Same as the above but with the keyword function
Problem
Write a function called safe_inverse()
This function will have one input argument and one output argument
If called this way: y = safe_inverse(x) , the function should returny = 1/x unless x is 0, in which case y = 0
Passing Arguments
You don’t always have to pass the arguments you define!
A function can be called with fewer arguments, but not with more arguments, than specified.
This variation in the number of arguments is handled with nargin and nargout.
Handling Variable Arguments
Matlab has a series of functions that can check what types of arguments are being passednargin = number of input argumentsnargout = number of output arguments
After using these functions to determine how many arguments have been passed, you have to write the code to handle the various possibilities
Example Problem:Finding Prime Numbers
We wish to find a series of prime numbers
Algorithm: Sieve of Eratosthenes (ca 240 BC)
Method: List the numbers from 1 to N Starting with 2, strike every multiple of 2 from the list Then strike the multiples of 3 Then 4, 5, 6, etc. all the way up to N The remaining list are prime numbers
Creating a Primes Function
Now, suppose we want to alter our script to make it a function
This function should allow us to find the prime numbers from 1 to N if the user gives us an N, or from M to N if the user gives us both
The function should return a list of prime numbers and, if requested, the average of those prime numbers
Creating a Primes Function
What does the function need to do differently?We need to figure out how many input
arguments were passedIf 1, calculate primes from 1 to NIf 2, calculate primes from M to N
How many output arguments were requested?By default, we will always return the list of primesIf 2, calculate the average
Using nargin
We can use nargin to determine the number of input arguments
Note that arguments are always passed in order!
ourprimes(10, 250) ourprimes(100)
Function [list_of_primes, avg]=our_primes(M, N)
Dynamic Polymorphism
A function where the output is determined during the running of the function by the type of the input arguments
Sometimes just the value is different, and sometimes the shape or type is different
Global Variables Variables which can be used by all of Matlab,
including user-defined functions
Syntax: global variable_name
The global declaration should be placed in your main program and functions that will use it
Global variables should not be used as arguments passed to a function
Global Scope% Main Program
global x
X = 15;
A = 13
B = my_function(A)
% The Function
Function [B] = my_function(A)
global x
B = sqrt(A);
x = 10;
A A B
B X
Computer’s Memory
Arguments in a function can have the same name as those in the main program (or another function)….but they’re NOT the same variable unless defined as GLOBAL