0
What is calling a function or a process? And how and why are values returned?
In the description for the return statement, it is given that the statement returns a zero value to the called function. So basically, what is called 'calling' a function or a process? And how and why are values returned to it?
2 Answers
+ 34
We need function or process to perform sub tasks to avoid repeating the same task. Also it helps to keep the main process neat and understandable. Consider the main function as a godfather who doesn't do anything, he just calls the other functions and makes all the works done by them đ
You can call a function to perform a task from another function (caller). Now, does your caller function need to know the result? If yes, then you must return the result from called function. (The godfather says, "Let me know what you've done đĄ ").
Otherwise you may simply write a void function to perform the task. Here, the godfather says "I don't care what you've done, just do it!! đĄ"
void multiply (int a, int b) {
cout<<a*b;
// no return, caller function won't know the result
}
Function call : multiply (3,5);
int multiply (int a, int b) {
return a*b;
// the result is passed to the caller function
}
Function call : int result = multiply (3,5);
// It works as int result = 15;
+ 16
If we talk about main() function then it is always called by the operating system.
We can choose whether we want to return something back to it or not.
If we want something to return we write-
int main(){
using namespace std;
cout<<"hello";
return 0;
}
if we won't, then-
void main(){
using namespace std;
cout<<"hello";
}
And we can return anything to the callee. It doesn't matter âș