{smcl} {bf:Mata course: The programming language} {hline} {title:Only a few things to know} {hline} {bf:{help m2_if:if}} {cmd:if (}{it:exp}{cmd:)} ... {cmd:else} ... {bf:{help m2_for:for}} {cmd:for (}{it:exp1}{cmd:;} {it:exp2}{cmd:;} {it:exp3}{cmd:)} {it:stmt} {bf:{help m2_while:while}} {cmd:while (}{it:exp}{cmd:)} {it:stmt} {bf:{help m2_do:do}} {cmd:do} ... {cmd:while (}{it:exp}{cmd:)} {bf:{help m2_break:break}} break out of {cmd:for}, {cmd:while}, or {cmd:do} {bf:{help m2_continue:continue}} continue with next iteration of {cmd:for}, {cmd:while}, or {cmd:do} {bf:{help m2_goto:goto}} {cmd:goto} {it:label} {bf:{help m2_return:return}} {cmd:return(}{it:exp}{cmd:)} {hline} {p 4 4 2} and then there are {bf:{help m2_declarations:declarations}} {title:Declarations} {p 4 8 2} 1. You do not need them. {p 4 8 2} 2. Using them makes your code easier to debug. {p 4 8 2} 3. Using them can make your code easier to understand in the future. {p 4 8 2} 4. Using them can make your code run faster. {title:What's a declaration} {p 4 4 2} Consider the following program {cmd:function silly()} {cmd:{c -(}} {cmd:return(2)} {cmd:{c )-}} {p 4 4 2} and then consider {cmd:real scalar silly()} {cmd:{c -(}} {cmd:return(2)} {cmd:{c )-}} {p 4 4 2} The {cmd:real scalar} part is a declaration. {p 4 4 2} You are not required to specify it. The program works either way. {p 4 4 2} Advantages of specification: {p 8 12 2} 1. It is now obvious that code returns a real scalar even without looking at the body (pretend the {cmd:return(2)} was really long). {p 8 12 2} 2. Mata can catch the errors in your program if it attempts to return anything but a real scalar (perhaps the {cmd:return(2)} part has a bug). {title:So what can a function return?} {p 4 4 2} A function can return {cmd:void} {p 4 4 2} or it can return column 1 column 2 {hline 40}{cmd} real scalar complex rowvector numeric colvector string vector pointer matrix transmorphic{txt} {hline 40} Choose one word from column 1 and one from column 2 {p 4 4 2} When you do not declare what a function returns, {cmd:transmorphic matrix} {p 4 4 2} is assumed. {title:Argument declarations} {p 4 4 2} Just as with return values, functions can declare the types of their arguments {cmd:real scalar add(real scalar a, real scalar b)} {cmd:{c -(}} {cmd:return(a+b)} {cmd:{c )-}} {p 4 4 2} or not {cmd:real scalar add(a, b)} {cmd:{c -(}} {cmd:return(a+b)} {cmd:{c )-}} {p 4 4 2} Advantages of argument declarations: {p 8 12 2} 1. As a user, I can more easily see how the function is intended to be used. {p 8 12 2} 2. If I make a mistake using the function, Mata can tell me. {title:So what can arguments be?} {p 4 4 2} Anything but {cmd:void}. {p 4 4 2} Choose a word from column 1, and a word from column 2. column 1 column 2 {hline 40}{cmd} real scalar complex rowvector numeric colvector string vector pointer matrix transmorphic{txt} {hline 40} {p 4 4 2} When you do not say what an argument is {cmd:transmorphic matrix} {p 4 4 2} is assumed. {title:Declaration of working variables} {p 4 4 2} In your program, you can introduce new variables as you need them: {cmd}real scalar getvalue(real colvector V, real scalar i, real scalar j) {c -(} if (j