-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpureimpurefunc.js
More file actions
31 lines (30 loc) · 776 Bytes
/
pureimpurefunc.js
File metadata and controls
31 lines (30 loc) · 776 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
//Pure and Impure function
//pure :
//Pure function is any function which has 2 features
//1. It should always return same output for same input
//2.It will never change/update the value of the global variable
//ex :
function prod(a, b) {
return a * b; // This is a pure function
}
var ans1 = prod(1, 2);
var ans2 = prod(1, 2);
console.log(ans1, ans2);
// If any of two conditions are not maintain then impure function created
// Impure function :
//ex :
var x = 12;
function prod1(a, b) {
var x = 15;
return a * b; // although it gives same output, This is not a pure function
}
var ans1 = prod1(1, 2);
var ans2 = prod1(1, 2);
console.log(ans1, ans2);
//or
function out(a) {
return Math.random() * a;
}
var res1 = out(2);
var res2 = out(2);
console.log(res1, res2);