Functions CS 103 March 3, 2004
Review A function is a set of code we can execute on command to perform a specific task 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 When we call a function, we can pass arguments (variables) to and from the function By default, variables in Matlab are local in scope By default, variables in Matlab are local in scope
Polling Question Given the following code, what is the value of x after the main program has run? Given the following code, what is the value of x after the main program has run? MAIN PROGRAM x = 5; y = test_function(x) function [a] = test_function(b) x = 10 a = x* b x = 2 b = 4 y = 1
Scope & Local Variables The scope of a variable defines what parts of a program can access it The scope of a variable defines what parts of a program can access it By default, all variables in Matlab are local in scope By default, all variables in Matlab are local in scope Local scope means: Local scope means: Only the function or main program that created the variable can read or modify that variable. To the rest of Matlab, it’s like that variable doesn’t exist Only the function or main program that created the variable can read or modify that variable. To the rest of Matlab, it’s like that variable doesn’t exist Local variables created by functions are deleted after the function ends Local variables created by functions are deleted after the function ends
Local Scope % Main Program A = 13 B = my_function(A) % The Function Function [B] = my_function(A) B = sqrt(A); AAB 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 one of four forms depending on whether there are input or output arguments The syntax for calling and defining functions takes one of four forms depending on whether there are input or output arguments Calling a function: [outputs]=function_name(inputs) [outputs]=function_name function_name(inputs) function_name Calling a function: [outputs]=function_name(inputs) [outputs]=function_name function_name(inputs) function_name Defining a function: function [outputs]=function_name(inputs) function [outputs]=function_name function function_name(inputs) function function_name Defining a function: function [outputs]=function_name(inputs) function [outputs]=function_name function function_name(inputs) function function_name
Polling Question Write a function called safe_inverse() Write a function called safe_inverse() This function will have one input argument and one output argument This function will have one input argument and one output argument If the function were called y=safe_inverse(x) then the function should return y = 1/x unless x = 0, in which case y = 0 If the function were called y=safe_inverse(x) then the function should return y = 1/x unless x = 0, in which case y = 0
Passing Arguments You don’t always have to pass the arguments you define! 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 A function can be called with fewer arguments, but not with more arguments than specified
Handling Variable Arguments Matlab has a series of functions that can check what types of arguments are being passed Matlab has a series of functions that can check what types of arguments are being passed nargin = number of input arguments nargin = number of input arguments nargout = number of output arguments nargout = number of output arguments nargchk(min,max,nargin) can check for too few arguments, but not for too many nargchk(min,max,nargin) can check for too few arguments, but not for too many After using these functions to determine how many arguments have been passed, you have to write the code to handle the various possibilities 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 We wish to find a series of prime numbers Algorithm: Sieve of Eratosthenes (ca 240 BC) Algorithm: Sieve of Eratosthenes (ca 240 BC) Method: Method: List the numbers from 1 to N List the numbers from 1 to N Starting with 2, strike every multiple of 2 from the list Starting with 2, strike every multiple of 2 from the list Then strike the multiples of 3 Then strike the multiples of 3 Then 4, 5, 6, etc. all the way up to N Then 4, 5, 6, etc. all the way up to N The remaining list are prime numbers The remaining list are prime numbers
Creating a Primes Function Now, suppose we want to alter our script to make it a 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 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 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? What does the function need to do differently? We need to figure out how many input arguments were passed We need to figure out how many input arguments were passed If 1, calculate primes from 1 to N If 1, calculate primes from 1 to N If 2, calculate primes from M to N If 2, calculate primes from M to N How many output arguments were requested? How many output arguments were requested? By default, we will always return the list of primes By default, we will always return the list of primes If 2, calculate the average If 2, calculate the average
Using nargin We can use nargin to determine the number of input arguments We can use nargin to determine the number of input arguments Note that arguments are always passed in order! 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 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 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 Variables which can be used by all of Matlab, including user-defined functions Syntax: global variable_name Syntax: global variable_name The global declaration should be placed in your main program and functions that will use it 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 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; AAB BX 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