• Register
1 vote
35 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 18 views
18 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 19 views
19 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: Compiler showed: File "temp.py", line 56 return result SyntaxError: 'return' outside function Where was I wrong?
asked 5 days ago Mashhoodch 5.6k points
1 vote
1 answer 26 views
26 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, 2020 adamSw 11.3k points
0 votes
1 answer 8 views
8 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, 2020 Code Learner 5.1k points
0 votes
1 answer 30 views
30 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, 2020 Raphael Pacheco 4.9k points
0 votes
1 answer 18 views
18 views
Problem: How return returns a value in a function, why println prints text out to the command line. A function in java sometimes returns a value. How i can then call this function from another location in the code to get that value.
asked Nov 4, 2020 Mashhoodch 5.6k points
0 votes
2 answers 28 views
28 views
Lets say I define a simple function which will display an integer passed to it: def funct1(param1): print(param1) return(param1) the output will be the same but and I know that when a return statement is used in a function the output can be used again. Otherwise ... of a print statement cannot be used. But I know this is not the formal definition, Can anyone provide me with a good definition?
asked Oct 25, 2020 psandprop 2.4k points
0 votes
1 answer 37 views
37 views
Problem: Hi! I recently attempted an online multiple-choice question test. There was a statement that was to be marked as true or false. The statement was: A function cannot return the value of an enumeration type. I marked true but, I am confused. Can anybody here ... , then how can we return an enumeration by a function? An example program will be much helpful if this statement is false. Thanks!
asked Dec 5, 2020 Code Learner 5.1k points
0 votes
1 answer 20 views
20 views
My binary search function does not return a variable (C ++) int binarySearch (vector <Data> vectorBS, string x) { lower int = 0; int top = vectorBS.size () - 1; bool flag = false; while (lower <= upper) {     int half = (lower + upper) / 2;     if (vectorBS ... half; } else {     return -1; } } What could be the problem? half is declared inside the function and should return it when flag == true.
asked Aug 15, 2020 game 4.6k points