📄 m2_return.hlp
字号:
{smcl}
{* 29mar2005}{...}
{cmd:help m2 return}
{hline}
{* index return tt}{...}
{title:Title}
{p 4 4 2}
{bf:[M-2] return -- return and return(exp)}
{title:Syntax}
{cmd:return}
{cmd:return(}{it:exp}{cmd:)}
{title:Description}
{p 4 4 2}
{cmd:return} causes the function to stop execution and return to the
caller, returning nothing.
{p 4 4 2}
{cmd:return(}{it:exp}{cmd:)} causes the function to stop execution and return
to the caller, returning the evaluation of {it:exp}.
{title:Remarks}
{p 4 4 2}
Remarks are presented under the headings
{bf:Functions that return results}
{bf:Functions that return nothing (void functions)}
{title:Functions that return results}
{p 4 4 2}
{cmd:return(}{it:exp}{cmd:)} specifies the value to be returned.
For instance, you have written a program to return the sum of two numbers:
{cmd}function mysum(a, b)
{
return(a+b)
}{txt}
{p 4 4 2}
{cmd:return(}{it:exp}{cmd:)} may appear multiple times in the program. The
following program calculates {it:x} factorial; it assumes {it:x} is an integer
greater than 0:
{cmd}real scalar myfactorial(real scalar x)
{
if (x<=0) return(1)
return(x*factorial(x-1))
}{txt}
{p 4 4 2}
Note that, if {cmd:x}<=0, the function returns 1; execution does not
continue to the next line.
{p 4 4 2}
Functions that return a result always include one or more
{cmd:return(}{it:exp}{cmd:)} statements.
{title:Functions that return nothing (void functions)}
{p 4 4 2}
A function is said to be void if it returns nothing. The following
program changes the diagonal of a matrix to be 1:
{cmd}function fixdiag(matrix A)
{
real scalar i
for (i=1; i<=rows(A); i++) A[i,i] = 1
}{txt}
{p 4 4 2}
Note that this function does not even include a {cmd:return} statement;
execution just ends. That is fine, although the function could just
as well read
{cmd}function fixdiag(matrix A)
{
real scalar i
for (i=1; i<=rows(A); i++) A[i,i] = 1
return
}{txt}
{p 4 4 2}
The use of {cmd:return} is when the function has reason to end early:
{cmd}void fixmatrix(matrix A, scalar how)
{
real scalar i, j
for (i=1; i<=rows(A); i++) A[i,i] = 1
if (how==0) return
for (i=1; i<=rows(A); i++) {
for (j=1; j<i; j++) A[i,j] = 0
}
}{txt}
{title:Also see}
{p 4 13 2}
Manual: {hi:[M-2] return}
{p 4 13 2}
Online: help for
{bf:{help mf_exit:[M-5] exit()}};
{bf:{help m2_intro:[M-2] intro}}
{p_end}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -