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.

Return values from functions

Return values from functions

From the course: Computer Science Principles: Programming

Start my 1-month free trial

Return values from functions

- Functions, sometimes called methods, are tools for programmers to take common actions and to group them to call using the function name. With a function, you can send in values for it to use and perform actions on it. But you often need to get information out of it. Because of variable's scope, any variable that you create inside of it, will be thrown away when the function is finished. So you need to have a way to return a value from the function when it is called. There are three ways to work with a value. The first is to use a literal. A literal is the value itself, without any name or container. It is simply the value. So I could assign a variable with a literal value, like five. The second is to use a variable. That variable has a value, and I can assign that to another variable. The third is with a function or method. You essentially use it like a variable, or literal. But to do this, you need to have it return a value. Like a variable, it will have a type. In this case, the…

Contents