What is the class of function that when called repeatedly, has the same effect as calling once?
A function that fulfills this criteria is
int var = 0;
void func1()
{
var = 10;
}
As you can see, calling fun1
10 times has the same effect as calling it once (assigns 10
to var
)
A function that does not fulfill this criteria is
int var = 0;
void func2()
{
var++;
}
Calling func2
10 times results in var
being assigned a different value as compared to calling func2
once
naming functions
add a comment |
A function that fulfills this criteria is
int var = 0;
void func1()
{
var = 10;
}
As you can see, calling fun1
10 times has the same effect as calling it once (assigns 10
to var
)
A function that does not fulfill this criteria is
int var = 0;
void func2()
{
var++;
}
Calling func2
10 times results in var
being assigned a different value as compared to calling func2
once
naming functions
add a comment |
A function that fulfills this criteria is
int var = 0;
void func1()
{
var = 10;
}
As you can see, calling fun1
10 times has the same effect as calling it once (assigns 10
to var
)
A function that does not fulfill this criteria is
int var = 0;
void func2()
{
var++;
}
Calling func2
10 times results in var
being assigned a different value as compared to calling func2
once
naming functions
A function that fulfills this criteria is
int var = 0;
void func1()
{
var = 10;
}
As you can see, calling fun1
10 times has the same effect as calling it once (assigns 10
to var
)
A function that does not fulfill this criteria is
int var = 0;
void func2()
{
var++;
}
Calling func2
10 times results in var
being assigned a different value as compared to calling func2
once
naming functions
naming functions
asked 4 hours ago
WoofasWoofas
27713
27713
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
This type of function / operation is called Idempotent
Idempotence (UK: /ˌɪdɛmˈpoʊtəns/,[1] US: /ˌaɪdəm-/)[2] is the property of certain operations in mathematics and computer science whereby they can be applied multiple times without changing the result beyond the initial application.
add a comment |
The precise term for this is as @Woofas mentions, is idempotence. I wanted to add that while you could call your func1
method idempotent, you could not call it a pure function. The properties of a pure function are two: it must be idempotent and it must not have side effects, which is to say, no mutation of local static variables, non-local variables, mutable reference arguments or I/O streams.
The reason I mention this is that a idempotent function with side effects is not good either, since technically idempotent refers to the return ouptut of the function, and not to the side effects. So technically your func2
method is idempotent, as the output doesn't change according to the input.
You most likely want to specify that you want a pure function. An example of a pure function might be as follows:
int func1(int var)
{
return var + 1;
}
More reading can be found here.
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "131"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsoftwareengineering.stackexchange.com%2fquestions%2f387990%2fwhat-is-the-class-of-function-that-when-called-repeatedly-has-the-same-effect-a%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
This type of function / operation is called Idempotent
Idempotence (UK: /ˌɪdɛmˈpoʊtəns/,[1] US: /ˌaɪdəm-/)[2] is the property of certain operations in mathematics and computer science whereby they can be applied multiple times without changing the result beyond the initial application.
add a comment |
This type of function / operation is called Idempotent
Idempotence (UK: /ˌɪdɛmˈpoʊtəns/,[1] US: /ˌaɪdəm-/)[2] is the property of certain operations in mathematics and computer science whereby they can be applied multiple times without changing the result beyond the initial application.
add a comment |
This type of function / operation is called Idempotent
Idempotence (UK: /ˌɪdɛmˈpoʊtəns/,[1] US: /ˌaɪdəm-/)[2] is the property of certain operations in mathematics and computer science whereby they can be applied multiple times without changing the result beyond the initial application.
This type of function / operation is called Idempotent
Idempotence (UK: /ˌɪdɛmˈpoʊtəns/,[1] US: /ˌaɪdəm-/)[2] is the property of certain operations in mathematics and computer science whereby they can be applied multiple times without changing the result beyond the initial application.
answered 4 hours ago
WoofasWoofas
27713
27713
add a comment |
add a comment |
The precise term for this is as @Woofas mentions, is idempotence. I wanted to add that while you could call your func1
method idempotent, you could not call it a pure function. The properties of a pure function are two: it must be idempotent and it must not have side effects, which is to say, no mutation of local static variables, non-local variables, mutable reference arguments or I/O streams.
The reason I mention this is that a idempotent function with side effects is not good either, since technically idempotent refers to the return ouptut of the function, and not to the side effects. So technically your func2
method is idempotent, as the output doesn't change according to the input.
You most likely want to specify that you want a pure function. An example of a pure function might be as follows:
int func1(int var)
{
return var + 1;
}
More reading can be found here.
add a comment |
The precise term for this is as @Woofas mentions, is idempotence. I wanted to add that while you could call your func1
method idempotent, you could not call it a pure function. The properties of a pure function are two: it must be idempotent and it must not have side effects, which is to say, no mutation of local static variables, non-local variables, mutable reference arguments or I/O streams.
The reason I mention this is that a idempotent function with side effects is not good either, since technically idempotent refers to the return ouptut of the function, and not to the side effects. So technically your func2
method is idempotent, as the output doesn't change according to the input.
You most likely want to specify that you want a pure function. An example of a pure function might be as follows:
int func1(int var)
{
return var + 1;
}
More reading can be found here.
add a comment |
The precise term for this is as @Woofas mentions, is idempotence. I wanted to add that while you could call your func1
method idempotent, you could not call it a pure function. The properties of a pure function are two: it must be idempotent and it must not have side effects, which is to say, no mutation of local static variables, non-local variables, mutable reference arguments or I/O streams.
The reason I mention this is that a idempotent function with side effects is not good either, since technically idempotent refers to the return ouptut of the function, and not to the side effects. So technically your func2
method is idempotent, as the output doesn't change according to the input.
You most likely want to specify that you want a pure function. An example of a pure function might be as follows:
int func1(int var)
{
return var + 1;
}
More reading can be found here.
The precise term for this is as @Woofas mentions, is idempotence. I wanted to add that while you could call your func1
method idempotent, you could not call it a pure function. The properties of a pure function are two: it must be idempotent and it must not have side effects, which is to say, no mutation of local static variables, non-local variables, mutable reference arguments or I/O streams.
The reason I mention this is that a idempotent function with side effects is not good either, since technically idempotent refers to the return ouptut of the function, and not to the side effects. So technically your func2
method is idempotent, as the output doesn't change according to the input.
You most likely want to specify that you want a pure function. An example of a pure function might be as follows:
int func1(int var)
{
return var + 1;
}
More reading can be found here.
answered 6 mins ago
NeilNeil
19.7k3566
19.7k3566
add a comment |
add a comment |
Thanks for contributing an answer to Software Engineering Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsoftwareengineering.stackexchange.com%2fquestions%2f387990%2fwhat-is-the-class-of-function-that-when-called-repeatedly-has-the-same-effect-a%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown