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

learn more… | top users | synonyms

7
votes
8answers
3k 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
9answers
3k views

Why is Today() an example of an impure function?

It seems like, when reading something like this Wikipedia article about "pure functions", they list Today() as an example of an impure function but it seems pretty pure to me. Is it because there is ...
3
votes
2answers
695 views

How can “hash functions” be used to implement hash maps at all?

My understandment is that hash maps allow us to link, say, a string, to certain memory location. But if every string were to be linked to a unique place in memory it would need a huge block of empty ...
19
votes
2answers
1k views

Is it a good idea to provide different function signatures that do the same thing?

Here is a C++ class that gets constructed with three values. class Foo{ //Constructor Foo(std::string, int, char); private: std::string foo; char bar; int baz; }; All of ...
0
votes
2answers
7k views

Drawing Flowchart for function calculate a number in the Fibonacci Series

I'm trying make Flowchart for function calculate a number in the Fibonacci Series. But It looks like not right. I don't how draw the recursive function. Please help me how to fix it. My flowchart: ...
22
votes
4answers
9k 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 ...
3
votes
1answer
156 views

Returning status code where one of many errors could have occured

I'm developing a PHP login component which includes functions to manipulate the User object, such as $User->changePassword(string $old, string $new) What I need some advice with is how to return a ...
9
votes
3answers
911 views

Why can't we write nested shorthand functions in Clojure?

I tried to evaluate a Clojure expression with nested shorthand functions today, and it wouldn't let me. The expression was: (#(+ % (#(+ % (* % %)) %)) 5) ; sorry for the eye bleed The output was: ...
2
votes
1answer
142 views

Does automatic returning affect performance?

There's a bunch of languages that automatically return the last value in a function (mostly functional) like Ruby, Haskell, Lisp, etc. Does this feature (or what do you call it) affect the ...
0
votes
1answer
172 views

The idea of functionN in Scala / Functionaljava

From brain driven development It turns out, that every Function you’ll ever define in Scala, will become an instance of an Implementation which will feature a certain Function Trait. ...
0
votes
2answers
69 views

Looking for terminology for the relation of a subject and a predicate

While writing some predicates for collection filtering I have stumbled over the choice of the right words for the relation of the subject and the predicate (English is a foreign language for me). What ...
6
votes
3answers
449 views

Functional Methods on Collections

I'm learning Scala and am a little bewildered by all the methods (higher-order functions) available on the collections. Which ones produce more results than the original collection, which ones ...
9
votes
1answer
1k views

Is there a difference between arguments and parameters?

It might be like this: Parameter means from the callers POV and arguments mean inside the procedure, or other way round. Or is there no difference? Update In Swedish we say "anropsparametrar" i.e. ...
5
votes
2answers
455 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 ...
6
votes
2answers
530 views

Is it bad practice to output from within a function?

For example, should I be doing something like: <?php function output_message($message,$type='success') { ?> <p class="<?php echo $type; ?>"><?php echo $message; ...
5
votes
2answers
388 views

Function that requires many parameters

I have a problem related to this: Are there guidelines on how many parameters a function should accept? In my case, I have a function that describes a rounded rectangle. The caller specifies An ...
4
votes
4answers
1k views

Best Method of function parameter validation

I've been dabbling with the idea of creating my own CMS for the experience and because it would be fun to run my website off my own code base. One of the decisions I keep coming back to is how best ...
18
votes
4answers
8k views

Function declaration as var instead of function

More and more I'm seeing functions being declared like var foo = function() { // things }; Instead of how I had learned, like function foo() { // things } What's the difference? Better ...
14
votes
1answer
1k views

Why does the C library use macros and functions with same name?

I am reading 'The Standard C Library' by PJ Plauger which is really interesting. The book explains not only how to USE the library but also how it is implemented. I have finished reading the ctype.h ...
3
votes
5answers
422 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. ...
3
votes
3answers
350 views

How are functions called when passing ++

I don't understand what is passed to the function f() when I call it like this. main() { void f(int,int); int i=10; f(i,i++); } void f(int i,int j) { printf("%d %d",i,j); } gives me 11 10 ...
11
votes
5answers
303 views

Convert list of 24-hour-precipitation values into total-by-hour

Let's say I have a list of precipitation values by hour, each showing how much rain happened in the prior 24 hours, ordered by date. For example: { '2012-05-24 12:00': 0.5, // .5" of rain from ...
6
votes
4answers
1k views

What is the name for a NON-self-calling function?

I have a collection of normal functions and self-calling functions within a javascript file. In my comments i want to say something along the lines of "This script can contain both self-calling and ...
2
votes
3answers
978 views

Call stack starts at bottom or top?

A stack is something that piles bottom-up. Hence a call stack adds new items on the stack when functions are called with items being removed from the stack as each function ends until the stack is ...
29
votes
9answers
6k 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 ...
-8
votes
6answers
3k views

Function like C# properties?

I've been thinking about how C# properties work and could work. I know the purpose that C# properties were originally designed for, which is certainly useful. However instead in this question I'm ...
0
votes
1answer
135 views

Using functions as statements on Python

A great feature of Javascript is function statements. You can do this: (function myFunc(){ doSomething(); doSomethingElse(); })(); Which is a way to declare a function and call it without ...
4
votes
5answers
532 views

Using php functions, reserved words as local identifiers [closed]

I'm thinking of some of the array functions. "key", "each", "pos", "range". These are often very useful as local identifiers! I have also seen code that (ab)uses $return, $list, $array, $string. What ...
16
votes
3answers
6k views

Where should I put functions that are not related to a class?

I am working on a C++ project where I have a bunch of math functions that I initially wrote to use as part of a class. As I've been writing more code, though, I've realized I need these math functions ...
3
votes
5answers
2k 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 ...
2
votes
2answers
653 views

What is Module Object and Function Object?

I am currently teaching myself Python, using the GNU licensed book "Introduction to Computer Science using Python." In chapter 3, functions are covered. While I understand the concept of functions ...
3
votes
2answers
257 views

Should I use chained functions in Java?

From time to time, I'll have a class in Java that takes a multitude of parameters, however, sometimes when I am creating an object of this class, I don't need to use all the parameters. As of now, I ...
3
votes
5answers
728 views

Functions with side-effects in Delphi/Pascal

What is the proper approach to functions that have side-effects in Delphi/Pascal? For example, I could have a boolean function DeleteFile that returns True if the file was deleted and False ...
84
votes
10answers
6k views

One-line functions that are called only once

Consider a parameterless (edit: not necessarily) function that performs a single line of code, and is called only once in the program (though it is not impossible that it'll be needed again in the ...
8
votes
4answers
1k views

When would dynamic scoping be useful?

With dynamic scoping, a callee can access the variables of its caller. Pseudo C code: void foo() { print(x); } void bar() { int x = 42; foo(); } Since I have never programmed in a ...
3
votes
2answers
1k views

Setting $_POST variables as a means of passing data / Not passing parameters in functions

I've got a legacy PHP web application wherein almost each and every function makes references to $_POST variables - retrieving their values, AND setting them (or setting new POST variables) as a means ...
10
votes
8answers
5k views

Feature vs. Function

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 ...
3
votes
8answers
448 views

Wrapping simple statement in a function in java?

I was working on neo4j graph database in java. To get the reference node of this db: GraphDatabaseService graphDb=new EmbeddedGraphDatabase(DB_PATH); Node Root=graphDb.getReferenceNode() I ...
10
votes
2answers
696 views

Design: Object method vs separate class's method which takes Object as parameter?

For example, is it better to do: Pdf pdf = new Pdf(); pdf.Print(); or: Pdf pdf = new Pdf(); PdfPrinter printer = new PdfPrinter(); printer.Print(pdf); Another example: Country m = new ...
3
votes
4answers
871 views

Is function memoization really only for primitives?

I was thinking about this for quite some time. Is function memoization really only for primitives? I currently have this piece of code: Public Shared Function Mize(Of TArg1 As Structure, ...
6
votes
3answers
2k views

Good use of wrapper functions?

What do you consider good use of wrapper functions? When are they useful abstractions and in what cases harmful and unnecessary complexity?
13
votes
5answers
2k views

Why are PHP function signatures so inconsistent?

I was going through some PHP functions and I could not help notice the following: <?php function foo(&$var) { } foo($a); // $a is "created" and assigned to null $b = array(); foo($b['b']); ...
5
votes
3answers
1k views

Static methods or static functions?

I was just reading http://stackoverflow.com/questions/155609/what-is-the-difference-between-a-method-and-a-function and all of a sudden, the thing came to my mind was the static methods. As static ...
12
votes
7answers
3k views

Is there an optimal number of lines of code per function? [closed]

Functions are not only used to minimize duplication of code - they are also used to split up a long function into smaller ones to increase readability, as well as making the code self-commenting. Yet ...
6
votes
18answers
2k views

What do you name functions/variables/etc when you can't think of a good name?

When you are defining a function/variable/etc and are not sure what to name it, what do you name it? How do you come up with a name? If you use a temporary name as a place-card until you give it it's ...