Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Return Statement in Functions | Introduction to Functions
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
C++ Introduction

bookReturn Statement in Functions

The return statement terminates the execution of a function and returns a value of a predefined type.

function.h

function.h

copy
12345
int func() // int - predefined { int variable = 10; return variable; // variable = 10 }

If the type is specified incorrectly, the function will behave unpredictably.

main.cpp

main.cpp

copy
12345678910111213
#include <iostream> unsigned short func() { return -10; } // The unsigned short data type has no negative values. int main() { std::cout << func() << std::endl; }

That is, before creating a function, the type of data that it returns must be specified. Also, in C++, there are special void functions. Functions of this data type are allowed to return nothing:

first_example.cpp

first_example.cpp

second_example.cpp

second_example.cpp

copy
123456789101112
#include <iostream> void voidFunction() { std::cout << "It's void function!" << std::endl; // Function without return } int main() { voidFunction(); }

There can be multiple returns inside functions, and each one will only fire under certain conditions.

main.cpp

main.cpp

copy
1234567891011121314151617
#include <iostream> int func() { int a = 50; int b = 6; if (a > b) // If `a > b`, func will `return a` return a; else // Otherwise func will `return b` return b; } int main() { std::cout << func() << std::endl; // Func calling }

If there are two returns, the second return function will be ignored:

main.cpp

main.cpp

copy
123456789101112131415
#include <iostream> int func() { int a = 50; // Declare variable a int b = 6; // Declare variable b return a; // Function stops here, b is never returned return b; // Unreachable } int main() { std::cout << func() << std::endl; // Call func and print result }
question mark

What happens in when a return statement is executed inside a function?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 5. ChapterΒ 3

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

bookReturn Statement in Functions

Swipe to show menu

The return statement terminates the execution of a function and returns a value of a predefined type.

function.h

function.h

copy
12345
int func() // int - predefined { int variable = 10; return variable; // variable = 10 }

If the type is specified incorrectly, the function will behave unpredictably.

main.cpp

main.cpp

copy
12345678910111213
#include <iostream> unsigned short func() { return -10; } // The unsigned short data type has no negative values. int main() { std::cout << func() << std::endl; }

That is, before creating a function, the type of data that it returns must be specified. Also, in C++, there are special void functions. Functions of this data type are allowed to return nothing:

first_example.cpp

first_example.cpp

second_example.cpp

second_example.cpp

copy
123456789101112
#include <iostream> void voidFunction() { std::cout << "It's void function!" << std::endl; // Function without return } int main() { voidFunction(); }

There can be multiple returns inside functions, and each one will only fire under certain conditions.

main.cpp

main.cpp

copy
1234567891011121314151617
#include <iostream> int func() { int a = 50; int b = 6; if (a > b) // If `a > b`, func will `return a` return a; else // Otherwise func will `return b` return b; } int main() { std::cout << func() << std::endl; // Func calling }

If there are two returns, the second return function will be ignored:

main.cpp

main.cpp

copy
123456789101112131415
#include <iostream> int func() { int a = 50; // Declare variable a int b = 6; // Declare variable b return a; // Function stops here, b is never returned return b; // Unreachable } int main() { std::cout << func() << std::endl; // Call func and print result }
question mark

What happens in when a return statement is executed inside a function?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 5. ChapterΒ 3
some-alt