A function (also called a procedure, method, subroutine, or routine) is a portion of code intended to carry out a single, specific task.

learn more… | top users | synonyms

1
vote
2answers
43 views

How can I make an easy to understand subtraction accumulator?

I'm currently following the tutorials over at RubyMonk, and one of the problems I need to solve is to write a subtract function that would meet these conditions: invoking subtract(4, 5) should ...
1
vote
1answer
40 views

Python simple function - evaluating arguments

Think of the numpad number layout. hvd = horizontal, vertical or diagonal coord = 1,2 or 3 if horizontal or vertical and 1 or 2 if diagonal (1 is up, 2 is down)... hello('v', 2) - Asking for the ...
2
votes
0answers
51 views

PHP - faster/better implementation of is_dir function for FTP/FTPS connections?

I'm implementing an is_dir() function to determine if a given item is a directory or a file for FTP/FTPS connections. Currently, I have two methods: one using PHP's FTP wrapper for the is_dir() ...
0
votes
3answers
69 views

Review: Compare two Anagrams words in C

A simple attempt by me to test whether two words are anagrams or not. Here is the code: #include <stdio.h> #include <ctype.h> #define CH_LEN 15 #define N 26 int main(void) { char ...
2
votes
1answer
60 views

Iterative Collatz with memoization

I'm trying to write efficient code for calculating the chain-length of each number. For example, 13 -> 40 -> 20 -> 10 -> 5 -> 16 -> 8 -> 4 -> 2 -> 1. It took 13 iterations ...
0
votes
2answers
98 views

How to name this function?

The sample code is like this: def a_func(parent_node, child_node): parent_node.add(child_node) // check validity return child_node node1 = a_func(pnode, Node(attr_x = "a new node") node2 ...
0
votes
2answers
73 views

PHP PDO Custom class ple

i created a database class from a good tutorial and wanted to put it up here so it would get in some search results. it took me about 2 days to find it. also i added a few custom functions to it.. ...
0
votes
2answers
140 views

A better way of writing a PHP function

As the title says, I'm looking for a better way of writing the following PHP function as it's a very very long one. It's part of a bigger class and I'm trying to keep the amount of code as little as ...
1
vote
3answers
47 views

Function calls as parameters for other functions

I am in two minds about function calls as parameters for other functions. The doctrine that readability is king makes me want to write like this: br = mechanize.Browser() raw_html = br.open(__URL) ...
1
vote
2answers
223 views

What design pattern to use on this case

I have this code that converts audio to different file formats. import java.io.File; import it.sauronsoftware.jave.AudioAttributes; import it.sauronsoftware.jave.Encoder; import ...
1
vote
2answers
49 views

How to improve decode function

I have the next function: var decode = function(str, result) { var regex = /\d+/, number = regex.exec(str); if (number === null) { return str; } var start = ...
4
votes
3answers
176 views

Calling functions (or invoking methods) in constructor

I had written something like the following, public class ClassName { //private instance variables public ClassName() { //initialize instance variables this.someFunction(); } public void ...
1
vote
1answer
95 views

How can I improve this C style va_args replacement?

I inherited a lot of C code with many ellipsis (variadic) function. So I have a lots of API with the following signature. void getXY(int foo, ...) // many parameters and this is used in this way ...
5
votes
2answers
131 views

Can I use this function to store passwords?

Just wrote this function, wondering if it's secure enough to store passwords in a database: public static function hash($salt, $str) { $salt = md5($salt); $salt = ...
2
votes
1answer
76 views

Single function to read one, read all, or set

I have the below function, it is designed so I can call: $object->temp('one', 1); //add the key `one` to the temp array with value 1 $object->temp('two', 2); //add the key `two` to the ...
0
votes
2answers
94 views

Refactoring functions that generate string with random letters, numbers or mixed chars?

The bellow functions are used to generate a string with random characters, and they have a parameter to specify the string length. The string can be composed of letters, numbers or a conjunction of ...
2
votes
2answers
46 views

Naming of overloaded methods - singular or plural? [closed]

This question is about your opinions and/or existing conventions. Let's say I have an object Basket that store various items in some private property (Basket._items). Now, I want to have a couple of ...
2
votes
1answer
159 views

Arduino based SNES controller wireless transmitter/reciever, code questions

I am building a transmitter and reciever pair for two SNES controllers, as I don't like using long extension cords to get the controllers to reach the couch. I'm using atmega328p's for the avr's, ...
2
votes
2answers
257 views

Best practice for converting minutes and seconds in seconds

I have 2 input fields to input the duration of a CD song. The first input is for minutes and the second is for seconds When submitting the form, I must insert in the db the duration either in seconds ...
2
votes
1answer
65 views

Is it safe to cast a pointer to non-void function into a pointer to void function?

I thought it was a good idea to use this in my C++ projects: class CRAIICall { public: typedef void (WINAPI * FnType)(HANDLE); CRAIICall(HANDLE h, FnType fun) : m_h(h), m_fun(fun) ...
1
vote
1answer
69 views

How to improve a file access permission/deny function?

I've quickly made a function to deny or allow access to files that are only used thru Ajax Calls. Key Generator /** * Generate a key for file access permission * * Function that returns a key to ...
3
votes
1answer
82 views

How to improve function to detect the mime type using an agnostic method?

This function is used to get the mime type of any given file. The intended purpose was to have it working within any of our servers that vary from the latest PHP version down to PHP 4. /** * Get the ...
2
votes
1answer
101 views

How can this function to get a remote file improve?

I've prepared a function using cURL to get a remote file to the local storage: /** * cURL - Get Remove File * * Function using CURL the get a remote file to the local storage. * * @param str ...
2
votes
2answers
60 views

Is this function properly structured, leading to a secure row insertion?

I have the following function to insert a new row into a table: /** * Insert row into the designated table * * Function to add a new row into the designated table * * @param str $table ...
2
votes
1answer
70 views

How to optimize function that checks if a directory is empty when having over a million files inside?

I have the following function to confirm if a directory contains files: /** * Check if the directory is empty * * Function to ascertain if the specified directory contains files. * Comparing to ...
3
votes
1answer
57 views

How does this function look?

What do you think of my function. It is used to change a cyrillic word with a stress marker into the same cyrillic word but with the stress marker seperately represented as a number. As I want to ...
4
votes
3answers
230 views

C Split function review

I started learning C a week ago, and here is my implementation for the split function: char** my_split(const char* str, char delim, int* size) { int index = 0, start; char** results = NULL; ...
1
vote
2answers
82 views

How to improve this PHP function to confirm the presence/create directories?

While preparing an automated process to manipulate files, I've made the following function to check for the base directory and sub-directories presence, as to allow or deny the remaining code to be ...
1
vote
1answer
171 views

Better jQuery function argument with if-statement?

function check_itv(key) { if (key.length){ if(key=="left"){ left_itv = setInterval(left,100); check_left= false; check_right=true; check_up=true; ...
2
votes
5answers
143 views

Buy and Sell game project

it's a buy and sell game, and i revised it, it's still unfinished though, i would appreciate it if someone reviewed again for me. #include<stdio.h> #include<conio.h> ...
-3
votes
1answer
147 views

how can i improve/reduce this code? [closed]

any type of suggestion/correction will do, and i can't seem to get my functions to work or my conditional statements. int buyitem(int x, int y)//* function used to buy an item at any store*// { ...
0
votes
2answers
74 views

May be a memory leak somewhere or allocated memory not freed? [closed]

Here is the entire program, please help me, I've tried everything to find out what exactly is going with the memory. The problem is everything runs perfectly, but there are some extra characters ...
3
votes
2answers
154 views

How to improve these Haskell functions?

This is the algorithm for calculating sun rise/set time at various places on Earth. I took it as an example of multiple functions inside of one top function. (if someone figure out better title, ...
2
votes
1answer
196 views

Refactoring this state machine (upvoting/downvoting)

I have a list of resources which can be voted on. There are 3 voting states: upvoted, downvoted and no vote. In a fashion identical to stackexchange voting, it follows these rules: If you upvote a ...
4
votes
3answers
417 views

Javascript function and global variables

I have a getValue function which just grabs some numbers from an HTML page – How can I access those variables in later functions without passing them down the entire chain of functions? As they sit ...
2
votes
1answer
113 views

Do I need to create more functions? And also, is this code difficult to unit test?

I'm new to production code. And also I'm learning how to test code, I'll use the unittest module that comes with python to do that. One more question, how can I make this code more safe? """ Sync ...
2
votes
1answer
258 views

Refactoring Javascript into pure functions to make code more readable and maintainable

Note: I am very new to Javascript. The code I provided is long but not complicated. The code below works and runs fine. But I would like to seperate it out into logical 'pure' functions to make ...
3
votes
1answer
2k views

How can I shorten my jQuery for Show/Hide Function with Multiple Div IDs?

I've created a sliding panel menu that works (JSFiddle: http://jsfiddle.net/stephmoreland/3BT7t/), but I can't wrap my head around shortening the jQuery for it. Right now, when you click one of the ...
1
vote
4answers
299 views

Reading tokenized numbers from a file

int *getUserInput(FILE *fpin, int *elementCount) { int *userInput; // Load file into buffer int length = fileLength(fpin); char *buffer = (char *) malloc(length * sizeof(char)); int written ...
1
vote
2answers
683 views

calling functions over and over?! improvements needed

Not some much a problem, I am curious to know if my code can be cleaned up here a little. One annoyance is im calling a function three ( 3 ) times. I would like to know how this could be improved, ie: ...
4
votes
1answer
184 views

Is this code efficient?

I was wondering if this was a decent way of handling this function. Please take a look. // Use Less Mixins from Theme Options to adjust Stylesheets before Parsing Less to CSS add_filter( 'less_vars', ...
3
votes
2answers
98 views

Please review my function code for adherence to C standards and whatever else could be improved to it

I am relatively new to C and would like some feedback on a function that I have written, if it adheres to C standards or if there are some other things which I could have done better/differently. The ...
3
votes
2answers
182 views

Cross-post from SO- Palindrome finding function

I didn't know this site existed before now... awesome! I just made a thread here: http://stackoverflow.com/questions/8511620/c-palindrome-finder-optimization #include <iostream> #include ...
0
votes
1answer
240 views

Is my login function secure ? how to improve it?

Using cakePHP, I would love to have your point of view about this large source code, i am not sure if its enought secure. sha1() will be removed with another script. I found this large script can be ...
4
votes
3answers
3k views

Min / Max function of 1D array in C / C++

Due to software constraints, I cannot use the standard libraries, math.h,algorithm, templates, inline, or boost. I am also using standard C (ISO C99) such that array is not a reserved keyword like it ...
10
votes
3answers
498 views

Fastest Parameter Passing in delphi 7?

I have a function that accepts a big amount of data as a parameter (sometimes 1 megabyte) I was thinking which would be the fastest way to pass that data, dunno if I got this right, but here is what I ...
2
votes
1answer
373 views

Passing anonymous functions and getting variables from an outer scope in php

I'm doing something quite strange. I'm trying to create a tiny system for creating templates inline in php, so that I can refactor by creating multiple templates in the original script, and ...
4
votes
1answer
223 views

Python, WeakBoundMethod

I've written this small class in Pyhon that wraps bound methods but does not prevent the deletion of self. Do you have any though on my code? Do you think I handle errors appropriately? Is it missing ...
7
votes
6answers
456 views

What looks better in java

I wonder what is better code practice or just what looks better in Java. Version 1: protected Boolean doSomething(int amount) { if (amount < 1) return false; return ...
2
votes
2answers
87 views

Where to check?

Requirement: If the bookshop manager field changes from A to B, I need to email the person B saying that the new Bookshop has been allotted to him and he has to do certain activities. Here is my ...

1 2