Join Curt Frye for an in-depth discussion in this video Adding comments to a Mathematica script, part of Mathematica 10 Essential Training.
- Whenever you create a Mathematica file or any other sort of programmer script, you can help out yourself as well as your colleagues by adding comments. In this movie, I will show you how to add comments and also function-related help inside of Mathematica. I have created a new blank file, and I would like to add a comment to the top to indicate what I'm using the file for. To add a comment, you type a left parentheses followed by an asterisk, and when you press Space, you see that the text has turned from magenta to gray.
Gray indicates that you are entering a comment. So now, I'll type in Created for Mathematica 10: Essential Training, you can type in whatever you want, then another asterisk followed by a right parentheses, and when I press Shift + Enter, I see that my line is in gray and I have successfully created a comment. You can also add a usage message to a function. For example, let's say that I create a function that raises one value to an exponent, so I would create my function called raise, left square bracket, and then I need to name my two input variables, so I make that x followed by an underscore, then a comma, exp for the exponent, underscore, then a right square bracket, and I'll define the function expression, the actual calculation, by typing a colon followed by an equal sign, and then x, caret, exp.
So, I'm raising x to whatever number is provided in the exp variable. So, Shift + Enter, and there's my function, and just make sure it works, so I'll type raise, left square bracket three comma eight, then a right square bracket and Shift + Enter, and I get the value of 6561. But now let's say that I want to indicate what's going on inside of this function by adding a help message. I can do that by typing raise, and then double-colon, so two colons in a row, and then the usage keyword, and you can see that it is also in gray, indicating it's a comment.
I'll type an equal sign because I am assigning a value to this property, then it is a string of text, so I type a double-quote, and I type in raise raises x to the power exp, then a second double-quotation mark, close the string and Shift + Enter, and I get my text. Now, if I want to get help on this particular function and I type a question mark and then raise, r-a-i-s-e, and Shift + Enter, I get a help message indicating that raise raises x to the power exp.
So, as you can see, if you create a new function, you can add a help message that makes it easier for you and your colleagues to use that function later.
- Managing notebooks
- Working with operators
- Assigning values to variables
- Importing and exporting data
- Creating advanced formulas
- Creating and manipulating lists
- Manipulating arrays
- Analyzing data with descriptive analytics
- Manipulating matrices
- Managing scripts
- Creating charts
- Formatting data