Function: A function is basically a process which takes input and provides an output, for example, Printing a page from a printer. The parameters are the text, images, and anything else on the page to send to the printer. The printer then uses this information accordingly to process the data and returns the output, which in this case, is paper.
Subroutine: A subroutine is a process that can take input but does not provide an output. An example of this is doing a task for someone without reaping any benefits or rewards, the task still gets done but nothing is given to you (except maybe the tools to do the job, aka the parameters). So you could be cleaning up the garden, and a parameter might be the tool you use, one day you might use the lawn mower, whilst another day you might use cutting tools to clean up bushes. This is probably a bad example but it is quite hard to explain subroutines in 'real world' terms.
In programming subroutines are usually used to keep code easy to read and repeat code with slightly different scenarios (this is where parameters come in)
No comments:
Post a Comment