Function is a block of code which performs a specific task.
16
votes
5answers
3k views
Function returning true/false vs. void when succeeding and throwing an exception when failing
I'm building an API, a function that uploads a file. This function will return nothing/void if the file was uploaded correctly and throws an exception when there was some problem.
Why an exception ...
-3
votes
1answer
73 views
Python recursion in functions [closed]
I am just learning about recursive functions. This was an example given in a textbook where t is a turtle. I was wondering how the code works, but more specifically, how the code runs past the first ...
0
votes
2answers
64 views
How to name variables that clash with functions?
As an example, in Python I have a lowest_prime_factor function which does it exactly what it says it does. Then, in another function, I need to call this function and store it. In another language ...
0
votes
2answers
134 views
How to 'read' arrow functions in ES6?
I've been trying to learn some of the new features of ES6, and while most of it makes sense, I'm having trouble grasping the arrow function. I'm not asking so much why it exists as I am how to read it....
1
vote
1answer
96 views
How do you organize your hoisted functions?
In a module (file) I organize several functions related to a specific domain task. I usually put the function linearly from top to bottom, then compose a functional call on the bottom. Doing so I can ...
-1
votes
2answers
165 views
One Word for Functions, Variables, and Objects
Is there a single word that can describe JavaScript functions, variables, and objects? I was thinking "entities", but that word is usually used to describe the mapping between objects and a database. ...
2
votes
1answer
196 views
Can I add to a built-in function?
I want be to be able to re-write the code of an existing PHP function in an abstract manner.
Here is an example:
scandir() scans a directory for files and folders, and returns the relative paths '.' ...
5
votes
1answer
283 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 ...
16
votes
6answers
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 ...
0
votes
1answer
181 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
174 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
132 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 ...
0
votes
1answer
83 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
243 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
147 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
122 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
100 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
239 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
123 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
59 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
64 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
3answers
467 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
151 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
261 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
142 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
143 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
179 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
65 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, ...
10
votes
5answers
757 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
170 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
295 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
111 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
156 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
671 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
277 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
431 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
214 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
290 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
68 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
208 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
179 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
200 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
163 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
345 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
156 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 ...