I. No parameter functions:
IRB (main ): 010 : 0 > Def H
IRB (main ): 011 : 1 > Puts " Hello world! "
IRB (main ): 012 : 1 > End
=> Nil
IRB (main ):013: 0>H
Hello world!
=>Nil
IRB (main ):014: 0>H ()
Hello world!
=>Nil
Calling a function in ruby is as simple as calling a ruby name.If a function does not need to accept a parameter, you only need to mention it. You can add a pair of parentheses, but they are not required.
Ii. Functions with Parameters
IRB (main ): 015 : 0 > Def H (name)
IRB (main ): 016 : 1 > Puts " Hello # {name }! "
IRB (main ): 017 : 1 > End
=> Nil
IRB (main): 0 18 : 0 > H ( " Matz " )
Hello Matz!
=> Nil
You can use # {parametername} to reference parameters. Wait, it seems that the parameter has no parameter type. Yes, this is the embodiment of the famous "Duck.
3. Default Parameters
IRB (main): 0 19 : 0 > Def H (name = " World " )
IRB (main ): 020 : 1 > Puts " Hello # {name. capitalize }! "
IRB (main ): 021 : 1 > End
=> Nil
IRB (main ): 022 : 0 >
H
"Chris"
Hello Chris!
=> Nil
IRB (main ): 023 : 0 > H
Hello world!
=> Nil
Parameters can be specified in parentheses or directly.
If a parameter has a default value, you can choose not to specify the parameter and call it as if there is no Parameter Function.
Problem:
The Return Value of the parameter.
Multi-parameter function call problems.
In a multi-parameter function, must the default parameter position be at the end?
Okay. Next I will talk about classes in ruby.