Jump to content
Sign in to follow this  
jwilson

Question About Functions

Recommended Posts

Hope this question isn't too broad...

 

 

In the code that I plan on writing (still in the flowchart stages) I will call a function from numerous other functions.

 

What I want to do is after the function is done I DON'T want it to return back to the function that called it but rather a particular spot in main().

 

Is there anyway for me to do something like this. Like a GOTO statement or something?

 

Thanks

Edited by jwilson

Share this post


Link to post
Share on other sites

It is not possible to use goto to transfer control to a label outside of the function block where the goto is used.

 

While some compilers implement exception handling that could provide the kind of functionality you are asking about but BoostC does not seem to offer this feature.

 

In general it's a bad practice to have a function that exits from more than one point.

 

Just debugging a function block with a misplaced goto or break statement can be quite a challenge. Don't go asking for trouble.

Share this post


Link to post
Share on other sites
...In general it's a bad practice to have a function that exits from more than one point...

 

It's very common for a function to have multiple exit points (i.e. multiple return statements). Maybe what you really mean is multiple entry points :)

 

Regards,

Pavel

Share this post


Link to post
Share on other sites

I have cause problems for myself several times by creating functions with multiple returns and complex while loops with breaks to exit. Just misplace one little brace and four hours later have an oh $%@* moment.

 

It's a good practice for me to have function that exit at only one point.

 

But I see your point when using C for a resource limited target.

 

I have resorted to just about every trick to get the code to fit one time or another.

 

Assembler is great. I have used a lot of them, and the PIC offers so many opportunities to really just do yourself. And not in a good way. :)

Share this post


Link to post
Share on other sites

Thanks for the replies.

 

Well I am not quite sure how I will get around not implementing this without making it a function.... but i will see what i can do.

Share this post


Link to post
Share on other sites

jwilson,

 

Well I am not quite sure how I will get around not implementing this without making it a function.... but i will see what i can do.

What exactly are you trying to do ?

 

Regards

Dave

Share this post


Link to post
Share on other sites

Well I was wanting to use main to basically just call my functions and make the code extremely clear to someone without them having to know exactly how everything works.

I decided on a slightly different approach and I am going that route.

 

Thanks for your replies.

Edited by jwilson

Share this post


Link to post
Share on other sites

Is there any reason you dont just use a state machine in your main? i am guessing you would

have a condition to handle which function you go into as well as what you do in the function.

Using the case/switch will help (not prevent) you stay away from going too deep into the stack,

and in a lot of cases is more efficent in processing time since you dont evaluate against everything.

 

ie:

switch(funcReturn)
{
   case 1:
   {
       funcReturn = 0;
       break;
   }
   case 2:
   {
       stateFun = _weirdFunction();
       switch(stateFun)
       {
               case 1:
                    break;
               case 2:
               {
                   x=y;
               }
               default:
               {
                   stateFun = 0;
               }
       }
       funcReturn = 0;
       break;
   }
   default:
   {
       funcReturn = _someFunction();
       break;
   }
}

 

But the hard part is mentaly building and coding the state machine to do what you want.

On the flip side, using a state machine setup fits extremly well to a block diagram and is easy

to follow programming flow.

 

i hope that might help in some way.

Share this post


Link to post
Share on other sites

why not try to use an interrupt routine in your function? when an interrupt occurred, have your conditional statements handle where you want to go in your main.

Share this post


Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...