|
A function is "a portion of code within a larger program, performs a specific task".
Functions have many benefits including:
- reducing the duplication of code in a program (e.g., by replicating useful functionality, such as mathematical functions);
- enabling reuse of code across multiple programs;
- decomposing complex problems into simpler pieces (this improves maintainability and ease of extension);
- improving readability of a program;
- hiding or regulating part of the program.
- quoted from Wikipedia: practically, any program, no matter of the programming language used will increase in quality and functionality if you learn how to master functions.
I recommend you to also read this article on Wikipedia regarding functions if you would like to get a more advanced understanding of what they are and how to use them.
The basic concepts are the same, even if that article hasn't been written specifically for Ruby.
You will learn more Ruby specifics as you keep reading this tutorial and also learn how to define your own functions when you don't find an equivalent in the ones already built in.
Functions are also know under many other names such as subroutines, methods or subprograms. Don't be surprised if you notice this tutorial has a chapter about methods that resemble the concepts of functions... because they actually are functions. It's easy to get confused with the terminology, but once you have some basic programming skills things will get much easier.
A list with some built in functions in Ruby
abort
Array
at_exit {...}
binding
block_given?
callcc {| c|...}
caller([ n])
catch( tag) {...}
chomp([ rs=$/])
chomp!([ rs=$/])
chop
chop!
eval( str[, scope[, file, line]])
exec( cmd[, arg...])
exit([ result=0])
exit!([ result=0])
fail(...)
Float( obj)
fork
fork {...}
format( fmt[, arg...])
gets([ rs=$/])
global_variables
gsub( x, y)
gsub( x) {...}
gsub!( x, y)
gsub!( x) {...}
Integer( obj)
lambda {| x|...}
proc {| x|...}
lambda
proc |
load( file[, private=false])
local_variables
loop {...}
open( path[, mode="r"])
open( path[, mode="r"]) {| f|...}
p( obj) print([ arg...])
printf( fmt[, arg...])
proc {| x|...}
putc( c)
puts([ str])
raise(...)
fail(...)
srand([ seed])
String( obj)
syscall( sys[, arg...])
system( cmd[, arg...])
sub( x, y)
sub( x) {...}
trap( sig, cmd)
trap( sig) {...}
untrace_var( var[, cmd])
|
Built in functions are functions that have already been defined for you and are included in the standard Ruby distribution.
You use a built in function by calling it's name along with necessary parameters(if any required) like this:
puts "puts is a Ruby built in function"
You can find the syntax needed for using a certain function by using the fxri help console for example or by doing a search on Google.
You can also find out more about what a certain function does by using the same methods above.
You now know the absolute basics of functions.If you also read the Wikipedia article I recommended, you now know A LOT about functions. All right, on with the next chapter!
|
|