Skip to content
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

correct formatting of Functions-in-Ruby.md #104

Merged
merged 1 commit into from
Oct 17, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 28 additions & 9 deletions Functions-in-Ruby.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,39 @@
You can define a function using the reserve word `def` they are commonly methods than return a value
#Functions in Ruby
You define a function using the reserved word `def`on the first line of the function (the function declaration) and using the reserved word `end` on the last line, after all the action is done.

Ruby Methods always return a value, and always return only one single thing (an object). This object could be the object nil, but it is still an object.

An option for returning multiple objects is to return an Array that hold the things you need, but the Array itself is also a single object.

##Basic Examples:

```ruby
def my_function
return "something"
end
```
def my_function
return "something"
end
```

You can pass arguments that will be used within the method like this

```ruby
def sum(a, b)
return a + b
end
```
def sum(a, b)
return a + b
end
```

This method return the sum of a plus b arguments

##Return is optional, but...
Use of the return statement is optional in Ruby. However, if not used, the method will return the value that is returned by the last evaluated statement in that method.

```
def no_return(number)
number + 2
end

p no_return(3)
```

The function no_return() returns 5 because by default, Ruby methods return the value of the last expression evaluationed in the method.

If you want (or need) to return from a method "early" you can use return statement as usual, but be careful as doing so may result in some code not being executed.