ENG1060 Lecture 3: Week 3 Functns [reusable code]

50 views2 pages
Reusable code
!"#$%&'"()"%*"&+',-'*.+'-"/*'*."*'/,012*+34'/"%'3+1+"*'*.+'4"0+'02%("%+'*"4#'
,)+3'"%(',)+3'"&"$%
555'3+/,3('"'1"**+3%',-'/,(+'"%('3+24+'$*'6$*.'($--+3+%*'$%12*'"3&20+%*4
73$*+'/,(+'68'*.+'1231,4+'*."*'*.+'4"0+'/,(+'/"%'9+'24+('-,3'($--'13,94
:")+4'*$0+ $%';,%&'32%
:.,3*+3 /,(+
<+%+3$/' 555'"11;$/"9;+'*,'0"%='13,94
?
@++1'4$01;+'"%('40"3*
:*3"$&.*'*,'*.+'1,$%*
A"3$"9;+'"%('(=%"0$/
?
B+/,&%$4+'6.+3+')"3$"9;+4'/"%'9+'24+('$%'1;"/+',-'4*"*$/'%209+34
?
Function/file CD0E
F'"%'0G-$;+'68'$%12*4'"%(',2*12*4
.")+'*,'(+/;"3+'"4'"'-2%/*$,%'-$;+'6$*.'"'-2%/*$,%'.+"(+3
w
function outputs = function_name(inputs)
(,%H*'32% -2%/*$,%4I'J24*'+%423+'*."*'/233+%*'($3+/*,3='/,%*"$%4'
-2%/*$,%
!.+3+H4'%,'$%12*'-,3'-2%/*$,%4K'4,'6,%H*'6,3#D
w
!,'/";;'"'-2%/*$,%K'24+'" 4+1"3"*+8/,01;$0+%*"3='0G-$;+
FUNCTION/FILE/MUST/BE/NAME/OF/FUNCTION!
L$0'$4'*,'/";/2;"*+' ,2*12*4
M+-$%+',2*12*4'($3+/*;='9+;,6'-$34*';$%+III
w
>&D'N0$%'0"OP
X [4 -5 3 8; 1 9 5 4]
[K, S] = max(X)
K = [4 9 5 8], S = [1 2 2 1]
Q"O$020')";2+'$%'+"/.'/,;20%K'3,6'%209+3'-,3'+"/.')";2+
Functions
M$--+3+%/+'$%'.+"(+3',-'0G-$;+4
?
R4'0,(2;"3'
B+24+4 "'1"**+3%',-'/,(+',%'($--+3+%*'$%12*')";2+4
>"4$+3'*,'(,/20+%* "%('4."3+ 68',*.+34
?
S;"/#'9,O+4
S3+"#'21'"'13,9;+0'$%*,'40";;+3'0"%"&+"9;+'1"3*4
?
User-defined/functions
T")+'$%12*N4PK',2*12*N4PK'/"4+'4+%4$*$)+
U"%',%;='9+'/";;+('-3,0'/233+%*'($3+/*,3$+4
L;;'$%12*'"3&20+%*4'024*'9+'41+/$-$+('6.+%'/";;$%&'-2%/*$,%
% [above line without the word function]
% Written by: [name], [ID]
% Created on [date]
% [description of what the function does]
% [description of input argum and outputs]
V"0+4'4.,2;('9+W
:+;-G(,/20+%*$%&?
:"0+'%"0+ "4'0G-$;+III?
L),$('*.,4+'*."*'"3+'4$0$;"3'*,'92$;*G$%'-2%/*$,%4?
Multiple/inputs
>&D':$%+'!"=;,3'-2%/*$,%
function taylor_result = SineTaylor(x,n)
taylor_result = (-1n)^n / factorial(2*n+1) * x.^(2
*n+1)
C%'$4'"'4/";"3K'O'$4'"')+/*,3E
Multiple/outputs
V++('*,'9+'$%'" )+/*,3'CEK'+&D
function [drop_time, drop_velocity] = DebrisDrop
(conveyer_height)
drop_time = ---
drop_velocity = ---
Week$3:$Functns$[reusable$code]
:"*23("=K'X'L2&24*'YZ[\
Z]W^Z
Unlock document

This preview shows half of the first page of the document.
Unlock all 2 pages and 3 million more documents.

Already have an account? Log in

Document Summary

Taking advantage of the fact that computers can repeat the same mundane task over and over again. >>> record a pattern of code and reuse it with different input arguments. Write code w/ the purpose that the same code can be used for diff probs. Recognise where variables can be used in place of static numbers. = an m-file w/ inputs and outputs w w have to declare as a function file with a function header function outputs = function_name(inputs) don"t run functions! There"s no input for functions, so won"t work. To call a function, use a separate/complimentary m-file. Function file must be name of function! w. X [4 -5 3 8; 1 9 5 4] K = [4 9 5 8], s = [1 2 2 1] Maximum value in each column, row number for each value. Reuses a pattern of code on different input values. Break up a problem into smaller manageable parts.

Get access

Grade+
$40 USD/m
Billed monthly
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
10 Verified Answers
Class+
$30 USD/m
Billed monthly
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
7 Verified Answers

Related Documents