0
What's the calling process?, and what does *return 0;* change in it?
2 Answers
+ 1
The calling process is the simple action of writing in your main program the name of your defined function and passing variables created in your main application as parameters of your function defined outside of your main application.
If you notice, I wrote my functions in the last comment outside of the main( ) with local variables, like a and b. These a and b only exist inside the implementation of the function to show it what to do, but they don't exist in the main( ). They are substituted by num1 and num2, created inside the main. These we call universal variables.
With built-in functions, it works the same way.
Examples of built-in functions:
printf( );
scanf( );
getch( );
system( );
0
To understand clearly the return statement, you must know very well what are the void and non void data types.
The main difference is noticed if you try to create 2 mathematic functions to do the same thing.
Let's create a function to sum 2 integers:
int sum(int a, int b)
{
return a + b;
/*the return keyword must always be in "non void" functions. Perhaps, it only returns single values, without any kind of text*/
}
void sum(int a, int b)
{
int result;
result = a + b;
printf("%d\n", result );
/*in this case, as we declared the function as void, it has no numerical or logical return. If you want something to be shown, you must print it inside the scope of the function. You can print texts too, as we're dealing with double quotation marked strings when we're printing results*/
}
Another advantage is that you can define what's gonna be printed inside the scope instead of printing things outside of the function. It means that, in void functions, you can define texts to be printed everytime the function is gonna be called.
example 1:
int sum(int a, int b)
{
return a + b;
}
int main(){
int num1 = 2;
int num2 = 4;
printf("Sum of the 2 numbers is %d", sum(num1,num2));
return 0;
}
example 2:
void sum(int a, int b)
{
int result;
result = a + b;
printf("Sum of the 2 numbers is %d", result);
}
int main(){
int num1 = 2;
int num2 = 4;
sum(num1, num2);
return 0;
}
I hope it helps you to clarify things