Useful functions in all languages ​​- Programming - HWzone Forums
Skip to content
  • Create an account
  • About Us

    Hello Guest!

    Please note - in order to participate in our community, comment and open new discussions, you must join as a registered member.

    Our members enjoy many advantages, including the ability to participate in discussions, enjoy raffles and promotions for members of the site, and receive our weekly content directly by email.

    Do not like being harassed by email? You can register for the site but do not submit your registration to the weekly email updates.

Useful functions in all languages


Recommended Posts

Hello everyone.

In this discussion, anyone interested can register useful functions that can be used by any programmer in any language.

The goal is to get the programmers to find what they want without going around and looking for what they want between the chairs.

Anyone who has a function that in his opinion can serve the entire population is invited to publish his response in the current discussion in the following format:

The language in which the function was written

A few words about the function (what it gets, returns, etc.)

And of course the function code within a field of code.

Enter your code here

And of course the most important without spam and correspondence that do not contain the above rules.The discussion should serve as a kind of "textbook."


Sample language: ASP

Description: Add / delete data into MySQL database

Connection to database:

Set Conn = Server.CreateObject ("ADODB.Connection")   
'Create a connection object to the database

  Connstr = "Driver = {MySQL ODBC 3.51 Driver}; SERVER = ; DATABASE = ; UID = ; PWD = ; "
'Connection String' that connects us to the database and uses the appropriate driver for MySQL ODBC 3.51

Conn.Open Connstr
'Open the connection to the database

Add data to the database:

First, you must take the data and the form into a variable (in the Post or Get view) according to your opinion.

Suppose that the variables within them have values ​​strText1 and strText2 I would like to insert them into a table named tbl_MyTable for the Text1 and Text2 columns respectively.

  SQL = "INSERT INTO` tbl_MyTable` (`Text1`,` Text2`) VALUES ('"& strText1 &"', '"& strText2 &"'); "
  Conn.Execute SQL

Delete a record from the database:

It is strongly recommended that you add a column named ID to the database that we use as an index that contains a unique numerical value that no longer exists in the same column as an identity card.

So we can easily handle our data.

Conn.Execute SQL


Link to content
Share on other sites

  • תגובות 43
  • Opens on
  • Last comment

It helped me .. = P do not know how much this contributes in general ... = \

Programming language: VB

Function description: Makes a complete Laski string, with no spaces.

Private Function StrToAsc (Str As String) As String

For i = 1 To Len (Str)

StrToAsc = StrToAsc & Asc (Mid (Str, i, 1))

Next i

End Function

Programming language: VB

Description: Combining strings within an array (that is, connecting all the strings in a particular array to a single string).

Private Function CombStrArry (Arry () As String) As String

For i = 0 To (UBound (Arry))

CombStrArry = CombStrArry & "" Arry (i)

Next i

End Function

Programming language: VB

Description: (This is a procedure, but not important) takes a label (LabelToCheck), and checks whether it is longer than the character limit given (LenToChange). If so, then the procedure will replace the entire string with the first X characters (LenToChange), and add three points after them.

Private Sub ShortLabel (LabelToCheck As Label, LenToChange As Integer)

If (Len (LabelToCheck.Caption))> (LenToChange) Then

LabelToCheck = Mid (LabelToCheck.Caption, 1, LenToChange) & "..."

LabelToCheck.ToolTipText = LabelToCheck.Caption


LabelToCheck = LabelToCheck.Caption

End If

End Sub

Take into account that I've done everything long ago, and I did not have the strength now to check what works and how exactly ...

Link to content
Share on other sites

Language: PHP

Description: Integrity check email address (correct address: name@server.suffix)

function validEmail ($ email) {
if (eregi("^[a-z0-9]+([-_\.]?[a-z0-9])+@[a-z0-9]+([-_\.]?[a-z0-9])+\.[a-z]{2,4}", $email)){
return TRUE;
} Else {
return FALSE;

Language: PHP

Description: Random letter. The function returns a random signal. Custom requirements can be customized according to any desired layout (lowercase / upper case, numbers, characters ...). The example is simply to describe the use of random selection.

function random_letter ()
$ letters = array (1 => "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k "," l "," m ",
n, o, p, q, r, s, t, u, v, w, x, y, z ");
$ index = Key ($ letters);
$ element = Current ($ letters);
$ index = rand (1,26);
$ random_letter = $ letters [$ index];
return $ random_letter;

Note: If you insert the code into a tag, you will specify a predefined description of the language (in my case, adding ), The forum will automatically mark the code with appropriate colors ...

Link to content
Share on other sites

Use strings in C.

pay attention! It is important to initialize your string to NULL, and if you do not, your program will fly!

int setstr (char * strtar, const char * strsou)
if (strtar! = NULL)
free (strtar);
strtar = (char *) malloc (sizeof (char) * (strlen (strsou) + 1));
strcpy (strtar, strsou);
1 return;

int main ()
char * str = NULL, * str2 = NULL;
setstr (str, "hello world");
setstr (str2, "wassup?");
setstr (str, str2);
printf ("% s,% s", str, str2);

From now on I will try to chain my messages.

Link to content
Share on other sites

free static place, bad idea : nono:

Your code is incorrect and will just cause problems

It was correct to initialize the pointers in NULL rather than in a static string.

Besides strdup is not ANSI-C

Link to content
Share on other sites

  • 5 months later ...

For all those who program in Pascal. I made a library with three functions to relax. The functions are:

1. Strong - for those who do not know Pascal allows to do only stronger than the second degree, the function I added allows to make any possession you want.

Use the function as follows:

Pow (Base, Exp);

Base = Holding Base

Exp = holding estimator.

Say I want to insert a value of 4 into a variable 3. I will list:

a: = Pow (4,3);

The function returns a real value.

2. Random Number - Making a random number in Pascal is not something complicated but nevertheless I did a function that made it easier. The function accepts a range of numbers and gives a random integer number.

Use the function as follows:

Rnd (min, max);

min = initial number.

final number.

Make sure that the first number is smaller than the second one. If that is not the case, then turn them over and it will be all right.

Say I want a random number between 5 and 20 I will write:

Rnd (-5,20);

3. Number Lookup - This function is used to check if a number is negative / positive / zero. The function returns a value by type.

Negative number: -1 (minus one)

Positive number: 1 (one)

Reset: 0 (zero)

Use the function as follows:

Sign (num);

num = number or variable you want to check. The value returned is an integer.

Installing the library (unit)

Copy the MATH.PAS file to the C: \ TP \ Units folder.

Using the library

To use the functions in the library you need to write at the beginning of the program:

Uses Math;

This is all ... I hope you use it for the best, do not take down the credit.

If someone has ideas for more functions to tell me.

Download Address:




Link to content
Share on other sites

  • 3 weeks later ...

PhoenixBoy, for years I have not touched Pascal but as far as I remember there is a BUILD-IN function that knows how to do RANDOM and it does exactly what you described (I did not see your code ...) ???

Link to content
Share on other sites

PhoenixBoy, for years I have not touched Pascal but as far as I remember there is a BUILD-IN function that knows how to do RANDOM and it does exactly what you described (I did not see your code ...) ???

The truth is that the Pascal Pendulum gives numbers between 0 and the number written inside the parentheses minus 1 ...

Not that there is such a huge difference between this and a random number in a certain range as it did (actually it's a code of less than three lines) but nevertheless it is not exactly the same ...

Link to content
Share on other sites

  • 5 months later ...


This discussion has been archived and new comments can not be added.

  • Create new ...

At the top of the news:

new on the site