Tagged Questions

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

learn more… | top users | synonyms

0
votes
0answers
6 views

Hidden event handlers and their parameters

I'm having a bit of trouble understanding why has an event handler to be defined as a function. AFAIK, a function can be used as anonymous called procedure as event handler It defines a ...
-3
votes
0answers
31 views

Adding function parameters to my program [closed]

So how would I add parameters to this program? I hate having to add these. I'm cofused by why and when we needed. I have tried everything. https://gist.github.com/anonymous/93918af7ec382fea47ca
0
votes
2answers
70 views

memory needed to store a function in javascript

I have been using sizeof.js to investigate the size of various objects in javascript. It appears from this that the size of a function is essentially zero bytes, regardless of how many instructions ...
0
votes
0answers
13 views

Returning the contents of a byte Array Function C# [migrated]

I'm trying to get better at coding and more specifically, getting in to the habit of using functions to do things rather than have everything in "Main". I can't seem to find an answer which I can get ...
1
vote
0answers
86 views

Python: Why are int & list function parameters differently treated? [migrated]

We all know the dogma that global variables are bad. As I began to learn python I read parameters passed to functions are treated as local variables inside the funktion. This seems to be at least half ...
0
votes
0answers
21 views

Python - Function - Argument said to be “undefined” [migrated]

I'm currently taking a Python course, and got to the chapter in our book that talks about functions. (Please note, this is my first time learning any programming.) One of the exercises I'm working on ...
1
vote
2answers
127 views

How does the CPU access the values of stack-allocated variables? [duplicate]

Consider the following C++ function: void doStuff() { Thing thingA; Thing thingB; thingA.doSomething(); // .. etc } During the execution of this function, variables thingA and ...
20
votes
8answers
4k views

What is the benefit of a function without parameters which only calls another function

A tutorial (for Javascript) I'm doing suggests we write a function like this one: function sayHello() { //Some comments explaining the next line window.alert("Hello"); } Other than ...
1
vote
0answers
44 views

PHPExcel Application With Cases

I've been working on an application for my company's intraweb which will automate production scheduling based on information from our legacy mainframe databases in addition to side tables with ...
35
votes
12answers
4k views

Why do most programming languages have special keyword or syntax for declaring functions? [closed]

Most programming languages (both dynamically and statically typed languages) have special keyword and/or syntax that looks much different than declaring variables for declaring functions. I see ...
0
votes
2answers
126 views

Is there a way for Object 1 to call Object 2's enums in a function call?

For example, if I have two classes "Director" and "Follower". I want the Director to tell the follower where to go (ex: follower1.go(direction.LEFT)), and I want the Director to know what directions ...
0
votes
1answer
88 views

Why does this function work even though the vars are different

$inumber1 = 10; $inumber2 = 20; function add($number1, $number2) { echo $number1 + $number2; } add($inumber1, $inumber2); I'm learning PHP coding for the first time and I'm ...
2
votes
1answer
171 views

How do multiple levels of inheritance affect virtual function call overhead in C++?

I am considering using a class hierarchy with more than a single level of inheritance, where virtual member functions form a "chain", for example: struct Base { virtual void foo(); }; struct D1 : ...
0
votes
1answer
85 views

How to split a Service class in two but still use them like one?

I've got a class: AuthenticationService findLoggedInUser() Checks session if User is logged in. If not, check client persistent user login cookie and log in. loginUser($email, $pw, $remember = ...
14
votes
7answers
2k views

Function only returns unchanged parameter, useless?

I just found this function in the project I'm working at: -- Just returns the text unchanged. -- Note: <text> may be nil, function must return nil in that case! function Widget:wtr(text) ...
39
votes
9answers
5k views

Beginning a sentence with a function name? [closed]

Occasionally while typing something up that relates to a case-sensitive programming language I end up starting a sentence with a function name. Now the rules of English state that the first word in a ...
0
votes
1answer
303 views

Does this function do one thing only?

Is the following method considered to be doing one thing only? I'm wondering about that since it takes an optional argument. public function findErrors($name = null) { if ($name) { ...
9
votes
4answers
456 views

Are first-class functions a substitute for the Strategy pattern?

The Strategy design pattern is often regarded as a substitute for first-class functions in languages that lack them. So for example say you wanted to pass functionality into an object. In Java you'd ...
28
votes
8answers
5k views

Is splitting up a function into several inner functions an anti-pattern? [duplicate]

Imagine a long and complicated process, which is started by calling function foo(). There are several consecutive steps in this process, each of them depending on result of the previous step. The ...
0
votes
2answers
110 views

Should you avoid adding unecessary stack frames?

I have been working with laravel (PHP) recently, and there is an option when using their fluent ORM to define where clauses using closures. This gives us two, functionally equivalent, ways to specify ...
2
votes
2answers
136 views

Property or Method to obtain information about a collection?

Suppose I have a MyList class which extends ObservableCollection<MyObject> where MyObject has the Boolean Property Enabled. Let's say I want the number of MyObject objects that have the ...
0
votes
3answers
159 views

Use of malloc in C

Is it necessary to call free function every time we use malloc in C. I am asking this because I have seen many times that it is not called . Thank you
0
votes
1answer
233 views

static or non-static, that is the question? [duplicate]

For comparisons / evaluations of objects of the same class(and other purposes), is it better to define a static or a non-static (is this called "dynamic" by chance?) function? Sample code: class ...
1
vote
2answers
180 views

What is the concept behind writing a cancel operation in c++? [closed]

I'm attempting to write a cancel operation for a software download application. This application will first transfer the software to the device and then install the software on it. (These are givens ...
0
votes
3answers
136 views

Writing a method to 'transform' an immutable object: how should I approach this?

(While this question has to do with a concrete coding dilemma, it's mostly about what's the best way to design a function.) I'm writing a method that should take two Color objects, and gradually ...
4
votes
2answers
563 views

Why are there no function pointers in Java? [closed]

Lately I started studying about different interesting concepts that exist in languages other than Java. Since the only language I've ever programmed with is Java, a lot of these concepts are very new ...
1
vote
3answers
133 views

Reason to treat internal class variables and functions inside a class as “separate” entities

Note: there is a similar question that addresses my subject: Better style for member variables? ... but that question does not address "dealing with large legacy code base and comprehension of ...
1
vote
1answer
114 views

Use Generic of Specific Function names for similar objects

Is there a school of thought on putting focus on using generic names for functions, opposed to naming functions based on the things they do? Example Say we have a Bill / Price Sheet object, that has ...
5
votes
6answers
590 views

Are closures considered impure functional style?

I have a few related questions on closures: Are closures considered impure in functional programming? It seems one can generally avoid closures by passing values directly to a function. Therefore ...
-1
votes
1answer
163 views

design patterns in Javascript - toggle functionality [duplicate]

My goal is to add some functionality to the bxslider script - in this case, I want to be able to enable/disable a keyboard event handler AFTER a slider is instantiated. I guess this code is self ...
2
votes
1answer
90 views

How to simplify unit testing with higher order functions?

this is not a question on how to write unit tests this is not a question on what to test this is a question on reducing the typing needed overall for a personal testing framework I know you can ...
2
votes
4answers
105 views

To make a class function or not?

I'm wondering about functions used like this: object1->objectMethod1(arg1, arg2); if objectMethod1: - does not change the object state - does not use the object's state at all - is used by the ...
2
votes
3answers
189 views

Using past participle for non-altering functions [closed]

I often have naming problems when I write classes and functions. Generally speaking, I try to follow the following set of rules if I have to deal with a function whose name is a verb: If the ...
1
vote
3answers
242 views

Does designing a method that changes the argument's (if was object) values a good practice?

The premise is using a language (e.g. C#, javascript) which passes an object by reference into a method. Assume there is an object "Player" which has a property "Level" and there is a method takes ...
5
votes
1answer
320 views

Passing a Scala function to a Java 8 method

The following Scala code works and can be passed to a Java method expecting a function. Is there a cleaner way to do this? Here's my first pass: val plusOne = new ...
0
votes
3answers
110 views

Long vs short scripts? Big vs small scripts? [closed]

As a programmer, I have always wondered whether it is preferable to write (a) short modular functions that are each stored in their own script (i.e., file) or (b) long comprehensive scripts that ...
1
vote
6answers
814 views

An alternative to an array of functions?

I'm programming an app (php) which requires a very long list of similar yet different functions, which are being called by a set of keys: $functions = [ "do this" => function() { // ...
2
votes
2answers
377 views

Why function returning by Address can not be a Lvalue?

Why it is not possible to make it LValue if a function return by address (while possible in case of reference)? int* returnByAdress() { int x =20; return &x; } int& ...
0
votes
2answers
2k views

When to use functions vs methods?

So I know that methods are more OOP 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 functions to me? ...
1
vote
3answers
2k views

If statements vs switch cases? in a JavaScript game and if to use a function [duplicate]

I am developing a game in JavaScript where you start with a user input, stored in the variable "controller". The options for the user consists of start to start the game or about to learn about the ...
5
votes
2answers
131 views

Should more than one function be used when they do similar, but not identical things?

For example, if I wanted to generate some HTML based on some input. Which is the preferred way out of: <?php function generate_html($type, $input){ switch($type){ case 'paragraph': ...
6
votes
6answers
578 views

Why do many languages not support named parameters? [closed]

I was just thinking how much easier it would be to read code if, when calling a function, you could write: doFunction(param1=something, param2=somethingElse); I can't think of any drawbacks and it ...
1
vote
1answer
121 views

Helper functions - well defined inputs or well defined outputs?

Problem In Codeigniter (PHP framework), I am trying to create a helper function that will inject an ActiveRecord (for MySQL) command into a chain of existing ActiveRecord calls. The reasoning for ...
1
vote
2answers
223 views

Using Functions for Never-Repeated Code [duplicate]

What are some best practices for using functions to break up large blocks of code into discrete chunks of logic when those functions are only ever going to be used once within the lifetime of a ...
10
votes
5answers
2k 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: ...
33
votes
6answers
2k views

Why is “tight coupling between functions and data” bad?

I found this quote in "The Joy of Clojure" on p. 32, but someone said the same thing to me over dinner last week and I've heard it other places as well: [A] downside to object-oriented programming ...
3
votes
3answers
437 views

Good Procedure or Function Design

This is in reference to the question posted here. As I would judge it, the question there should be closed simply because it seems to ask why the Borland developers made one thing a function and ...
8
votes
2answers
877 views

What is an example of a continuation not implemented as a procedure?

An interesting discussion about the distinction between callbacks and continuations over on SO has prompted this question. By definition, a continuation is an abstract representation of the logic ...
4
votes
2answers
504 views

Should I put utility methods inside a class?

I have been working on a library which contains a large set of functions. For the sake of simplicity, I am going to use just one set as an example. I am not sure which is the better way, in terms of ...
27
votes
4answers
1k views

How do programming languages define functions?

How do programming languages define and save functions/methods? I am creating an interpreted programming language in Ruby, and I am trying to figure out how to implement function declaration. My ...