Functions CS 103. Review A function is a set of code we can execute on command to perform a specific...

17
Functions CS 103

Transcript of Functions CS 103. Review A function is a set of code we can execute on command to perform a specific...

Page 1: 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.

Functions

CS 103

Page 2: 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.

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

Page 3: 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.

In-Class Question

What are the values of x and y in the Command Window?

???

???

Page 4: 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.

In-Class Question

What are the values of x and y in the Command Window?

Page 5: 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.

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

Page 6: 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.

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.

Page 7: 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.

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

Page 8: 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.

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

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

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.

Page 10: 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.

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

Page 11: 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.

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

Page 12: 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.

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

Page 13: 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.

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

Page 14: 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.

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)

Page 15: 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.

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

Page 16: 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.

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

Page 17: 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.

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