Functions
Writing large programs effectively requires decomposing the code into several independent modules. This makes the program easier to maintain and edit. This is done by breaking the problem into small, manageable pieces. A function is a portion of code within a larger program that performs a specific task and is relatively independent of the remaining code. This helps in decomposing the code into smaller independent modules. The task performed by a function can be summarized as taking a set of input variables and returning a value after performing computations with these values. The values of the input variables may also be updated during the computation. Since functions are written independently of the main code, the same function can be called from the main program with different input variables. This allows reuse of the code and hence shortens the overall program.
Aim of the experiment: To understand how functions help break down a large program into smaller, reusable, and independent modules, making code easier to maintain, debug, and extend.
Example: Suppose you are making a program that calculates sales tax and returns the total payable amount. The function would ask for a subtotal (s_total) and the tax percentage (p) as arguments, then take that s_total and multiply it by p/100 to calculate the sales tax (s_tax). After this, the function would calculate the total payable amount by adding sales tax (s_tax) and subtotal (s_total), and return it to the main program. This function can be called many times from the main program for different customers by providing their subtotal and sales tax to be applied.