Looking Adeptly at Programming Function Examples

by | Programming

Before looking at the different examples of programming functions, it is best to understand the purpose and definition of the function. A function is the means by which someone who uses a program can execute a code block which has two purposes: to finish a certain task and to return values. Although functions are expected to return a certain value, it is not always that values are returned.

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 the primary program. The C language uses the 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 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, the use of the comma is needed to separate the different parameters. One function declaration could resemble this:

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

For these functions to be called, a value must be assigned to the parameters, hence:

Function call:

Print two strings (“hi”, “guys”);

The output should appear as:

hi
guys

One other 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 placed onto them and 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. Functions do calculations, indeed, but it is also useful in indicating any errors that are encountered in any function. To return information from functions, you can use return () statement on the specified function.

An example of a script for PHP is the following:

<?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}
?>

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;

These equations may show as a bunch of gibberish letters and numbers but these symbols actually account to make a certain task easier. And that, for us, is the most important thing.

Computer Programming and Its Rich History

If it's the history of programming that has to be retold, then it is safe to begin an account with the difference engine of Charles Babbage way back in 1822. Even from the time when computers were so simple, they still needed to have instructions so that they will be...

Why Hardware Development Could Mean the Future of Computer Programming

Computer programming is also known as a software program or sometimes it is simply called "program". Whatever name it's called, its purpose is to provide instructions to a computer machine. If there is one thing that a computer needs to be able to function (aside from...

The Future of Computer Programming

When it comes to the world of computer programming, it would be safe to say that the future is bright. And why is that so? Gone are the days when only the rich and powerful have the tools to educate themselves. Nowadays, a single household possesses at least one...

Computer Programming Functions: Get to Know What They Are

When it comes to computer programming vocabulary, there are so many terminologies that people who do not have enough know-how tend to become lost and confused. One term could be translated into so many different names in different languages. For example, the following...

The Need For Computer Programming Language Evolution

The digital world constantly changes. New technologies are introduced and new developments in the industry are being made known to the public. There will always be changes in technology. And technology will constantly improve to help create a better world. And one of...

Computer Programming Fundamentals You Should Know

Quick! Can you tell me how to prepare a bowl of cereals with milk? Too simple, right? You can probably give me a series of no-brainer instructions that I can perform in less than a minute. Now try telling a computer to do just that. What began as a simple task has now...
Get In Touch

Ready to Chat?