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.

Slides:



Advertisements
Similar presentations
Question Bank. Explain the syntax of if else statement? Define Union Define global and local variables with example Concept of recursion with example.
Advertisements

Introduction to Programming using Matlab Session 2 P DuffourJan 2008.
Lecture 5.
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
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Eight Sub and Function Procedures.
An Introduction to Programming with C++ Fifth Edition
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.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Chapter 6: User-Defined Functions I
Promoting Code Reuse Often in programming, multiple procedures will perform the same operation IN OTHER WORDS – the same piece of code will do the same.
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.
IE 212: Computational Methods for Industrial Engineering
1 Web-Enabled Decision Support Systems Objects and Procedures Don McLaughlin IE 423 Design of Decision Support Systems (304)
Using Arrays and File Handling
Functions 1 ENGR 1181 MATLAB 14.
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.
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 March 3, 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.
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.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Chapter 10 Introduction to Classes
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.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Chapter 6 Review: User Defined Functions Introduction to MATLAB 7 Engineering 161.
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.
CSCI 171 Presentation 6 Functions and Variable Scope.
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.
User-Defined Functions in MATLAB. What is a function? From the conceptual standpoint, a “function” is a blackbox that transforms “input arguments” to.
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.
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
Recap Functions with No input OR No output Determining The Number of Input and Output Arguments Local Variables Global Variables Creating ToolBox of Functions.
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.
JavaScript and Ajax (JavaScript Functions) Week 5 Web site:
 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,
Chapter 9: Value-Returning Functions
3 Introduction to Classes and Objects.
Method.
Scripts & Functions Scripts and functions are contained in .m-files
User-Defined Functions
User Defined Functions
C++ for Engineers and Scientists Second Edition
group work #hifiTeam
Functions In Matlab.
CSCI N317 Computation for Scientific Applications Unit 1 – 1 MATLAB
Using Script Files and Managing Data
JavaScript: Introduction to Scripting
 A function is a named sequence of statement(s) that performs a computation. It contains  line of code(s) that are executed sequentially from top.
Presentation transcript:

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

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

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

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

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; 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