Function is a block of code which performs a specific task.
2
votes
0answers
114 views
How to move from OOP object composition to FP function composition in C#
I have been working for a few weeks on a new web project and I am realizing that all I am doing is basically calculations and transformations on data, and that most of my classes do not contain any ...
-3
votes
0answers
12 views
Syntax error at second definition in Python [migrated]
def specificChecker(someThing, checker):
if checker == None:
return someThing
elif checker == True:
return not someThing
else:
return None
def ...
-2
votes
0answers
38 views
Is it possible get JS function? [on hold]
I think that when JS developer had create JavaScript, they have create some function like log(), write(), alert(). They had do something like function alert() {} and then standard it (I imagine).
So, ...
14
votes
5answers
4k views
A language based on limiting amount of arguments passed to functions
The idea is inspired by the fact operators such as +, -,%, etc. can be seen as functions with either one or two arguments passed, and no side-effects. Assuming I, or someone else, writes a language ...
1
vote
1answer
121 views
Is it possible to run a program in C language without libraries?
I was working on a C program and i just noted that i can execute my program even if i didn't declared the required libraries, this is the code:
#include <stdio.h>
#include <string.h>
int ...
1
vote
2answers
168 views
How can you enforce a “do you really want to do this” confirmation in a publically exposed API?
Is there a commonly (or rarely) used pattern for a "confirming you want to do this" message when calling a function from a library?
I have a API that exposes some operations that are potentially ...
40
votes
15answers
8k views
Is it inadvisable to make a function that essentially renames a built-in function?
I get confused over min and max functions, in certain contexts.
In one context, when you're using the functions to take the greater or lesser of two values, there is no issue. For example,
//how ...
0
votes
2answers
122 views
Is it bad practice to verify arguments passed to a function? [duplicate]
I find myself writing a lot of functions whereby the first few lines "verify" the arguments, that is, they typecast, check falseness, range, etc. before doing any work. The main reasoning for this is ...
1
vote
1answer
76 views
Fundamental difference between a static class and namespaced functions
I have been using "static" classes as a method to group functions with relating purposes under a common name that provides readability and maintainability to the code at the cost of performance and ...
2
votes
2answers
198 views
In C++, why shouldn't all function parameters be references?
I am currently learning C++ from the C++ Primer 5th edition. The book's chapter on functions states that only large objects (large being relative as standard library strings count, but "primitive ...
4
votes
2answers
131 views
Calling different library functions based on parameter
I am writing a rather large class where the user can specify at runtime whether she wants to use algorithm A or B of an third party library e.
Basically there is only one function call different in ...
0
votes
1answer
109 views
How to call an unknown member function through an instance of a related class?
I have two classes. The first, called Game_Events, controls the objects and manages the general tasks. The second, called Button, is for one of those object instances.
The example below is wrong but ...
0
votes
3answers
93 views
Design for a function to skip a step if called multiple times
Terrible title, but this is the situation I find myself in often and have not found a good design to make it nice. Lets say I'm working with Javascript and I have an object I am working on that needs ...
3
votes
4answers
234 views
When should a function take a pointer for a collection to fill vs returning a pointer with a filled collection?
In C++ I frequently see these two signatures used seemingly interchangeably:
void fill_array(Array<Type>* array_to_fill);
Array<Type>* filled_array();
I imagine there is a subtle ...
-3
votes
1answer
121 views
Returning a variable or a return code from a function
Let assume that we have a main() function in which we call a createDirectory() function.
In Python for instance, the code would be:
def main():
# Do some stuff
createDirectory(myPath)
# ...
2
votes
2answers
58 views
Cleanly generating several 0-airty JavaScript functions with slightly different bodies
Say I have a bunch of JavaScript functions similar to:
message = [“000”, “111”, “222”, “333”];
function F0(){
alert (message[0]);
}
function F1(){
alert (message[1]);
}
function F2(){
...
-2
votes
1answer
60 views
Multiple arguments for last parameter
I have function that has three parameters, called key, value and attribute.
Parameter attribute may be empty or have within two arguments. Code of function below is shortened (deprived of arguments ...
3
votes
2answers
346 views
Differences between “first-class function” mechanisms
Some languages (Javascript, Python) have the notion that a function is an object:
//Javascript
var fn = console.log;
This means that functions can be treated like any other object (first-class ...
4
votes
1answer
146 views
Is there a programming language which requires argument qualifiers (reference/value) to be specified at the call point?
For quite a long time now, I have been using a calling convention from C++ google style guide, which boils down to the following: "[for a function] arguments are values or const references while ...
7
votes
3answers
1k views
Is a function getting a value from another function considered pure?
I'm trying to figure out a way to handle default variable values when making functions without side effects and have ended up with the following:
function getDefaultSeparator() {
return ':';
}
...
23
votes
2answers
1k views
Why is passing large anonymous functions as arguments to other functions so widely accepted in JavaScript?
I have an opinion (which I am sure will be shared by some) that passing anonymous functions which contain more than a few lines of code, as arguments to other functions affects readability and self-...
1
vote
3answers
209 views
Is it considered good practice to always have methods return a value?
Sorry for the terrible title but hopefully these snippets will give you the gist.
Method 1:
class Person:
def __init__(self, name):
self.name = name
def set_name(self, new_name):
...
1
vote
4answers
134 views
How to name functions that use conditionals in refactoring [closed]
Consider this bit of code :
private Norf foo(Baz baz) {
// ...
// Logic on baz
// ...
if (baz.color == Baz.BLUE) {
// Do this thing
}
// ...
// More logic
// ...
2
votes
1answer
134 views
Enumerating the primitive recursive functions
How can I enumerate (by expression tree size, for example) all of the primitive recursive functions that map natural numbers to natural numbers in a traditional programming language like C?
For ...
1
vote
1answer
132 views
Why use tuples as function parameters in languages that support currying?
In languages that support currying, I can't think of many cases where using a tuple as function input parameters would be better than breaking the tuple apart into multiple parameters, which then ...
-1
votes
2answers
57 views
“Separate Query from Modifier” and return values [closed]
I've heard it said that you should keep query and modifier functions separate from one another, and I agree with the sentiment. When trying to apply it, I often run into two issues/questions:
What, ...
8
votes
4answers
435 views
Swift functions vs computed properties
Say I have have a class Event as follows:
class Event {
private var attendees: [Person] = []
// Case 1
//*******
// Should I use a func…
func countOfAttendees() -> Int {
...
0
votes
1answer
137 views
How to document **kwargs in python? [closed]
I have a function which has a large number of arguments.
I want to have the names of the arguments available in the help() function, but I want the results as a dict.
At the moment, I have the ...
2
votes
2answers
267 views
Java - Using a Function variable to set the toString() method's return value
Lately I've started adding this to certain classes in an in-house API:
public class MyClass {
// I usually do this with classes I expect to
// be printed out or tested a lot (particularly
// ...
1
vote
1answer
106 views
Reducing the arity of functions [closed]
I have discovered that reducing the arity of functions in my code to zero or one improves their non-functional characteristics significantly, such as testability, maintainability and their ...
0
votes
1answer
131 views
Function or class design in library API for efficient object reuse
I am currently building a library in C++11, where I spent a lot of time trying to design a good interface. After some small redesigns along the way, I've ended up with a design that I am happy with. ...
5
votes
3answers
578 views
Functions whose parameters are implicitly supplied
Callback functions are able to accept some parameters, but they are not explicitly declared when calling the function.
How does this actually work? What is going on that allows us to pass a function ...
5
votes
2answers
230 views
What is the difference between currying and partial function application in practice
I understand the difference between partial function application and a curried function (f(X x Y x Z) -> N vs f(X -> (Y -> (Z -> N)))), but I do not see what the consequence of this ...
3
votes
1answer
425 views
What does comma signify in a function definition?
From PHP manual:
array array_change_key_case ( array $array [, int $case = CASE_LOWER ] )
What does comma signify in a function definition?
1
vote
1answer
186 views
Function name in parentheses after fuction call in Python
I ran into these lines of code in the QPYTHON Android app. They are part of a sample that uses the Bottle module to create a simple Web server that seems to work fine.
app = Bottle()
app.route('/', ...
2
votes
2answers
219 views
What's wrong about extending a class with prototype methods?
I was at a bar last night with a few of my colleagues. They said that it's a bad idea to extend the functionality of basic JavaScript Objects with a prototype method.
For example, let's say you ...
0
votes
2answers
66 views
problems compiling a function with a trait Add in Rust [closed]
I'm trying to write a generic function summ in rust - but to no avail. Could someone please elucidate the problem?
fn summ<T:Add>(a:T,b:T)->T {
a+b
}
0
votes
1answer
187 views
Const and non-const methods, and possible mutable data member?
I'm working on a design and implementation change due to a bug. The bug fix requires us to modify a class object in a const method. I think the two options are:
Have the non-const method cast this ...
0
votes
4answers
172 views
How can Guard Statements and Small Functions coexist?
By Guard Statements I mean something similar to the first part of the function:
def doSomething(String something)
{
// Guard Statement
if(!something)
{
return false
}
// ...
1
vote
2answers
111 views
Are there constraints for functions in structured programming?
I just talking with a colleague (University instructor) who teaches C (Fundamental of programming Course). He said I won't give score to a student, if he uses I/O (scanf or printf or cin count) in a ...
2
votes
2answers
192 views
Haskell types for functions
I don't understand the answer to this question:
Q: Can Haskell find a type for the function selfapply defined by: selfapply f = f f
A: The function selfapply is not typeable in the simple ...
0
votes
2answers
162 views
Is there a approximate string comparison function which is odd like mathematical functions are odd?
I say odd like a mathematical function, a mathematical function is odd if
f(-x) = -f(x)
I understand that comparing two strings may seem to have little to do with this, because the arguments cannot ...
0
votes
1answer
313 views
Setting up folders and importing modules in Python
I'm building an app to automate some processes that are annoying to do by hand (looking in a file, taking certain information out of a file, building another file with that information).
My project ...
1
vote
1answer
142 views
When should one create a new py-function rather than tweaking another one?
Me and a co-developer are arguing when it's wise to cast a new function rather than tweaking another one. By tweaking I mean an option or a hidden check in an existing function. The question could be ...
1
vote
2answers
237 views
Are any side effects not concrete side effects?
In Chapter 23 of "Object Oriented Software Construction" (1988), Betrand Meyer makes a distinction between side effects, concrete side effects, and abstract side effects.
Meyer defines a side effect ...
26
votes
5answers
2k views
Method extraction vs underlying assumptions
When I split big methods (or procedures, or functions — this question is not specific to OOP, but since I work in OOP languages 99% of the time, it's the terminology that I'm most comfortable with) ...
2
votes
3answers
537 views
How are basic functions implemented in a programming language if they are not built in? [closed]
The lowest levels of a programming language's library functions are always obscure and often have no code that looks like it even remotely does anything. Some languages have these functions like ...
0
votes
1answer
151 views
Can i create a function with variable parameters in C?
I've always wondered where a single function such as printf(); can take in variable parameters and provide the right results. For example,
printf("Number is %d",a);
which has two parameters, and
...
1
vote
2answers
59 views
Function that would output all distinct values in an array
I want to create a function that outputs the distinct values in a given array. For example, in the following sequence 2 2 1 1 5 2 , the distinct digits are 2 1 5. What I've done till now is:
void ...
31
votes
1answer
2k views
Name for a Function which Returns its Arguments?
A function that does nothing, takes no arguments and returns nothing is traditionally called a noop, or no-op. An example of a noop is below:
function noop(){}
http://en.wikipedia.org/wiki/NOP
So ...