New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
document return nothing
convention in Functions man page
#27286
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,9 @@ julia> function f(x,y) | |
f (generic function with 1 method) | ||
``` | ||
|
||
This function accepts two arguments `x` and `y` and returns the value | ||
of the last expression evaluated, which is `x + y`. | ||
|
||
There is a second, more terse syntax for defining a function in Julia. The traditional function | ||
declaration syntax demonstrated above is equivalent to the following compact "assignment form": | ||
|
||
|
@@ -56,16 +59,25 @@ julia> ∑(2, 3) | |
Julia function arguments follow a convention sometimes called "pass-by-sharing", which means that | ||
values are not copied when they are passed to functions. Function arguments themselves act as | ||
new variable *bindings* (new locations that can refer to values), but the values they refer to | ||
are identical to the passed values. Modifications to mutable values (such as `Array`s) made within | ||
a function will be visible to the caller. This is the same behavior found in Scheme, most Lisps, | ||
are identical to the passed values. | ||
Consequently, assigning a new value to a function argument just rebinds it to that new value, | ||
but does not modify any variable in the caller scope. | ||
However, modifications to *mutable* values (such as `Array`s) made within | ||
a function will be visible to the caller. | ||
|
||
There is a dedicated [FAQ section](@ref faq-arg-modif) on how a function | ||
can or cannot modify its arguments, with a set of code examples. | ||
Julia argument passing behavior is the same behavior found in Scheme, most Lisps, | ||
Python, Ruby and Perl, among other dynamic languages. | ||
|
||
|
||
## The `return` Keyword | ||
|
||
The value returned by a function is the value of the last expression evaluated, which, by default, | ||
is the last expression in the body of the function definition. In the example function, `f`, from | ||
the previous section this is the value of the expression `x + y`. As in C and most other imperative | ||
or functional languages, the `return` keyword causes a function to return immediately, providing | ||
the previous section this is the value of the expression `x + y`. | ||
As an alternative, as in most other languages, | ||
the `return` keyword causes a function to return immediately, providing | ||
an expression whose value is returned: | ||
|
||
```julia | ||
|
@@ -125,7 +137,9 @@ There are three possible points of return from this function, returning the valu | |
expressions, depending on the values of `x` and `y`. The `return` on the last line could be omitted | ||
since it is the last expression. | ||
|
||
A return type can also be specified in the function declaration using the `::` operator. This converts | ||
### Return type | ||
|
||
A return type can be specified in the function declaration using the `::` operator. This converts | ||
the return value to the specified type. | ||
|
||
```jldoctest | ||
|
@@ -140,6 +154,41 @@ Int8 | |
This function will always return an `Int8` regardless of the types of `x` and `y`. | ||
See [Type Declarations](@ref) for more on return types. | ||
|
||
Incidentally, the type of the arguments can also be specified similarly, | ||
but this is the topic of the [Methods](@ref) chapter. | ||
|
||
### Returning nothing | ||
|
||
Some functions are used only for their side effects, and do not need to return a value. | ||
In these cases, the Julia convention is to return the value [`nothing`](@ref): | ||
|
||
```julia | ||
function printx(x) | ||
println("x = $x") | ||
return nothing | ||
end | ||
``` | ||
|
||
Notice that `nothing` is not a Julia keyword but a singleton object of type `Nothing`. | ||
To support this convention, the REPL does not print anything for it: | ||
|
||
```jldoctest | ||
julia> nothing | ||
|
||
``` | ||
|
||
There are two possible shortened forms for the `return nothing` expression. | ||
On the one hand, the `return` keyword implicitly returns `nothing`, so it can be used alone | ||
(similar in that sense to `return;` in C). | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here |
||
On the other hand, since functions implicitly return their last expression evaluated, | ||
`nothing` can be used alone when it's the last exression. | ||
The preference for the expression `return nothing` as opposed to `return` or `nothing` | ||
alone is a subjective matter of coding style. Some people prefer explicitness | ||
(always specifying the returned value), while some others favor compactness. | ||
|
||
As a side note on functions with side effects, the [style guide](@ref man-style-exclam) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this fit under the |
||
recommends to append `!` to the names of functions that modify their arguments. | ||
|
||
## Operators Are Functions | ||
|
||
In Julia, most operators are just functions with support for special syntax. (The exceptions are | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can remove the c comparison. It is a bit different imo since in c you explicitly declare the return type.