Most voted "function" questions
The function (also called procedure, method, subprogram, or routine) is a portion of code intended to perform a single, specific task.
Learn more…1,309 questions
Sort by count of
-
152
votes4
answers6835
viewsWhy should I only use a "Return" in each function?
It is common to see the recommendation to use only one return by function/method. But this seems somewhat meaningless and leaves the code more confusing in many cases. See the examples: The way I…
-
43
votes3
answers8167
viewsWhen to use recursion and when to use loops?
A problem can be solved and get the same result used a loop or through recursive calls to a function. Whereas the programming language being used has both features, how to know when one is better…
function loop software-engineering encoding-style recursionasked 10 years, 6 months ago Maniero 444,682 -
33
votes1
answer522
viewsDoes the size of a function affect memory performance and consumption?
Does the fact that a function is higher or lower affect the performance of the application? Especially in PHP. If you need performance, it would be better to perform large or small? And memory…
-
31
votes6
answers7191
viewsHow to put default (default) arguments in a Javascript function?
In R this would be very simple: funcao <- function(x=10) return(x) If you call funcao() without arguments the result will be 10. How to do the same in Javascript? And how to know if there are…
-
29
votes3
answers660
viewsShould we validate function parameters?
In languages like Java, method parameters are "validated" in the build (at least the type): public void facaAlgo(String str) { // ... } // em algum outro lugar: int i = 2; this.facaAlgo(i); // erro…
-
28
votes6
answers1694
viewsWhy not use a Boolean parameter?
I’ve seen in some places you shouldn’t wear something like this: int teste(object obj, bool especial) { ... } There within the function some operation will be done or not, depending on what you…
function encoding-style software-engineering parameters booleanasked 7 years, 9 months ago Maniero 444,682 -
27
votes6
answers1673
viewsBig job or small job?
Why is creating a large function or method with many lines of code considered a "bad practice"? What are the drawbacks of this? What do I get in dividing into smaller functions or methods? What…
-
26
votes3
answers2363
viewsWhat does it mean: "functions are first class objects in Javascript"?
Several times I have heard this: "functions are first class objects in Javascript". Functions being stored in variables and passed in methods and so on was not something that surprised me so much in…
-
26
votes4
answers6194
viewsBest practices for naming functions
I would like to know which tenses are most used to name the functions. For example, there is a more suitable in this hypothetical case? Calculadora calculadora; calculadora.soma(3, 4);…
-
22
votes2
answers609
viewsCould current Javascript engines optimize "tail" recursive calls?
In functional programming, it is very common to use functions recursive. Certain languages, such as Scheme, do not even have control structures for loops, depending on recursion to iterate over…
-
21
votes1
answer329
viewsHow to implement memoization in a PHP function?
I saw it today in a reply the following code: function fibonacci($n) { $a = 0; $b = 1; $c = 1; for ($i = 1; $i < $n ; $i++) { $c = $a + $b; $a = $b; $b = $c; } return $c; } echo fibonacci(100)…
-
20
votes4
answers19928
viewsstrtoupper() with accents
The function strtoupper() PHP is not turning the accented letters into uppercase, see the example: echo strtoupper("virá"); // retorna VIRá Do you have any native function that solves this problem?…
-
20
votes1
answer4057
viewsWhat is the difference between statement and definition?
These things seem to be the same thing. Are they? And assignment is different? The terms are interchangeable.
function terminology language-independent variable-declarationasked 8 years, 6 months ago Maniero 444,682 -
20
votes4
answers2408
viewsWhat is buffer overflow?
Whenever I use the function gets() the compiler returns me the following warning: Function is Dangerous and should not be used Translation: this function is dangerous and should not be used I hear a…
-
20
votes4
answers9040
viewsHow to return 2 or more values at once in a method?
It is common to see examples of methods returning one value at a time. I’ve seen cases where you need to create a class only to package the data and be able to return more values. Use the generic…
-
19
votes1
answer639
viewsWhat happens when we call a function?
At the machine instruction level, what will happen on the call?
-
17
votes4
answers1239
viewsFormat city names and ignore words like "do", "dos", "das", "da", etc
I’m working with Webservice whose city names are all out of shape and I’d like to create a function to treat the names evenly. An example is this: CHICKEN HARBOR I’d like to keep it that way: Porto…
-
16
votes3
answers568
viewsIs there an advantage to an explicit "self" instead of the implicit "this"?
Unlike most [most popular] object-oriented languages, which use a keyword to refer to the object that is "target" of a method call (commonly called this), the Python language requires every method…
-
14
votes2
answers341
viewsFirst-class functions: Why should input types be counter-variants?
To demonstrate the problem I will use Scala code (although this is a rule formalized by Luca Cardelli). trait Function1[-A, +R] { def apply(x: A): R } In Scala this means that a function with an…
-
14
votes2
answers5459
viewsWhat are middleware in Nodejs?
What are middleware and how important it is for the Node platform?
-
14
votes5
answers13398
views -
14
votes2
answers2762
viewsWhat is the purpose of the void parameter in functions in the C language?
The parameter void it’s just semantic or he does something I don’t know? int main() { return 0; } int main(void) { return 0; }
-
14
votes2
answers2108
viewsAre function and method the same thing?
When we talk about methods and functions, we are talking about the same thing? For example: function blablabla blabla That is a method?
-
14
votes1
answer368
viewsWhat is abstraction and how does it influence the creation of functions?
I was reading a answer about Python user’s @Cool in which he mentions the term abstraction in relation to complexity, functions and object orientation, see: What you can do if you can’t change the…
-
13
votes2
answers460
viewsWhat is Closure Object and how do I get the anonymous function return as parameter?
Let’s say I have a class, and in that class i have a method, and in a parameter that method it is possible to use a anonymous function thus: Class and method: class Classe { private $exemplo = [];…
-
13
votes1
answer1093
views -
13
votes4
answers4494
views -
13
votes4
answers542
viewsIs there a design standard or recommendation that defines the optimal amount of parameters a function should have?
I always value writing my codes in a short and readable way. My motto is always to think that "someday someone will mess with my code and I want the person who does it to understand it easily".…
function encoding-style parameters language-independent argumentasked 6 years, 5 months ago Wallace Maxters 102,340 -
12
votes2
answers151
viewsName for anonymous functions
In some example code frameworks, libs, etc. I found that anonymous functions were passed with a name to the same. minhaFuncao(function minhaFuncaoAnonima() { // ... }); What is the purpose of naming…
-
12
votes2
answers15638
viewsWhat is the difference between Function and Procedure?
What are the differences between the two, and examples of where and generally are used.
-
12
votes3
answers5645
viewsWhat is the purpose of declaring a function within a function?
In Python it is possible to declare a function within another function, as shown in the following code. def foo(palavra=None): print(palavra) def bar(outra_palavra=None): print(outra_palavra) if…
-
12
votes1
answer3054
viewsWhat is a pure function?
When studying functional programming, I heard a lot of the term "pure function", or Pure Function. What characterizes this type of function and what is its importance for the functional paradigm?…
function terminology language-independent functional-programmingasked 7 years, 1 month ago vinibrsl 19,711 -
12
votes4
answers4581
viewsHow to multiply in Python without the multiplication operator?
I have a task and I’m having trouble completing it. What I did was this:: m= int(input('Digite o primeiro fator:')) n= int(input('Digite o segundo fator:')) def multiplica(numero): while m > 0:…
-
12
votes1
answer1392
viewsWhy, in C, does a function need to be declared before it is used?
This question was asked on Facebook. Lé is a problem because there is no room for a good answer, there is no way to classify the answers as to their quality properly, much appreciated by the…
-
11
votes3
answers17410
viewsHow to create a Javascript function that accepts an arbitrary number of arguments?
Functions of the type console.log take any number of arguments. How to specify this for a Javascript function?
-
11
votes3
answers2755
viewsHow to assign the results of a function that returns a list of objects?
In R we can make a function return more than one object through a list. But how to assign these objects to two distinct variables? Example: f<-function(){ primeiro<-1:10 segundo<-11:21…
-
11
votes3
answers1593
viewsFunction as parameter in jQuery functions
I always used some functions without really understanding what was happening and where such parameters came from. I speak of the following: Example 1: $("#link").on("click", function(event){…
-
11
votes2
answers1824
viewsIs there a difference between strtr and str_replace?
I was analyzing the way functions behave strtr and str_replace And I noticed that, at first glance, they both seem to do exactly the same thing. Example: $translators = [ 'da hora' => 'bacana',…
-
11
votes2
answers1385
viewsWhen to use "inline"?
Everybody says you don’t have to use inline functions since the compiler knows what to do better than the programmer. But if it has in the language it should serve for something. Is it useful in any…
-
11
votes2
answers8828
viewsAfter all, what is the function Repr in python for?
I’m a bit of a beginner in python and I wanted to know what this confusing function is really for, I looked at several sites but no one has really said what it’s for
-
11
votes2
answers2509
viewsWhat is the function of Function in jQuery and what is the right way or time to use?
Whenever I go to work with jQuery I just create: $(function() { }); Because it was the first way I learned, at first I thought it was a "int main()" pattern in C, but then I came across other forms…
-
10
votes2
answers2045
viewsAre functions and methods in PHP case-insensitive?
Some time ago, by an accident at the time of a debug I realized that PHP makes no difference between upper and lower case when calling a function. Example: print_r($teste); print_R($teste);…
-
10
votes2
answers246
viewsHow to make a "Hello Word!" that way
Without using the items below: String-like strings and functions before output Numbers Regular Expressions Functions with the names: "Hello", "World", "Helloworld" or anything similar only the…
-
10
votes1
answer7950
viewsWhat is the difference between functions and procedures?
I’m studying algorithms and I’m having a hard time understanding the difference between them and when to use these sub-algorithms in a program. I’m learning to code with algorithm in Portugol.…
-
10
votes1
answer257
viewsWhat are extended functions?
I’m writing an article on programming in Kotlin, I came across this name, which are extended functions?
android function terminology characteristic-language kotlinasked 7 years, 2 months ago Lucas Vieira 397 -
10
votes3
answers168
viewsWhy is it necessary to create a function to execute certain methods?
.onclick = function() {myFunction()}; Why the example below does not work? .onclick = myFunction() It runs without me having clicked! <script> document.getElementById("demo").onclick =…
-
10
votes3
answers302
viewsWhat is the cost of calling many functions?
Recently, faced with a discussion about Clean Code and programming best practices, a co-worker commented that in his previous job he had a lot of resistance on the part of the other programmers to…
-
10
votes2
answers930
viewsPrototype functions in C/C++
What kinds of functions are these? What can these prototypes do? /*1*/int func ( int (*x)(int,int) ) /*2*/int func ( int x(int,int) ) /*3*/int func1 ( int(fn)() ) /*4*/int func2 ( int(*fn)() ) Is…
-
10
votes3
answers598
viewsWhat is the difference between keys and parentheses in an Arrow Function in Javascript?
What is the relevant difference between keys ({ }) and parentheses (( )) in a return of a function? const example = () => ( ... ); Versus: const example = () => { ... };…
javascript function characteristic-language syntax arrow-functionsasked 4 years, 9 months ago novic 35,673 -
10
votes1
answer177
viewsWhat is Type Annotation?
I’m reading a book about Typescript and I came across something that caught my attention which is the Type Annotation. Take the example: function foo(): { a: number, b?: number } { if (this.a <=…