In: Computer Science
Response in Coral Code and code example in Coral Code please!!!! Respond to the following in a minimum of 175 words:
It is important to program your code efficiently. Efficient code does not include duplicate code that performs the same procedure. Functions are helpful in making code modular. How can functions reduce the amount of written code and make code easier to read/follow?
Provide a code example that supports your comments.
The answer to the above problem is as follows:
When we talk about function, its a group of statements that perform a specific task and we can call this from the code itself. This is what a function does in any language or if we are dealiing with Coral Code then also, it serves the same purpose. When we define a function, we can also pass in parameters to the function which are essentially values that can be used inside the function, and this function can also return values from it, to the place where its called.
All these features of a function, allows the coder/programmer the ability to refrain from repeating the same code at multiple places, as the group of code that is being repeated can be coded inside a function, and this function can be called at all required places. As such the redundancy and duplicacy of code is removed from the programs. At the same time, it allows us to make the program modular, by grouping the statement that perform a specific task like say, compute factorial, compute fibonacci term, search element, add record or other such task into one group, thereby dividing the program into modules.
Below is an example of function that computes sum of two numbers. Now if we want to find out sum of multiple pair of two values like (5,6) ; (2,3); (11,15) etc . Then without functions we will have to write separate code for each pair. But with functions, we can simply call the function sumOfTwoNumber() and pass the two values as parameters and store the returned result.
Example of function in CORAL code-
Function sumOfTwoNumbers(float num1, float num2) returns float
sum
sum = num1 + num2
Function Main() returns nothing
float resultSum
resultSum = sumOfTwoNumbers(5, 6)
Put resultSum to output
Image of CORAL Code-
The output of the above code will be 11
If this answer helps, please give an up vote and feel free to comment for any query.