Click here to Skip to main content
15,905,566 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have a sub-function with return type which is not "void" (e.g: int, double,...), I want to exit that function when any condition is incorrect, e.g:

C#
private int f(int x)
{
     if (x==1)
     {
           return 2*x;
     }
     else
     {
           // exit this function???
     }
}


How to do this? Thanks.
Posted
Comments
Sergey Alexandrovich Kryukov 12-Dec-13 13:23pm    
This is not a function. What is a "sub-function" anyway? There is no such thing.
—SA

C#
private int f(int x)
       {
           if (x == 1)
           {
               return 2 * x;
           }
           else
           {
               return 0;
               // exit this function???
           }
       }
 
Share this answer
 
v3
Comments
Andrewpeter 12-Dec-13 9:47am    
Oh, thanks. When return type of function is user-define type (e.g: A_type,...) how can I solve it? Hope you help me!
Karthik_Mahalingam 12-Dec-13 10:00am    
If it is a value type you can use default(Type);
if it is referece type you can use return null
Andrewpeter 12-Dec-13 10:04am    
Hj, thank you very much! It's great!
Karthik_Mahalingam 12-Dec-13 10:09am    
welcome andrew :)
You cannot exit the function without returning a value

you can use the default value (or) some defined value of the return type to exit the function


C#
private int f(int x)
       {
           if (x == 1)
           {
               return 2 * x;
           }
           else
           {
               return default(int);
               // exit this function???
           }
       }



If it is a value type you can use default(Type);
if it is referece type you can use return null
 
Share this answer
 
v2
Comments
Andrewpeter 12-Dec-13 9:51am    
OK, it's great! My vote is 5. Thanks.
Karthik_Mahalingam 12-Dec-13 10:04am    
thanks andrew :)
Dave Kreskowiak 12-Dec-13 11:40am    
I beg to differ. You CAN exit the function without returning a value, by throwing an exception.
Karthik_Mahalingam 12-Dec-13 20:52pm    
yes i agree, but it is illegal. It is against the coding flow of execution.

if they are using exception handler in the calling method, it wont execute the remaining lines of code instead it will go to the catch block.
Dave Kreskowiak 12-Dec-13 21:23pm    
Illegal?? Says who??

If you're referring to the OP's exact situation, no, it's not appropriate. I just said it IS possible to exit a sub without returning a value. I didn't say it was appropriate for every situation.
There are couple of ways you can do that

Method 1 - Return NULL (you will need to change the return type int to int?)
C#
private int? f(int x)
{
     if (x==1)
     {
           return 2*x;
     }
     else
     {
           return null;// exit this function
     }
}


Method 2 - You can set a default value that will represent incorrect condition.

C#
private int f(int x)
{
     if (x==1)
     {
           return 2*x;
     }
     else
     {
           return  -1; // exit this function with a default value and in caller function you will check if it is -1.
     }
}


Method 3 - You can create TryParse kind functions where return type is bool and one parameter is out.
 
Share this answer
 
Comments
Andrewpeter 12-Dec-13 9:53am    
Thanks. It's very good! I'll applicate for my app.
BillWoodruff 12-Dec-13 10:28am    
+5 Excellent answer.

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