{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

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

View Full Document Right Arrow Icon
User Defined Functions
Image of page 1

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

View Full Document Right Arrow Icon
Small Self Contained Computing Units Dedicated to a Single Computing Task Independent Test Reusable Code Isolation for Unintended Side Effects (Pass by Value) Functions
Image of page 2
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
Image of page 3

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

View Full Document Right Arrow Icon
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
Image of page 4
% 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
Image of page 5

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

View Full Document Right Arrow Icon
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’.
Image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    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.

    Student Picture

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

  • Left Quote Icon

    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.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    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.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern