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

learn more… | top users | synonyms

17
votes
11answers
1k views

Why are we supposed to use short functions to sectionalize our code? [duplicate]

I've seen an increasing trend in the programming world saying that it is good practice to separate code blocks into their own functions. Obviously, if that code block is reusable, you should do that. ...
0
votes
1answer
78 views

Good practice to use namespace or prefix to indicate what file function is from? [duplicate]

For example, I have the function generate_salt() in encryption.file but the person may not know where generate_salt() is from. Using a namespace like encryption::generate_salt(), or ...
2
votes
3answers
131 views

Non-trivial get function, should it be named differently

For example I have this code. class Command { var $responseFactory; var $service; /** * Returns the class name of the responseFactory if it is set * or the default class name ...
0
votes
5answers
716 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 ...
44
votes
4answers
3k views

How do functional languages handle random numbers?

What I mean about that is that in nearly every tutorial I've read about functional languages, is that one of the great things about functions, is that if you call a function with the same parameters ...
6
votes
2answers
3k views

Reason for return statement in recursive function call

I just had a doubt in my mind. The following subroutine(to search an element, in a list, for example) has a return statement at the end: list *search_list(list *l, item_type x) { if (l == NULL) ...
2
votes
4answers
330 views

How do you decide what code to put into a function?

I started out with a script that was a few hundred lines. Later, I realized I wanted another script that would require much of the same code. I decided to wrap certain areas of the original script ...
1
vote
6answers
429 views

Naming functions that retrieve a value

I have this personal rule to start all function/method names with a verb. My verb of choice for functions or methods that get a value based on some data structure or object is get. I'm wondering if ...
0
votes
1answer
100 views

Show all definitions in Scheme?

I want to see all user-made definitions in a Scheme REPL, both loaded from files and entered at the REPL. Is there any way to "dump all definitions"? E.g. if there is: (define (plusone x) (+ 1 ...
0
votes
1answer
316 views

PHP function types

I am trying to find a way of classifying different types of PHP functions. For example that fopen, fwrite, fclose and so on are all part of IO, and the MySQL functions and MySQLi class is all for ...
17
votes
2answers
1k views

Boolean Method Naming Affirmative vs Negative

Should boolean methods always take the affirmative form, even when they will only ever be used in the negative form? Say I wanted to check whether an entity exists before creating one, my argument is ...
1
vote
1answer
161 views

Documenting Function That Takes Random Parameters?

What's the best approach to creating documentation (displaying the function prototype if you will) for functions that take a variety of different forms in terms of parameters. Let's say there are 10 ...
3
votes
2answers
499 views

Programming by Intention, Depth-First or Breadth-First?

Say I have the following graph of dependencies between procedures/functions/methods: o / \ v e / \ / \ r f l w That is, function o first calls function v, and then ...
112
votes
13answers
24k views

Is it OK to split long functions and methods into smaller ones even though they won't be called by anything else? [duplicate]

Lately I've been trying to split long methods into several short ones. For example: I have a process_url() function which splits URLs into components and then assigns them to some objects via their ...
11
votes
4answers
7k views

C++ why & how are virtual functions slower?

Can anyone explain in detail, how exactly the virtual table works & what pointers are associated when virtual functions are called. If they are actually slower, can you show the time that the ...
4
votes
6answers
5k views

CoffeeScript and Named Functions

Elsewhere: an argument has arisen over the terminology of a named function in CoffeeScript. In particular somebody refereed to something like this foo = -> console.log("bar") as a named ...
0
votes
1answer
157 views

LOOP program only need inc and zero

I have 4 different commands in LOOP programming language: y=Zero() y=Val(x)=copy x and put it in register y y=Inc(x)=x+1 y=Dec(x)=x-1 Finally I also have loop n times { ... ...
2
votes
1answer
154 views

LOOP-computable functions

I was just reading a chapter about LOOP-computable functions and I have the following question: Is it possible to numerate every LOOP program with an algorithm? Formally: Is it possible to have a ...
4
votes
3answers
397 views

Renaming long named method in C# [closed]

I'm working on a project where exist one method with title string ValidateNewPasswordExpireCurrentPasswordAndCreateNewPassword(...) I'm sure the method name must be changed. But can't found good ...
8
votes
8answers
4k 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 ...
37
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 ...
4
votes
2answers
879 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
11k 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: ...
26
votes
4answers
15k 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
168 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 ...
8
votes
3answers
1k 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
143 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
207 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
83 views

Looking for terminology for the relation of a subject and a predicate [closed]

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
515 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
2k 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
492 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
548 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
415 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 ...
19
votes
4answers
12k 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 ...
13
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
464 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
387 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
326 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
1k 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 ...
34
votes
9answers
9k 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
4k 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
147 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
616 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 ...
19
votes
3answers
8k 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
916 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 ...