28.2k views
2 votes
There is no hard-and-fast rule as to which is better, but what do you think? What kinds of circumstances would lead you to writing a function versus using a loop?

User Cyonder
by
7.2k points

2 Answers

1 vote

Answer: look below in explanation

Step-by-step explanation:

For a function, it is needed when it needs to perform a task numerous times in different places at the same time with unique parameters while for a loop, it performs a task multiple times until the loop ends. For a function, mainly it is when we want to change the values and when we want to perform a specific operation whenever we call it. It can be also used to be called at different places to perform operations several times. In a loop, you can't complete the operations until loop finishes and values cannot be changed in loop.

User Campsjos
by
8.1k points
3 votes

Answer:

Functions

The piece of code that is used to perform the same operation at multiple places with different parameters.

Loops

The piece of code that is used to perform a task multiple times and continue until the loop ends.

Explanation

Both of these have different kind of circumstances to apply. For example.

  1. Function can perform a specific operation whenever we call. but when we use loop it will perform same task at multiple times at same place.
  2. Functions can be called at different places to perform operations multiple times but once loop called it cannot complete the operations until loop completed.
  3. Each time when we call the function we can change the values but in loop in iteration we cannot change the value of any variable until loop ends.

User Sase
by
7.7k points