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.

Slides:



Advertisements
Similar presentations
Lecture 5.
Advertisements

A MATLAB function is a special type of M-file that runs in its own independent workspace. It receives input data through an input argument list, and returns.
User-defined Functions Selim Aksoy Bilkent University Department of Computer Engineering
Q and A for Chapter 3.4 – 3.14 CS 104 Victor Norman.
Introduction to Matlab & Data Analysis
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Eight Sub and Function Procedures.
Chapter 14: Overloading and Templates C++ Programming: Program Design Including Data Structures, Fifth Edition.
An Introduction to Programming with C++ Fifth Edition
VBA Modules, Functions, Variables, and Constants
Example 2.
Chapter 6: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
CIS 101: Computer Programming and Problem Solving Lecture 5 Usman Roshan Department of Computer Science NJIT.
CIS 101: Computer Programming and Problem Solving Lecture 4 Usman Roshan Department of Computer Science NJIT.
Modules, Hierarchy Charts, and Documentation
Chapter 6: User-Defined Functions I
Chapter 15: Operator Overloading
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
Functions A function is a snippet of code that performs a specific task or tasks. You use a multitude of functions daily when you do such things as store.
Introduction to programming in MATLAB MATLAB can be thought of as an super-powerful graphing calculator Remember the TI-83 from calculus? With many more.
Programmer Defined Functions Matthew Verleger. Windows It’s estimated that Window’s XP contains 45 million lines of code (and it’s over 10 years old).
IE 212: Computational Methods for Industrial Engineering
C++ for Engineers and Scientists Second Edition Chapter 6 Modularity Using Functions.
1 Functions 1 Parameter, 1 Return-Value 1. The problem 2. Recall the layout 3. Create the definition 4. "Flow" of data 5. Testing 6. Projects 1 and 2.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
CSCI 130 Chapter 5 Functions. Functions are named uniquely Performs a specific task Is independent –should not interfere with other parts of program May.
Chapter 9: MuPAD Programming II Procedures MATLAB for Scientist and Engineers Using Symbolic Toolbox.
Function M-File Numerical Computing with. MATLAB for Scientists and Engineers.
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.
Recap Function M-files Syntax of Function M-Files Comments Multiple Input and Output Functions.
CHAPTER 5 FUNCTIONS I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Advanced Topics- Functions Introduction to MATLAB 7 Engineering 161.
COMP 116: Introduction to Scientific Programming Lecture 11: Functions.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
CPS120: Introduction to Computer Science Lecture 14 Functions.
JavaScript Scripting language What is Scripting ? A scripting language, script language, or extension language is a programming language.
Covenant College November 27, Laura Broussard, Ph.D. Professor COS 131: Computing for Engineers Chapter 5: Functions.
Procedural Programming Criteria: P2 Task: 1.2 Thomas Jazwinski.
Slide 1 of 19Session 13 Ver. 1.0 Querying and Managing Data Using SQL Server 2005 In this session, you will learn to: Implement stored procedures Implement.
2 Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
XP New Perspectives on XML, 2 nd Edition Tutorial 7 1 TUTORIAL 7 CREATING A COMPUTATIONAL STYLESHEET.
An Introduction to Programming with C++ Sixth Edition Chapter 10 Void Functions.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Stored Procedures / Session 4/ 1 of 41 Session 4 Module 7: Introducing stored procedures Module 8: More about stored procedures.
PHP Reusing Code and Writing Functions 1. Function = a self-contained module of code that: Declares a calling interface – prototype! Performs some task.
ENG College of Engineering Engineering Education Innovation Center 1 Functions 1 in MATLAB Topics Covered: 1.Uses of Functions Organizational Tool.
User-Defined Functions (cont’d) - Reference Parameters.
C allows programmer to define their own function according to their requirement. These types of functions are known as user-defined functions. Suppose,
 The real power of PHP comes from its functions; it has more than 1000 built-in functions.  PHP User Defined Functions  Besides the built-in PHP functions,
Parameter Passing: Arrays 1.Create new variables (boxes) for each of the formal parameters allocated on a fresh stack created for this function call. int.
Chapter 9: Value-Returning Functions
User-Written Functions
Method.
Scripts & Functions Scripts and functions are contained in .m-files
Stack Data Structure, Reverse Polish Notation, Homework 7
User-Defined Functions
User Defined Functions
C++ for Engineers and Scientists Second Edition
group work #hifiTeam
Variables and Arithmetic Operations
Coding Concepts (Sub- Programs)
Functions In Matlab.
CSCI N317 Computation for Scientific Applications Unit 1 – 1 MATLAB
MATLAB Programs Chapter 6 Attaway MATLAB 5E.
Presentation transcript:

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