Click here to Skip to main content
15,897,273 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
when we are returning the value as 0 for the main() which is make sure that the given program is executed properly .But can we return a user defined function in main()

What I have tried:

#include<stdio.h>
int add(int,int);
int main()
{
-----
-----
return add();
}

int add(int a,int b)
{
------
------
}
Posted
Updated 20-Jul-16 11:53am
Comments
Richard Deeming 19-Jul-16 11:28am    
My guess would be "yes", but why don't you try it and see?
ShreeshaVitthal 20-Jul-16 4:14am    
ya thanks got it.

Quote:
when we are returning the value as 0 for the main() which is make sure that the given program is executed properly .But can we return a user defined function in main()
You are not returning a user defined value, but a value from a function.
C++
int add(int a,int b)

This function returns int, which is a built-in type in C++. And so you can use these functions in your programs to return the values. Read the paragraph after following quotation. However, if you wanted to do something like this,
C++
class userdefined {
  // members
};

userdefined main() {
   userdefined defined;

   // processing
   return defined;
}

This would not fall in the standards of C++ and you will be forcing the program to work in a way that it is not expected to. Compiler will complain about a few things, such as, "main must return int". That is because of the specifications.

Quote:
when we are returning the value as 0 for the main() which is make sure that the given program is executed properly
To be precise, 0 comes from the standards, where as the actual macro for this is "EXIT_SUCCESS". This is to tell the underlying framework, that the program terminated with success, normal termination. When you return another value from the function, underlying framework or the service may not know that the result was intended. Framework would consider it to be some sort of error or what-ever that framework or operating system has in the database. That is why, programs must return 0, if you feel unhappy with this you can skip this line as of C 1999 because the compiler itself will add the return 0; at the end of the program, directing framework to consider it to be a normal termination.

For more please read the following documents,

Main function - cppreference.com[^]
Structure of a program - C++ Tutorials[^]
c++ - Should I return EXIT_SUCCESS or 0 from main()? - Stack Overflow[^]

No standard for main function:

That is wrong, there is a standard for this and compilers follow that. For a demo, try this[^]:
C++
class defined { };

defined main()
{
  defined usrdfnd;
  
  return usrdfnd;
}

/* 
 7:14: error: '::main' must return 'int'
 In function 'int main()':
 11:10: error: cannot convert 'defined' to 'int' in return
 */

You can read the error message and see what it says. You can however, always change the compiler procedures and modify the way it follows the standards.
 
Share this answer
 
v3
That will not even compile, since you are calling add without its required parameters. And returning a function (even if that were possible) from main makes no sense since the shell would not know what to do with it.
 
Share this answer
 
Yes you can.

It is up to you to define which values are returned by the main function and how they have to be interpreted (usually in the program documentation and optionally as part of the help / usage output with command line application). There is no standard what values should be returned. However, it is common to return zero upon normal program termination.
 
Share this answer
 
Comments
Afzaal Ahmad Zeeshan 19-Jul-16 11:40am    
But that would require a lot of modifications to compilers in order to train them to not follow this specification. Besides, I won't trust a compiler that doesn't follow the specifications of a language.
Jochen Arndt 19-Jul-16 11:48am    
I don't get your point.

He asked if he can return the value returned by his add() function. That is perfectly valid as long as he returns an int which is the case with his example. Even other types are valid here when they can be casted to int without loss of precision (e.g. signed and unsigned char and short).
Afzaal Ahmad Zeeshan 19-Jul-16 11:54am    
Yes, the values can be returned and function calls can be made but only as long as they are integral. That is the part where I do agree to you. But the part that it there is no standard, is where I do not. Values must also conform to the standards, otherwise operating system may be logging error reports (if the return code directs it to).
Jochen Arndt 19-Jul-16 12:15pm    
The only standard is that main() must return an int.

There is no standard about the value itself. If you think there is one that applies here, please tell me.

It is only common practice to return zero upon normal exit. One reason for returning zero/non-zero is that these can be used with command chaining using the && and || operators.
Yes you can as long as your code don't have other errors.

General principle, anything that is legal to store in a variable of given type is also legal as the value of a return of a function of same type.
C++
// say you have a variable
int myvar;
// if it is legal to
myvar= 1+int(15/3)+4*5;
//it is also legal to
return 1+int(15/3)+4*5;
// if the function is define as
int myfunc()
 
Share this answer
 
v2
thank you guys this is what i tried
#include<stdio.h>
int add()
{
	int a=10,b=10,c;
	printf("Fuction is invoked\n");
	c=a+b;
	printf("Sum is %d",c);
}

int main()
{
	int v=9;
	printf("testing one\n");
	if(v>9)
	printf("try againg\n");
	return add();
}

output:--
testing one
Function is invoked
 
Share this answer
 
v2
Comments
jeron1 20-Jul-16 10:10am    
So the "Sum is..." not printed? or the console window closes so fast you can't see it?
Patrice T 20-Jul-16 12:17pm    
Use Improve question to update your question.
So that everyone can pay attention to this information.
Advantage, everyone will see your new situation as thisnot a Solution.
Or open a new question as this code also fail.
Richard MacCutchan 25-Jul-16 11:29am    
This is just standard C programming where a function is called in a statement and the function call replaced by its return value. So you are not returning a function, you are returning the value returned by calling add.
From your Solution 5.
Put this line in main and you will see the problem.
C++
printf("Sum is %d",add());


You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
 
Share this answer
 
Comments
ShreeshaVitthal 25-Jul-16 9:54am    
Then there is no point in the question
Patrice T 25-Jul-16 10:21am    
I don't understand, can you develop ?
English is not my native language.
ShreeshaVitthal 25-Jul-16 11:13am    
The code which you have given if I use that then I may get the answer, but task is to get the answer by returning add() through main().
Patrice T 25-Jul-16 11:52am    
it is for debugging purpose only.
If you do it, you will see that add() do not retrun a value !

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900