Some Function Examples In Computer Programming

Some Computer Programming Function Examples



The purpose of a function is to allow a programmer to create a block of code that can be re-used over and over again. The function is created to perform a certain task. Some functions are also used to return a value to the calling program.


A function is also considered as a procedure in some programming languages. On the other hand, functions are commonly known as any term that is being used in referring to names of code blocks. Take note that it is the C programming language which solely uses the keyword function. Functions accept restrictions, they also return values, and they are maintained on a separate location from the code of primary program. The C language uses main function as the point of entry to certain programs.


Functions can show up in a single or two locations. This is dependent on whether the function is single line or multi-line. Having a single line function means a value is returned after the performances of work (in a single line) while the multi-line function is broadened over different lines.


Perhaps, the most common example of a programming function is a mathematical function. LOG and TAN are examples of mathematical functions. The other two known functions are string functions and the date functions.


Simply defined, a programming function allows you to assign certain values where results can be calculated in a matter of seconds while saving yourself from the task of doing the computations manually.


On the declaration or calling of a function which has two or more parameters, a comma is used to separate the different parameters. One function declaration could look like this:


function print_two_strings($var1, $var2)
{
echo $var1;
echo "\n";
echo $var2;
return NULL;
}


For this function to be called, a value must be assigned to the parameters, hence:


Function call:

print_two_strings(“hi”, “guys”);


The output should look like:

hi
guys



Another good way to have active parameters is the use of PHP’s integral functions such as func get args, func get arg, and func num args functions. These functions are able to calculate arithmetic means of any values that are passed into them and an output is derived. An example:

mean(35, 43, 3);


The output is then:

Mean: 27



A programming function is usually best when it returns some value or information. Some functions do calculations, but it is also a good idea to report any errors that are encountered during a function. To return information from functions, you can use return () statement on the specified function.

An example of script for PHP is the following:

<script language="php"> function add_numbers($var1 = 0, $var2 = 0, $var3 = 0) { $var4 = $var1 + $var2 + $var3; return $var4; } $sum = add_numbers(2,4,6) echo “The result of 2+4+6 is {$sum} ?></script>
The result is:

The result of 2+4+6 is 12.


Take note that {} statement ended the course of the function. If multiple variables are to be returned, a group of variables should be returned, not a single variable: An example:


function maths ($input1, $input2) {
$total = ($input1 + $input2);
$difference = ($input1 - $input2);
$ret = array("tot"=>$total, "diff"=>$difference);
return $ret;
}


There are also ways of accessing functions without having to type a function name or {} syntax. This can be done in two ways: the call_user_func or the call_user_func_array. One complex example is the following:


$one = "One";
$two = "Two";
$three = "Three";
$callback_func = "my_function";
$result = call_user_func_array($callback_func,array($one,$two,$three));
echo $result;



Even though these examples may look like gibberish, they all perform very useful tasks. The key to good functions is to have them perform one task well, and design them so they can be used over and over again in different programs.



Is a career in Computer Programming right for you? Find out by reading Secrets Of Changing To A Computer Career: http://cburleigh.rickstooke.hop.clickbank.net

 

 
Translate Page Into German Translate Page Into French Translate Page Into Italian Translate Page Into Portuguese Translate Page Into Spanish Translate Page Into Japanese Translate Page Into Korean

More Articles

 

 

Search This Site

 

Related Products And FREE Videos





 

More Articles


Computer Programming Language Evolution

... languages is required in this ever-changing world. It is mandatory that they have to keep up with the demands of the current times. Before, computers are used against a black, monochrome background. Right now, computers uses images, colors, and interactive icons. The contrast is very striking that you ... 

Read Full Article  


Computer Programming Macromedia Training

... packages available in the Macromedia family. For instance, if you are interested in developing computer software, and if you are looking for a way to make a dynamic website, then you may need Adobe ColdFusion, which works like the ASP package of Microsoft, or the programming language PHP. If you are hoping ... 

Read Full Article  


Computer Programming For Credit Card

... committed over the internet. The main reason why identity theft is rampant over the internet is because there are not sufficient security measures that both online sellers and buyers follows. It is really the task of credit card computer programmers to make indestructible security programs for their clients. ... 

Read Full Article  


Introduction Computer Programming Career

... programming also includes testing the source code, debugging it to check for flaws and weaknesses and maintaining it to ensure optimum performance. What does a computer programmer do? The main job of a computer programmer is to write the codes that serve as the foundation of software programs. He is also ... 

Read Full Article  


Magazines About Computer Programming

... the MSDN magazine is what you need. Their issues are always rich with source codes and helpful articles. They also have an archive of back issues, should you be interested in them too. 9. Leading Edge Java This magazine could qualify as the practical Java handbooks for Java developers. It contains tutorials, ... 

Read Full Article