• Register
1 vote
44 views

Problem:

I am exploring the c++ programming language nowadays. It’s really interesting to learn new things every day. I was trying to adopt the methods in c++ but struggling to return values from a void method. I actually want to know that if it is possible to return values from a void method or you may have a return statement in a void method.

Thanks for your co-operation in this learning process. I really appreciate your effort.

Thanks again.

12 7 7
15,250 points

Please log in or register to answer this question.

1 Answer

1 vote
 
Best answer

Solution:

Void functions are “void” due to the fact that they are not supposed to return values. True, but not completely. We may not able to return value from a void function but we may return something others from a void function in various cases. Let’s see how to do it.

#include <iostream> 
using namespace std; 
  
void func() { 
   cout << "I am a void function";   
   return;  
} 
  
int main() { 
   func(); 
   return 0; 
}

We simply wrote a return statement inside the void function and call the void function from the main function. This program provides an output of:

I am a void function

We also can return a void function inside a void function. Let’s see:

#include<iostream> 
using namespace std;   
void func() { 
    cout << "The is a void function"; 
} 
  
void anotherFunc() { 
    return func(); 
} 
  
int main() { 
    anotherFunc; 
    return 0; 
} 

So, here we’ll get the out of the void func because of passing the function into another void function, and later on, call it from the main function.I hope these above examples make things clear to you.

Thanks

13 9 6
94,240 points

Related questions

1 vote
1 answer 21 views
21 views
Problem: I am a beginner in programming language and I hvae been doing some coding practice and learning some programing logics .So, can anyone explain me the return statement in a void method of python?
asked Apr 8, 2020 LizzyM 6.1k points
1 vote
1 answer 25 views
25 views
Problem: I admit it that Java programming is fun but it seems confusing sometimes. I am trying to adopt somethings about method and object in java. I know how to create a method and calling it inside the main method. I have a very simple question, can a void ... have a return statement? What happens if I try to return values from a void method? A well-explanation is expected. Thanks again, guys.
asked Mar 30, 2020 Gavin 15.3k points
0 votes
1 answer 5 views
5 views
Problem Can someone help me how to return a value from a function in c++
asked Mar 24 Subhendu 4.1k points
0 votes
1 answer 4 views
4 views
Problem Anyone can help me how i return array from function
asked Mar 26 Subhendu 4.1k points
0 votes
1 answer 2 views
2 views
Problem: I have an array int arr[5] that is passed to a function fillarr(int arr[]): int fillarr(int arr[]) { for(...); return arr; } How can I return that array? How will I use it, say I returned a pointer how am I going to access it?
asked Mar 26 Shovo210 13.2k points
0 votes
1 answer 2 views
2 views
Problem: Can anyone tell me the solution of this problem? How to return an array in c++?
asked Apr 3 Ifra 24.4k points
0 votes
1 answer 3 views
3 views
Problem what is the meaning of return 0 in c++
asked Mar 26 Subhendu 4.1k points
0 votes
1 answer 9 views
9 views
Problem: My question is simple. Why do we use return 0 in a C/C++ program if it does not do anything? For example, if I have a program whose output is 100 somehow, then I use return 0 after the print command, the output will still be 100. Then, what is the use of return 0?
asked Mar 2 Code Learner 9.2k points
0 votes
1 answer 18 views
18 views
Problem: I am a noob and my question is simple. What does return 0 do in every C++ program? Is it important to end each program with return 0 and what if we use any number other than 0 in the return statement? I am confused so looking forward to a detailed answer. Thanks!
asked Feb 24 Code Learner 9.2k points
0 votes
1 answer 22 views
22 views
Problem; what does return do in c++
asked Feb 6 kaash_kashi 1.2k points