473,382 Members | 1,648 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

More than one return statement in a function in c

54
Hi ,
Can we have more than one 'Return' statement in c?
If its possible please tel me the flow of the control between called and calling function.
Thanks
Jul 24 '07 #1
3 5827
Although it is to have more than one return statement in any C function, only one of them can be called. As soon as the first return statement is called, the function exits and other return statements will not be called. To see why, consider the example:

Expand|Select|Wrap|Line Numbers
  1. double convert_miles_to_kilometers(double mi)
  2. {
  3.     double km;         // Declare a new variable to hold the converted value.
  4.     km = mi * 1.61;  // Conversion: 1 mile = 1.61 Km.
  5.     return km;          // Return the value from the function.
  6.  
  7. }
  8.  

So if main calls this function, it will have a statement such as:
Expand|Select|Wrap|Line Numbers
  1. double x;                                           // Declare variable.
  2. x = convert_miles_to_kilometers(5);     // Call the conversion function.
  3. printf("5 miles are %d kilometers.\n", x);
  4.  

Note that the function contains only one return statement, which was returned to x in the second line of the second code snippet.


Now consider a block of code that contains two return statements:

Expand|Select|Wrap|Line Numbers
  1. double absolute_value(double x)
  2. {
  3.     if (x >= 0)
  4.         return x;    // The absolute value of a positive value is unchanged.
  5.     else
  6.         return -x;   // The absolute value of a negative value is its opposite.
  7.  
  8. }
  9.  
If main calls the function absolute_value(2.5), the if statement will be evaluated to true, so the first return statement will be called and the function will exit. If, however, main calls absolute_value(-2.5), the if statement will be evaluated to false, so the second return statement (after else) will be called and the function will exit without having called the first return statement.



Hope that answers your question.




EDIT:
If you really have to return two values from a function, such as a function that takes in a length in meters and converts it to feet and inches, return statements won't do. Instead, you may have to pass the variables feet and inches by reference to let the function modify their address.
Jul 24 '07 #2
sindhu
54
Thanks Cook,
Its of immense help
Jul 30 '07 #3
Banfa
9,065 Expert Mod 8TB
EDIT:
If you really have to return two values from a function, such as a function that takes in a length in meters and converts it to feet and inches, return statements won't do. Instead, you may have to pass the variables feet and inches by reference to let the function modify their address.
Strictly speaking in C (and the was a C question) there is no pass by reference. C only passes by value.

I understand that you mean use pointers, however these pointers are passed by value, whatever they point to is in fact not passed at all although a pointer to them is and they can be accessed via the pointer.

I realise that this is quite a subtle (possibly even nit-picky) difference but support of pass by reference is one of the defining features of a language so it is worth noting.
Jul 30 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Dave Benjamin | last post by:
Here are some more ideas for how to implement a statement-friendly code block syntax in Python. Hopefully more "Pythonic" (that is, of or pertaining to those features noticably reminiscent of...
22
by: bearophile | last post by:
Ville Vainio: >It's highly typical for the newbies to suggest improvements to the >language. They will usually learn that they are wrong, but the >discussion that ensues can be fruitfull anyway...
27
by: Maximus | last post by:
Hi, I was just wondering, is it good to use return without arguments in a void function as following: void SetMapLayer() { if( !Map ) return; layer = LAYER_MAP; }
4
by: bluedolphin | last post by:
This is my first function in Visual and I'm having a simple syntax syntax issue that I'm hoping someone can help correct for me. I have a function Public Function...
10
by: LaEisem | last post by:
On-the-job, I have "inherited" a lot of old C language software. A question or two about when "casting" of null pointer constants is needed has occurred during behind-the-scenes cleanup of some...
15
by: Greenhorn | last post by:
Hi, when a function doesn't specify a return type ,value what value is returned. In the below programme, the function sample()is returning the value passed to 'k'. sample(int); main() { int...
6
by: lovecreatesbeauty | last post by:
I ever missed a `return' statement when write a function `int HighDigit(Num)' to get the highest digit of an integer. But even if the `return' statement is ignored the function still can obtain...
13
by: Edward W. | last post by:
hello, I have this function below which is simple and easy to understand private function ListHeight (byval UserScreenHeight as int) as int if UserScreenHeight < 1024 return 30 else return 50...
7
by: Terry Olsen | last post by:
How do I get this to work? It always returns False, even though I can see "This is True!" in the debug window. Do I have to invoke functions differently than subs? Private Delegate Function...
2
AmberJain
by: AmberJain | last post by:
HELLO, Wikipedia defines return statement as: Now there's a C program I have coded---->
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.