I'm trying to define a function in Racket which takes no arguments. All the examples that I have seen take one or more arguments. How can I do that?
A function with argument, as in function(something) is to return a value after processing within the function. A function with no argument, eg function(), is to perform a task with no value returned.
A nullary or niladic function.
So, if we call the function without passing the arguments for them, it will throw an error. For the optional parameters, we don't have to pass any arguments. If we don't, the default value will be used.
You can define a function that doesn't take any arguments, but the parentheses are still required. Both a function definition and a function call must always include parentheses, even if they're empty.
(define (fun1)
"hello")
(define fun2
(lambda ()
"world"))
(define fun3
(thunk
"I am back"))
(fun1)
=> "hello"
(fun2)
=> "world"
(fun3)
=> "I am back"
EDIT
If, as @Joshua suggests, you want a procedure which can take any argument(s) and ignore them, the equivalent definitions would be:
(define (fun1 . x)
"hello")
(define fun2
(lambda x
"world"))
(define fun3
(thunk*
"I am back"))
(fun1)
(fun1 1 2 3)
=> "hello"
(fun 2)
(fun2 4 5 6 7)
=> "world"
(fun3)
(fun3 8 9)
=> "I am back"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With