Call a function: Difference between revisions
Content added Content deleted
No edit summary |
|||
Line 244: | Line 244: | ||
/* Pointers *sort of* work like references, though. */</lang> |
/* Pointers *sort of* work like references, though. */</lang> |
||
=={{header| |
=={{header|C++}}== |
||
<lang |
<lang C++> |
||
; Clojure is pass-by-value just like Java |
|||
/* function with no arguments */ |
|||
foo(); |
|||
(defn a [] "This is the 'A' function") |
|||
</lang> |
|||
(a) |
|||
<lang C++> |
|||
;Calling a function with a fixed number of arguments |
|||
/* passing arguments by value*/ |
|||
(defn b [x y] (list x y)) |
|||
/* function with one argument */ |
|||
(b 1 2) |
|||
bar(arg1); |
|||
⚫ | |||
baz(arg1, arg2); |
|||
</lang> |
|||
<lang C++> |
|||
;Calling a function with optional arguments (multi-arity functions) |
|||
/* get return value of a function */ |
|||
(defn c ([] "no args") |
|||
variable = function(args); |
|||
([optional] (str "optional arg: " optional))) |
|||
</lang> |
|||
(c) |
|||
(c 1) |
|||
<lang C++> |
|||
#include <iostream> |
|||
;Calling a function with a variable number of arguments (variadic) |
|||
using namespace std; |
|||
(defn d [& more] more) |
|||
/* passing arguments by reference */ |
|||
(d 1 2 3 4 5 6 7 8) |
|||
void f(int &y) /* variable is now passed by reference */ |
|||
{ |
|||
⚫ | |||
y++; |
|||
(defn e [& {:keys [x y]}] (list x y)) |
|||
} |
|||
(e :x 10 :y 20) |
|||
int main() |
|||
{ |
|||
;Using a function in first-class context within an expression |
|||
int x = 0; |
|||
(def hello (fn [] "Hello world")) |
|||
cout<<"x = "<<x<<endl; /* should produce result "x = 0" */ |
|||
; |
f(x); /* call function f */ |
||
cout<<"x = "<<x<<endl; /* should produce result "x = 1" */ |
|||
(def return-of-hello (hello)) |
|||
} |
|||
;Is partial application possible and how? Yes |
|||
(def add10 (partial + 10)) |
|||
(add10 3) ;=> 13 |
|||
(add10 1 2 3) ;=> 16 |
|||
</lang> |
</lang> |
||