- [Instructor] A function may return a value back to its caller with a return statement. Here I have a working copy of func.cpp from chapter five of the exercise files. In this example, the return type of the function is set to void, so this means that the function is not returning values. So we're going to change that and have it return int and we'll take an integer also as an argument and we will return i times two. So now, if I say int x equals func and pass it a 42 I can expect that x to be 84, right? So I'm going to print f x is percent d and x and when I build and run, you'll notice that my result says x is 84, as expected.
So now func returns an int value, just like passing a value to a function, return values are copied to the stack and returned on the stack. This means that you cannot return anything large. If you need to return a large object, you can do so with a reference. Let's take a look at how you would do that. So I'm going to add an include here for the string library and we're going to return a string. So I'm going to say const string ampersand, so you're returning a string reference.
I won't need that parameter anymore and instead of this puts, I'm going to say const static s or string s equals and then I'm going to simply return s and that'll return the referenced s because I have this ampersand here in the return type, so it's returning a reference. And now, instead of all of this, I'm going to say const string s equals func and print f func returns percent s and s.c_str to give us a c string for our s value.
Now when I build and run, we get func returns. This is func, which is exactly what we expected. So a couple of notes here. You'll notice that our storage here is const static and, of course, static is because automatic variables are stored on the stack and we don't want to store a big object on the stack. And while, obviously, this is not a huge object, it's probably enough that we don't really want to store it on the stack. And const because const is just always a good idea, especially with static storage, especially when you're passing around references to that static storage.
But it's generally a good idea for anything that you're not going to need to change in the future. I tend to default to using const more often than not, and then if I find that I need to actually change something, I can always remove it. And, of course, we're returning a const reference to the string and we're taking our variable that's getting initialized by the return value for func, that is also a const string and this is being initialized, of course, by that reference. And so, that'll be copied into this new variable.
If I wanted to, I could use an ampersand there and now, we're not even using a temporary variable here and we're actually only using this const static storage that's passed out from the function and I can build and run. Of course, we get exactly the same result. Returning a value from a function is much like passing values to a function and that happens on the stack. And you'll want to keep all of those caveats in mind as you do so.
Author
Released
12/19/2018- Setting up Xcode and Visual Studio
- Statements and expressions
- Variables
- Primitive arrays and strings
- Conditionals
- Loops
- Data types
- Operators
- Functions
- Classes and objects
- Templates
- Standard Library and Standard Template Library
Skill Level Intermediate
Duration
Views
Related Courses
-
Learning C++ Pointers
with Peggy Fisher53m 55s Intermediate -
Learning C++
with Erin Colvin2h 26m Beginner
-
Introduction
-
About this course1m 2s
-
Using the exercise files1m 10s
-
What is C++?4m 7s
-
-
1. Installation
-
2. Basic Syntax
-
Anatomy of a C++ program8m 43s
-
Statements and expressions3m 23s
-
Identifiers2m 23s
-
Defining variables3m 6s
-
Pointers4m 29s
-
References3m 27s
-
Primitive arrays3m 11s
-
Primitive strings3m 58s
-
Conditionals5m 6s
-
The branching conditional2m 55s
-
Iterating with for4m 42s
-
Range-based for loop3m 41s
-
Using stdout6m 58s
-
3. Data Types
-
Overview of data types3m 9s
-
Integer types2m 26s
-
Integer sizes6m 7s
-
Fixed-size integers4m 7s
-
Floating point types5m 41s
-
Characters and strings3m 53s
-
Character-escape sequences3m 40s
-
Qualifiers6m 1s
-
References5m 43s
-
Structured data2m 23s
-
Bit fields2m 45s
-
Enumerations3m 55s
-
Unions2m 47s
-
Defining types with typedef2m 22s
-
The void type2m 23s
-
The auto type3m 24s
-
-
4. Operators
-
Common operators2m 54s
-
Logical operators2m 17s
-
Bitwise operators3m 29s
-
Ternary conditional operator1m 47s
-
Dynamic memory operators7m 14s
-
Type cast3m 14s
-
Using sizeof4m
-
Using typeid2m 44s
-
Operator precedence2m 13s
-
-
5. Functions
-
Overview of C++ functions3m 32s
-
Creating a function3m 20s
-
Passing values to a function6m 13s
-
Using function pointers6m 22s
-
-
6. Classes and Objects
-
Defining a class3m 32s
-
Data members3m 56s
-
Function members6m 44s
-
Constructors and destructors6m 10s
-
Overloading operators6m 34s
-
7. Templates
-
Understanding templates2m 48s
-
Template functions4m 42s
-
Template classes3m 42s
-
-
8. Standard Library
-
File I/O4m 45s
-
Binary files4m 22s
-
File management4m 11s
-
Unformatted character I/O5m 57s
-
Formatted character I/O3m 34s
-
String functions5m 2s
-
Handling system errors2m 33s
-
9. Standard Template Library (STL)
-
Overview of the STL2m 25s
-
Vectors3m 56s
-
Strings3m 33s
-
I/O streams8m 3s
-
Handling exceptions4m 22s
-
-
Conclusion
-
Thank you1m 30s
-
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.
CancelTake notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.
Share this video
Embed this video
Video: Returning values from a function