{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Chapter5-1b

# Chapter5-1b - Functions Small Self Contained Computing...

This preview shows pages 1–7. Sign up to view the full content.

User Defined Functions

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Small Self Contained Computing Units Dedicated to a Single Computing Task Independent Test Reusable Code Isolation for Unintended Side Effects (Pass by Value) Functions
function [outarg1, outarg2, …] = fname (inarg1, inarg2, …) % FNAME (single line) comment line used by help system % Other descriptive comments (function description) … % description (Continued) …… (Executable code) …… return % early return to calling program …… end % function name – Optional but good practice Structure of a Function Dummy Arguments (Placeholders  for the actual values passed to/from  function

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
function [sum, diff] = sum_diff(a, b) % SUM_DIFF Calculates sum and difference of 2 numbers % sum = a + b; diff = a - b; c = sum; d = diff; fprintf('Inside the function: c = %g and d = %g\n', c, d) end % end sum_diff A Typical (Simple) Function Load Class Example 5_1
% Program test_sum_diff c = 7; d = 3; [e, f] = sum_diff(c,d); fprintf('The sum is %g and difference is %g.\n', e,f) fprintf('Back (outside) from the function: c = %g and d =  %g\n', c, d) Inside the function: c = 10 and d = 4 The sum is 10 and difference is 4. Back (outside) from the function: c = 7 and d = 3 A Typical (Simple) Function

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Sorting Function – Required to sort a vector row array of numbers in ascending order, e.g., 3 5 1 4 2 into 1 2 3 4 5. We’ll name it ‘ssort1’.
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern