• Register
1 vote
18 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

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 15 views
15 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 LizzyM 6.1k points
1 vote
1 answer 18 views
18 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 Gavin 15.3k points
1 vote
1 answer 19 views
19 views
Problem: Hello good souls, I am an entry-level student of Python programming. I recently started learning python. I&rsquo;ve created a python function that should calculate the multiplication value of two numbers and returns it. But the script is not executing and produces an error of ... # Calculate the value of a*b mul = a * b return(mul) print('Multiplication is: ', is_a_mul_fuc(10, 5));
asked Jun 15 adamSw 11.3k points
0 votes
1 answer 7 views
7 views
Problem: I want to return a string value from a function in C++. I have returned integers from functions but have no idea how to return strings. If someone could help me, it would be appreciated. Thanks
asked Oct 12 Sheeza 2.2k points
0 votes
1 answer 22 views
22 views
Problem: I have recently started learning C language as currently lockdown is going on so I am learning C language with the help of available online tutorials. I have managed to write the binary search algorithm. If I try to compile my binary search algorithm then I am facing very ... +1, high);     else if(val == sorted[mid])         return mid; } Please guide me in fixing my above C code.
asked Aug 1 Raphael Pacheco 4.9k points