Programming Functions and why are they so special

796 views

I’m still trying to wrap my ahead around this concept.

I’ve done basic maths, so I know that f(x) = x is like a function, but I can’t find an intuitive way of explaining why some functions don’t have to return values. In addition, what separates functions from just lines of code?

In: Technology

12 Answers

Anonymous 0 Comments

a function is just shorthand for lines of code.

At the simplest level you’re basically you’re telling the computer “every time someone tries to execute a function, just replace it with all the lines of code in the definition of the function”

This allows code to be much more readable and smaller. If i have 50 lines of code that define, lets say a hotel scheduling software’s function “BookGuest(LastName, first name, room)”, and i use that 40 times, i can either have the 2000 lines of code from rewriting those 50 lines 40 different times, or i can have 90 lines of code, from the definition of 50 lines and then just one line for each of the 40 uses.

It also allows change super quickly, easily, and with less mistakes. Suppose i get a new Sales system, and oh no, i have to change 2 of the lines in the code of booking a guest. If i did it as a function, i just change 2 lines in the function, and them bam every time in the code it sees the functions name, it calls up the new and improved code.

If i instead typed out those 50 lines 40 times, i now have to go to all 40 uses and make sure i change those exact 2 lines the same in every spot, and make sure i hit all 40 and not 39 of the cases.

You are viewing 1 out of 12 answers, click here to view all answers.