From the course: Computer Science Principles: Programming

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Use functions to repeat actions

Use functions to repeat actions

From the course: Computer Science Principles: Programming

Start my 1-month free trial

Use functions to repeat actions

- One of the reasons for the invention of computers was to automate tasks that have to be repeated multiple times. So instead of repeating a group of actions that need to take place, you can simply say do these steps and they take place. The way to do this is with functions. Functions are groups of actions that the programmer defines and gives a name. While variables are named values, functions are named actions. Let's look at an example. If I have three actions that regularly take place. It might become annoying to constantly code these commands in my app. For instance, I might need to perform some math to calculate a sales total. We can then take these actions and group them together as a function and give it a name. Now every time we need to get that information we can simply call that function by its name. After the code starts running, when it encounters the function name the program jumps to that function, enters it and runs the code that is inside. At the end, it then returns…

Contents