User-defined Functions (6/28) Pass-By-Value Example 1 ▣Command window: ▣my script..m: X=2; disp(Hello')方 my_script x=5; Hello! y=x+2 y= 7 @日济大学 TONGJIUNIVERSITY
Command window: x = 2; my_script Hello! y = x + 2 y = 7 my_script.m: disp( 'Hello' ); x = 5; Example 1 User-defined Functions (6/28) Pass-By-Value
User-defined Functions (7/28) Pass-By-Value Example 2 1 function out=sample(a,b) 2- fprintf('In sample:a=%f,b=%f %f\n',a,b) 3- a=b(1)+2*a: 4- b=a.*b; 5- out=a+b(1); 6- fprintf('In sample:a=%f,b=%f %f\n',a,b) 1- a=2:b=[64]: 2- fprintf('Before sample:a=%f,b=%f %f\n',a,b); 3- out=sample(a,b); 4- fprintf('After sample:a=%f,b=%f %f\n',a,b); 5- fprintf('After sample:out=%f\n',out); By execute the second M.file,what will appear in the command window? 翻凡济大学 TONGJI UNIVERSITY
Example 2 By execute the second M.file ,what will appear in the command window? User-defined Functions (7/28) Pass-By-Value
User-defined Functions (8/28) Pass-By-Value The answer: Before sample:a=2.000000,b=6.000000 4.000000 n sample:a=2.000000,b=6.0000004.000000 n sample:a=10.000000,b=60.00000040.000000 After sample:a=2.00000o,b=6.0000004.000000 After sample:out=70.000000 @日济大学 TONGJI UNIVERSITY
The answer: User-defined Functions (8/28) Pass-By-Value
User-defined Functions (9/28) Pass-By-Value MATLAB programs communicate with their functions using a Pass- by-value scheme. When a function calls occurs,Matlab makes a copy of the actual arguments and passes them to the function. Maybe the function modifies the copy,but it will not affect the original data in the caller. This feature helps to prevent unintended side effects in which an error in the function might unintentionally modify variables in the calling program. PHAW 细月济大学 TONGJI UNIVERSITY
MATLAB programs communicate with their functions using a Passby-value scheme. When a function calls occurs, Matlab makes a copy of the actual arguments and passes them to the function. Maybe the function modifies the copy ,but it will not affect the original data in the caller. This feature helps to prevent unintended side effects in which an error in the function might unintentionally modify variables in the calling program. User-defined Functions (9/28) Pass-By-Value
User-defined Functions (10/28) Optional Arguments Many MATLAB functions support optional input arguments and output arguments,such as plot,max. How do MATLAB functions know how many input and output arguments are present,and how do they adjust their behavior accordingly? There are 8 special functions that can be used by MATLAB functions to deal with the optional arguments and to report errors in those arguments.Here we will introduce6 of them and the remaining 2 will be introduced in the following lessons. 同濟大学 AW TONGJI UNIVERSITY
Many MATLAB functions support optional input arguments and output arguments, such as plot, max. How do MATLAB functions know how many input and output arguments are present, and how do they adjust their behavior accordingly? There are 8 special functions that can be used by MATLAB functions to deal with the optional arguments and to report errors in those arguments. Here we will introduce 6 of them and the remaining 2 will be introduced in the following lessons. User-defined Functions (10/28) Optional Arguments