11.07.2015 Views

[U] User's Guide

[U] User's Guide

[U] User's Guide

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

[ U ] 18.4 Program arguments 219Let’s try our program:. listargsThe 1st argument you typed is:The 2nd argument you typed is:The 3rd argument you typed is:The 4th argument you typed is:We type listargs, and the result shows us what we already know—we typed nothing after theword listargs. There are no arguments. Let’s try it again, this time adding this is a test:. listargs this is a testThe 1st argument you typed is:The 2nd argument you typed is:The 3rd argument you typed is:The 4th argument you typed is:thisisatestWe learn that the first argument is ‘this’, the second is ‘is’, and so on. Blanks always separatearguments. You can, however, override this feature by placing double quotes around what you type:. listargs "this is a test"The 1st argument you typed is:The 2nd argument you typed is:The 3rd argument you typed is:The 4th argument you typed is:this is a testThis time we typed only one argument, ‘this is a test’. When we place double quotes aroundwhat we type, Stata interprets whatever we type inside the quotes to be one argument. Here ‘1’contains ‘this is a test’ (the double quotes were removed).We can use double quotes more than once:. listargs "this is" "a test"The 1st argument you typed is: this isThe 2nd argument you typed is: a testThe 3rd argument you typed is:The 4th argument you typed is:The first argument is ‘this is’ and the second argument is ‘a test’.18.4.1 Named positional argumentsPositional arguments can be named: in your code, you do not have to refer to ‘1’, ‘2’, ‘3’,. . . ; you can instead refer to more meaningful names, such as n, a, and b; numb, alpha, and beta;or whatever else you find convenient. You want to do this because programs coded in terms of ‘1’,‘2’, . . . are hard to read and therefore are more likely to contain errors.You obtain better-named positional arguments by using the args command:program prognameargs argnames. . .endFor instance, if your program received four positional arguments and you wanted to call them varname,n, oldval, and newval, you would codeprogram prognameargs varname n oldval newval. . .end

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!