Call a function: Difference between revisions

Content added Content deleted
(→‎{{header|Python}}: more on *args, **kwargs and so on.)
(Added Ruby)
Line 2,170: Line 2,170:
fb cannot be invoked as function (it does not return a value
fb cannot be invoked as function (it does not return a value
</pre>
</pre>

=={{header|Ruby}}==
Ruby does not have functions, but Ruby classes have "methods" which are equivalent.
The parentheses around the arguments are optional (definition and call).
A method returns the last expression that was evaluated in the body of the method.
The return keyword can be used to make it explicit that a method returns a value.

*Calling a function that requires no arguments
:<lang ruby>def foo() p "foo" end

foo #=> "foo"
foo() #=> "foo"</lang>

*Calling a function with a fixed number of arguments
:<lang ruby>def foo arg; p arg end # one argument

foo(1) #=> 1
foo "1" #=> "1"
foo [0,1,2] #=> [0, 1, 2] (one Array)</lang>

*Calling a function with optional arguments
:<lang ruby>def foo(x=0, y=x, flag=true) p [x,y,flag] end

foo #=> [0, 0, true]
foo(1) #=> [1, 1, true]
foo(1,2) #=> [1, 2, true]
foo 1,2,false #=> [1, 2, false]</lang>

*Calling a function with a variable number of arguments
:<lang ruby>def foo(*args) p args end

foo #=> []
foo(1,2,3,4,5) #=> [1, 2, 3, 4, 5]</lang>

*Calling a function with named arguments
:<lang ruby>def foo(id:0, name:"", age:0) p [id, name, age] end

foo(age:22, name:"Tom") #=> [0, "Tom", 22]</lang>

*Using a function in statement context
::?

*Using a function in first-class context within an expression
:The method is not a first-class function. However, there is '''Proc''' object.
::See [[First-class functions#Ruby]]

*Obtaining the return value of a function
:<lang ruby>def foo(a,b) a + b end

bar = foo 10,20
p bar #=> 30
p foo("abc","def") #=> "abcdef"

# return multiple values
def sum_and_product(a,b) return a+b,a*b end

x,y = sum_and_product(3,5)
p x #=> 8
p y #=> 15</lang>

*Distinguishing built-in functions and user-defined functions
::There is no distinction.

*Distinguishing subroutines and functions
:Subroutine and function don't exist at ruby. It is only a method that there is.
::The Kernel module is included by class Object, so its methods are available in every Ruby object.
::These methods are called without a receiver and thus can be called in functional form.

::<lang ruby>puts "OK!" # Kernel#puts
raise "Error input" # Kernel#raise
Integer("123") # Kernel#Integer
rand(6) # Kernel#rand
throw(:exit) # Kernel#throw

# method which can be seen like a reserved word.
attr_accessor # Module#attr_accessor
include # Module#include
private # Module#private
require # Kernel#require
loop { } # Kernel#loop</lang>

*Stating whether arguments are passed by value or by reference

::passed by value of object reference.

*Is partial application possible and how
::However something similar can be done, see [[Partial function application#Ruby]]


*Others
:Block Argument:
::The block argument sends a closure from the calling scope to the method.
::The block argument is always last when sending a message to a method. A block is sent to a method using <code>do ... end</code> or <code>{ ... }</code>.
::<lang ruby>class Array
def sum(init=0, &blk)
if blk
inject(init){|s, n| s + blk.call(n)}
else
inject(init){|s, n| s + n}
end
end
end

ary = [1,2,3,4,5]
p ary.sum #=> 15
p ary.sum(''){|n| (-n).to_s} #=> "-1-2-3-4-5"
p (ary.sum do |n| n * n end) #=> 55</lang>

:Splat operator:
::You can turn an Array into an argument list with * (or splat) operator.
::<lang ruby>def foo(a,b,c) p [a,b,c] end

args = [1,2,3]
foo *args #=> [1, 2, 3]
args = [1,2]
foo(0,*args) #=> [0, 1, 2]</lang>

:Syntax sugar:
::In Ruby, many operators are actually method calls.
::<lang ruby># return value substance
i = 3
p 1 + i #=> 4 1.+(i)
p i < 5 #=> true i.<(5)
p 2 ** i #=> 8 2.**(i)
p -i #=> -3 i.-@()
a = [1,2,3]
p a[0] #=> 1 a.[](0)
a[2] = "0" # a.[]=(2,"0")
p a << 5 #=> [1, 2, "0", 5] a.<<(5)
p a & [4,2] #=> [2] a.&([4,2])
p "abcde"[1..3] #=> "bcd" "abcde".[](1..3)
p "%2d %4s" % [1,"xyz"] #=> " 1 xyz" "%2d %4s".%([1,"xyz"])</lang>
::Method call which was displayed in the comment is usable actually.


=={{header|Scala}}==
=={{header|Scala}}==