Function is a block of code which performs a specific task.

learn more… | top users | synonyms

0
votes
2answers
90 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
74 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
187 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 ...
5
votes
6answers
4k views

Template Functions and Function templates in C++

I came across a question which asked the difference between Function Templates and Template Functions in C++ and also Template Classes and Class Templates. I was under the impression that Function ...
0
votes
1answer
108 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 ...
4
votes
2answers
121 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 ...
4
votes
4answers
13k views

What's the difference between a function and a method?

I've heard that methods are more Object-Oriented than functions. I was wondering if someone could show me an example of a function and a method and explain the differences between methods and ...
-1
votes
0answers
67 views

Please let me know more refactoring strategies for my codebase?

I have written a shell command-line interpreter which uses loops and recursion (but maybe not "recursive decent", that usually solves this problem) I want to refactor the function to 4 or 5 smaller ...
3
votes
4answers
232 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 ...
0
votes
3answers
83 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
1answer
120 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
1answer
59 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
338 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 ...
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(){ ...
4
votes
1answer
135 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 ...
1
vote
3answers
202 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): ...
2
votes
1answer
131 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 ...
51
votes
9answers
28k views

Are there guidelines on how many parameters a function should accept?

I've noticed a few functions I work with have 6 or more parameters, whereas in most libraries I use it is rare to find a function that takes more than 3. Often a lot of these extra parameters are ...
1
vote
4answers
132 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 // ...
1
vote
1answer
127 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 ...
14
votes
5answers
8k views

Multiple arguments in function call vs single array

I have a function that takes in a set of parameters, then applies to them as conditions to an SQL query. However, while I favored a single argument array containing the conditions themselves: ...
2
votes
2answers
188 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
5answers
1k views

Named output parameters vs return values

Which code is better: // C++ void handle_message(...some input parameters..., bool& wasHandled) void set_some_value(int newValue, int* oldValue = nullptr) // C# void handle_message(...some ...
8
votes
4answers
375 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 { ...
-1
votes
2answers
55 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, ...
0
votes
1answer
129 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
257 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
103 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
126 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. ...
0
votes
1answer
309 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 ...
5
votes
3answers
542 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
216 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
420 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
179 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('/', ...
5
votes
3answers
720 views

What can procs and lambdas do that functions can't in ruby

I've been working in Ruby for the last couple weeks, and I've come to the subject of procs, lambdas and blocks. After reading a fair share of examples from a variety of sources, I don't how they're ...
2
votes
2answers
196 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 ...
17
votes
4answers
15k views

In C++ why and how are virtual functions slower?

Can anyone explain in detail, how exactly the virtual table works and what pointers are associated when virtual functions are called. If they are actually slower, can you show the time that the ...
0
votes
2answers
65 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 }
7
votes
5answers
413 views

Should private functions be held to the same standards as public functions?

If I'm building private utility functions, should they be held to the same rigorous standards in terms of handling invalid data as public functions? Example:If I'm writing code to calculate the ...
0
votes
1answer
180 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
167 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 } // ...
4
votes
8answers
609 views

Confusion regarding def function within Python

I've been learning Python for about 2 months now (Started with Learn Python The Hard Way, now reading Dive Into Python), and within both books, I still seem to be confused over this one bit of code. ...
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 ...
9
votes
9answers
11k views

Why Java does not allow function definitions to be present outside of the class?

Unlike C++, in Java, we cannot have just function declarations in the class and definitions outside of the class. Why is it so? Is it to emphasize that a single file in Java should contain only one ...
38
votes
4answers
45k views

Why store a function inside a python dictionary?

I'm a python beginner, and I just learned a technique involving dictionaries and functions. The syntax is easy and it seems like a trivial thing, but my python senses are tingling. Something tells me ...
14
votes
8answers
11k views

Feature vs. Function [closed]

Often I hear PMs (Project Managers) talk about feature and function. And I'm just so puzzled to differentiate them. Sometimes I think of a feature to be equivalent to a user story. Something like "As ...
0
votes
2answers
161 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 ...
1
vote
1answer
140 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 ...