Answer:
- Make code more reusable and modular
- Abstract and hide complexity
Step-by-step explanation:
A function is a block of code that defines some process that will get repeated whenever that function is called.
For example (the following is purely pseudo-code and is not written in any specific language):
square(x):
return x * x
The function square(x) takes an input x and returns that number squared.
Now this function can be called in another block of code or even within another function.
For example:
sum_of_squares(x, y):
return square(x) + square(y)
As you can see, calling the function square(x), within the function sum_of_squares(x, y), helped make the code shorter.
This achieved two things:
- Make the code reusable and modular: we were able to reuse the the function square(x) instead of writing it twice
- Abstract and hide complexity: By calling the function, we are able to use it without knowing how it works. This hides complexity and makes code easier to use. For instance, consider a function that calculates the cross-product (don't worry about what this is if you don't know yet, just keep in mind that it is a complicated mathematical process) of two vectors v and u, instead of manually coding the complicated process every time we need to calculate the cross-product, we can just define and call the function cross_product(v, u), this just made the code a lot easier to understand