What is a Function

Updated: January 25, 2022
No Credit Card Required
ClaimYour
FreeTrialClass

Transcript

In the past, many factory workers held jobs in which they had to repeat the same steps over and over. As you can imagine, this could get extremely boring. Nowadays, many of these repetitive jobs are done by robots or machines. Robots can perform the same tasks all day long without moaning or getting bored. This frees people up for more interesting work, like programming the robots.

Computer programming can be a creative and rewarding job. But sometimes when you write program code, it can take a lot of time, as robots need detailed instructions about what to do in programming language. The more complex the task, the more detailed and longer the code is. At times, it can become hard to follow what you wrote. Luckily, just as computers can take on repetitive jobs in the factory, there's something in programming that can take care of repetitive tasks and make life easier for the programmer.

Functions can replace repetitions in code, they can combine many instructions into a single line, or block of code. Functions are useful if there are many parts of the program where we want the computer to do the same thing. For example, making a robot move his fingers. What's great about functions are that they can be stored as part of the program and called to perform tasks only when needed. Imagine for a moment you have a talented pet that you have taught a trick. It repeats it only when given the command. That's what a function does. It carries out the same track over and over whenever you ask it to.

This robot can grab the ice cream cartons really easily because he has fingers like a human's fingers on his hand. It's a piece of programming code that tells him how to move his first finger. Writing the same piece of code four times for each finger could take a lot of time. That's where functions come in. The code block four finger movement is written once and is called on when any of the fingers need to be moved. This way, instead of writing several lines of code for each finger, you only have to write one. And if there happens to be an error in the code, the programmer can easily search for and eliminate the mistake. Instead of going in and changing many lines. The programmer changes the function once and it will affect every line where it is being called. Functions are extremely useful for repeating detailed tasks over and over and also reduce the number of lines needed. This makes coding easier and gives the programmer much more free time.

Loading...