ฟังก์ชันในไซแลบ ติวเตอร์เรียล 11
Tutorial 11 – Functions in Scilab
ฟังก์ชันจัดให้มีจุดประสงค์เดียวกันในไซแลบและในภาษาโปรแกรมอื่นๆ โดยเป็นบล็อกของโค๊ดที่เป็นอิสระ ด้วยมีพารามีเตอร์อินพุตและเอ้าพุตของตัวเอง ซึ่งสามารถเกี่ยวพันธ์กับตัวแปรในเวลาที่เรียกใช้ฟังก์ชัน เป็นการพัฒนาโปรแกรมให้เป็นโมดูล และจัดกลุ่ม(encapsulate) ชุดของประโยคคำสั่ง และให้เกี่ยวพันธ์กับชื่อของฟังก์ชัน ไซแลบได้จัดเอดิเตอร์ผนึกรวมไว้ภายในไซแลบเรียกว่าไซแพด(SciPad)
wherein the user can type the code for functions and compile and load them into the workspace. SciPad can be invoked by clicking on Applications Editor on the main menu at the top of the Scilab work environment. Let us write a simple function to calculate the length of a line in the x-y plane, given the coordinates of its two ends (x1, y1) and (x2, y2). The length of such a line is given by the expression l= x2−x1 2 y2−y1 2 . Let us first try out the calculation in Scilab for the points (1,4) and (10,6). -->x1=1; y1=4; x2=10; y2=6; -->dx=x2-x1; dy=y2-y1; -->l=sqrt(dx^2 + dy^2) l = 9.2195445 Note that x1, y1, x2 and y2 are input values and the length 'L' is the output value. To write the function, proceed as described below: 1. Open SciPad by clicking on Application Editor on the main menu. 2. Type the following lines of code into SciPad and define the function len(): function [L] = len(x1, y1, x2, y2) dx = x2-x1; dy=y2-y1; L = sqrt(dx^2 + dy^2); endfunction In the code above, function and endfunction are Scilab keywords and must be typed exactly as shown. They signify the start and end of a function definition. The variable enclosed between square brackets is an output parameter, and will be returned to Scilab workspace (or to the parent function from where it is invoked). The name of the function has been chosen as len (short for length, as the name length is already used by Scilab for another purpose). The input parameters x1, y1, x2 and y2 are the variables bringing in input from the Scilab workspace (or the parent function from where it is called). 3. Save the contents of the file to a disk file by clicking File Save in SciPad and choose a folder and name for the file. Note both the name and folder for later use. 4. Load the function into Scilab by clicking on Execute Load into Scilab on the SciPad main menu. Tutorial 11 – Functions in Scilab | 32 This function can be invoked from Scilab prompt as follows: -->ll = len(xx1, yy1, xx2, yy2) where ll is the variable to store the output of the function and xx1, yy1, xx2 and yy2 are the input variables. Note that the names of the input and output variables need not match the corresponding names in the function definition. With version 5 of Scilab, output from statements in a function are never echoed, irrespective of whether the statements end with the semicolon or not. Use the function disp() to print out intermediate results in order to debug a function. To use the function during subsequent sessions, load it into Scilab by clicking on Execute Load into Scilab in the main menu. If there are no syntax errors in your function definition, the function will be loaded into Scilab workspace, otherwise the line number containing the syntax error is displayed. If it is loaded successfully into the Scilab workspace, you can verify it with the command whos -type function and searching for the name of the function, namely, len. We can improve the above function by representing a point as an array of size 1x2, where the elements represent the x and y coordinates of the point. This way, the function can be rewritten as follows: function [L] = len(p1, p2) L = sqrt(sum((p2 – p1).^2)) endfunction The modified function can be used as follows: -->p1 = [0, 0]; p2 = [4, 3]; -->ll = len(p1, p2) ll = 5. Scilab allows the creation of in-line functions and are especially useful when the body of the function is short. This can be done with the help of the function deff(). It takes two string parameters. The first string defines the interface to the function and the second string that defines the statements of the function. Several statements can be separated by semi colons and written as a single string. The above modified function to calculate the length could be defined inline as follows: -->deff('[L]=len(p1, p2)', 'L=sqrt(sum((p2-p1).^2))'); Scilab allows the user to call a function with fewer input parameters than the number of input arguments in the function definition. It is an error to provide more input parameters than the number of input arguments. The same is the case with output parameters and output arguments. It is possible to inquire and find the number of input and output arguments from code within the function. The function argn() returns the number of input and output arguments, which in turn can be used inside the function body to take decisions such as assigning default values in case the Tutorial 11 – Functions in Scilab | 33 input argument has not been supplied. Using variables that have not been supplied as arguments results in an error and the function being aborted. function [a, b, c] = testarg(x, y, z) [out, inp] = argn(0); a = 0; b = 0; c = 0; mprintf(“Output: %d\n”, out); mprintf(“ Input: %d\n”, inp); endfunction We can now load the above function into Scilab workspace and call it with different number of input and output parameters and see how Scilab responds: -->[a, b, c] = testarg(1, 2, 3); Output: 3 Input: 3 -->[a, b, c] = testarg(1, 2); Output: 3 Input: 2 -->[a, b] = testarg(1); Output: 2 Input: 1 -->testarg(1); Output: 1 Input: 1 -->testarg(1, 2, 3, 4); !--error 58 Wrong number of input arguments Arguments are : x y z -->[a,b,c,d]=testarg(1,2) !--error 59 Wrong number of output arguments Arguments are : a b c Supplying fewer arguments than required is acceptable as long as that variable is not being used within the function, but then specifying an input variable and not using it would be pointless. But, checking the number of input arguments and assigning default values to those not supplied is a good use of the argn() function. Supplying more arguments than required is an error and function execution is aborted. Supplying fewer output arguments than required is acceptable, but the values of variables that are not caught are lost for subsequent use. Supplying more output arguments than required is an error and function execution is aborted. Tutorial 11
ไม่มีความคิดเห็น:
แสดงความคิดเห็น