answersLogoWhite

0

What is a function and its use?

Updated: 8/20/2019
User Avatar

Wiki User

8y ago

Best Answer

Functions allows us to compartmentalise code to reduce code complexity and code duplication.

As an example, let us suppose we wish to calculate the sum of all digits in a value. Given a value, x, we must isolate the digits of x and sum them. To achieve that we use the following code:

int sumx = 0; // the sum of all digits in x

while (x!=0) { sumx = sumx + (x % 10);

x = x / 10;

}

Now let us suppose we have another value, y, and we wish to perform the same operation.

int sumy = 0; // the sum of all digits in y

while (y!=0) {

sumy = sumy + (y % 10);

y = y / 10;

}

Each time we wish to perform this operation upon a value we need to write the same code, changing the names of the variables to suit. Writing code in this way is tedious and prone to error. It also makes code difficult to read, to the extent we include user-comment to better explain its purpose.

To simplify our code, we can compartmentalise this code as a function:

int sum_of_digits (int num) {

int sum = 0;

while (num!=0) {

sum = sum + (y % 10);

y = y / 10;

}

return sum;

}

Now our original code becomes:

int sumx = sum_of_digits (x);

int sumy = sum_of_digits (y);

Note that the code is self-explanatory, so the comments in the original code become superfluous. Moreover, anyone reading this code is not distracted by the low-level implementation details of the actual operation. A reader doesn't care how a result is obtained so long as the result can be relied upon. However, if they really do need to check the code to ensure the operation is reliable, that code can be found in just one place, in the function itself.

This is clearly a trivial example, however most functions are trivial. So trivial, in fact, that a compiler can optimise away the function call entirely, expanding the code to reproduce the original version with the duplicated code. By allowing the compiler to generate the duplicate code for us, we greatly reduce the chances of human-error (provided the function operates as intended, of course).

Given that functions allow us to compartmentalise low-level code and hide implementation details, we can use those low-level functions as the building blocks for higher-level functions. High-level functions essentially become a series of lower-level function calls. Thus functions can be seen as being a programming tool, making use of the language's built-in vocabulary to extend the language, increasing the vocabulary to perform highly-complex operations with a handful of simple function calls.

User Avatar

Wiki User

8y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is a function and its use?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

How do you get the current time in Microsoft Excel?

You use the NOW function, like this:=NOW()You use the NOW function, like this:=NOW()You use the NOW function, like this:=NOW()You use the NOW function, like this:=NOW()You use the NOW function, like this:=NOW()You use the NOW function, like this:=NOW()You use the NOW function, like this:=NOW()You use the NOW function, like this:=NOW()You use the NOW function, like this:=NOW()You use the NOW function, like this:=NOW()You use the NOW function, like this:=NOW()


What function tests a specific condition in Excel?

The IF function is the main function to do it and you can also use other logical functions, like the AND function, the OR function or the NOT function.


Which feature do you use to enter a function?

You can use the Insert Function feature.


Can you use Sum Function in school?

If you use Excel in school, then, yes, you can use the SUM function.


What word can you use instead of function?

function


What is the formula used in Excel to calculate interest?

The most common function to do this is IPMT and you can also use ISPMT.The most common function to do this is IPMT and you can also use ISPMT.The most common function to do this is IPMT and you can also use ISPMT.The most common function to do this is IPMT and you can also use ISPMT.The most common function to do this is IPMT and you can also use ISPMT.The most common function to do this is IPMT and you can also use ISPMT.The most common function to do this is IPMT and you can also use ISPMT.The most common function to do this is IPMT and you can also use ISPMT.The most common function to do this is IPMT and you can also use ISPMT.The most common function to do this is IPMT and you can also use ISPMT.The most common function to do this is IPMT and you can also use ISPMT.


How do you use this function in c programme?

I don't use that function in C programme.


To add numeric enteries quick in excel use the function?

Use the Sum function.


Which Excel function would you use when you want to determine a monthly loan payment?

The PMT function.


Can you use main function as a recursive function in C?

Yes


What is an anaerobic function?

An anaerobic function is one that does not use oxygen.


What are four ways to add a function to a formula?

Type the formula directly in the cell or the formula. Use formula AutoComplete. Select a function from the Function Library group, or click the Insert Function button to select a function from the Insert Function dialog. Use AutoSum