Chapter5-1b - Functions Small Self Contained Computing Units Dedicated to a Single Computing Task Independent Test Reusable Code Isolation for

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

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

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

View Full DocumentRight 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
Background 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
Background image of page 3

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

View Full DocumentRight 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
Background 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
Background image of page 5

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

View Full DocumentRight Arrow Icon
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’. A Typical (Real) Function - I
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 09/13/2010 for the course EGR EGR115 taught by Professor Newman during the Spring '10 term at Embry-Riddle FL/AZ.

Page1 / 18

Chapter5-1b - Functions Small Self Contained Computing Units Dedicated to a Single Computing Task Independent Test Reusable Code Isolation for

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online