Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
43
Task/Call-a-function/Nim/call-a-function.nim
Normal file
43
Task/Call-a-function/Nim/call-a-function.nim
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
proc no_args() =
|
||||
discard
|
||||
# call
|
||||
no_args()
|
||||
|
||||
proc fixed_args(x, y) =
|
||||
echo x
|
||||
echo y
|
||||
# calls
|
||||
fixed_args(1, 2) # x=1, y=2
|
||||
fixed_args 1, 2 # same call
|
||||
1.fixed_args(2) # same call
|
||||
|
||||
|
||||
proc opt_args(x=1.0) =
|
||||
echo x
|
||||
# calls
|
||||
opt_args() # 1
|
||||
opt_args(3.141) # 3.141
|
||||
|
||||
proc var_args(v: varargs[string, `$`]) =
|
||||
for x in v: echo x
|
||||
# calls
|
||||
var_args(1, 2, 3) # (1, 2, 3)
|
||||
var_args(1, (2,3)) # (1, (2, 3))
|
||||
var_args() # ()
|
||||
|
||||
## Named arguments
|
||||
fixed_args(y=2, x=1) # x=1, y=2
|
||||
|
||||
## As a statement
|
||||
if true:
|
||||
no_args()
|
||||
|
||||
proc return_something(x): int =
|
||||
x + 1
|
||||
|
||||
var a = return_something(2)
|
||||
|
||||
## First-class within an expression
|
||||
let x = return_something(19) + 10
|
||||
let y = 19.return_something() + 10
|
||||
let z = 19.return_something + 10
|
||||
Loading…
Add table
Add a link
Reference in a new issue