Is it possible to call function which calls pure function can be called pure function? How I understand 'pure function' is the function that always give same output on same input. Then let's do image this case.
const function1 = name => { console.log(`${name}`); }; const function2 = name => { function1(name); console.log('you') } I know it's possible to look bit stupid, but what confused me is the example what I've seen as example of pure function. Because usually example was like this.
var c = 10; function add2(a,b){ return a + b + c; } console.log(add2(10,3)); // same console.log(add2(10,3)); // same c = 20; console.log(add2(10,3)); // different Then like we changed c on last code, if we change function1 on the first example, then function2 will be different as well. This simple thing make me curious how can I define 'pure function' strictly.
- edited)
const function1 = name => { return `hi, ${name}`; }; const function2 = name => { return `${function1('Alice')} and ${name}`}; }
function1orc. Both are outside. as you saidpure function is the function that always give same output on same inputand in your second example you have 3 times the same input but different outputfunction1is not pure because it has a side effect (logging to the console), sofunction2is not pure either. And notice it's only a necessary, but not sufficient conditions that all called functions are pure for the function itself being pure - the rules are more than just that.returna value based on the arguments are both pure