Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Functions CS 103

2 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

3 In-Class Question What are the values of x and y in the Command Window? ???

4 In-Class Question What are the values of x and y in the Command Window?

5 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

6 Local Scope Command Window >> A = 13; >> B = my_function(A); 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.

7 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_name output = function_name(inputs) output = function_name function_name(inputs) function_name Defining a function: Same as the above but with the keyword function

8 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 return y = 1/x unless x is 0, in which case y = 0

9 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.

10 Handling Variable Arguments Matlab has a series of functions that can check what types of arguments are being passed nargin = number of input arguments nargout = 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

11 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

12 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

13 Creating a Primes Function What does the function need to do differently? We need to figure out how many input arguments were passed If 1, calculate primes from 1 to N If 2, calculate primes from M to N How many output arguments were requested? By default, we will always return the list of primes If 2, calculate the average

14 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)

15 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

16 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

17 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


Download ppt "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."

Similar presentations


Ads by Google