{"action":"opened","issue":{"url":"https://api.github.com/repos/openscad/openscad/issues/1594","repository_url":"https://api.github.com/repos/openscad/openscad","labels_url":"https://api.github.com/repos/openscad/openscad/issues/1594/labels{/name}","comments_url":"https://api.github.com/repos/openscad/openscad/issues/1594/comments","events_url":"https://api.github.com/repos/openscad/openscad/issues/1594/events","html_url":"https://github.com/openscad/openscad/issues/1594","id":138052459,"number":1594,"title":"Default function arguments are evaluated for every function call","user":{"login":"kintel","id":186094,"avatar_url":"https://avatars.githubusercontent.com/u/186094?v=3","gravatar_id":"","url":"https://api.github.com/users/kintel","html_url":"https://github.com/kintel","followers_url":"https://api.github.com/users/kintel/followers","following_url":"https://api.github.com/users/kintel/following{/other_user}","gists_url":"https://api.github.com/users/kintel/gists{/gist_id}","starred_url":"https://api.github.com/users/kintel/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kintel/subscriptions","organizations_url":"https://api.github.com/users/kintel/orgs","repos_url":"https://api.github.com/users/kintel/repos","events_url":"https://api.github.com/users/kintel/events{/privacy}","received_events_url":"https://api.github.com/users/kintel/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2016-03-03T02:09:47Z","updated_at":"2016-03-03T02:09:47Z","closed_at":null,"body":"Consider the following example.\r\nThe default function argument ```(arg = expensive())``` is evaluated for each function call, making the apparently trivial function calls take a lot of time.\r\nIn this example, we wouldn't need to evaluate the default value at all, but even evaluating it only once would be an improvement.\r\n\r\nWhen fixing this, keep in mind that functions can be called before they're defined, as all function definitions are bound before assignment expressions are evaluated.\r\n```\r\nfunction expensive(n = 20) = n == 0 ? 1 : (expensive(n - 1) + expensive(n - 1));\r\nfunction myFunc(arg = expensive()) = arg;\r\n\r\necho(myFunc(1));\r\necho(myFunc(2));\r\necho(myFunc(3));\r\n```"}}