Jump to content

Functions, Optional Parameters And Returning Params


Recommended Posts

Hi the BoostC guide gives very little info about functions.

Please advise for the following:

 

as I understand:

void delaymsec(unsigned char delayms,bool busy)

Would return nothing, as a proceedure.

delaymsec(unsigned char delayms,bool busy)

Would return values in delayms and busy

 

is it possible to also make one parameter optional eg if delaymsec(); //returns value of bool busy only

 

Tks,

Al.

Link to post
Share on other sites
...void delaymsec(unsigned char delayms,bool busy)

Would return nothing, as a proceedure.

Correct

delaymsec(unsigned char delayms,bool busy)

Would return values in delayms and busy

 

Wrong, This function returns int with the value used in return statement. If you want it to return values in delayms and busy pass them as references:

 

void delaymsec(unsigned char &delayms,bool &busy)

 

 

is it possible to also make one parameter optional eg if delaymsec(); //returns value of bool busy only

 

Yes, just use reference for busy only:

 

void delaymsec(unsigned char delayms,bool &busy)

 

Regards,

Pavel

Link to post
Share on other sites
Thanks vm. Sorry to be pedantic, but to refer to your last example of the function:

void delaymsec(unsigned char delayms,bool &busy)

 

on the calling side:

delaymsec(); would return the value of busy, with no error for the missing parameter delayms?

Tks,

Al.

 

When function is called all its arguments should be specified. For void delaymsec(unsigned char delayms,bool &busy) call like delaymsec(); won't compile.

 

Regards,

Pavel

Link to post
Share on other sites

OK tks. To clarify for others who read this thread if they are as BoostC illiterate as I:

To return values from function you must still specify the values, even if not used eg:

to get the value of busy:

delaymsec(0,0); returns true if the function has busy set.

 

Actually the plot thickens (or I)

The function is meant to be re-entrant eg if a interrupt calls the function, and its already being used, it should return busy as true.

So I trust this is secure coding:

 

//From IRQ service routine:

if (delaymsec(0,0)==0) delaymsec(100,0); //allow the function if not being used elsewhere.

 

void delaymsec(unsigned char delayms,bool &busy)
{
unsigned char temp;
if (busy==1 || temp==0) return busy;  //exit function to caller with busy
else busy=1;	
do
{
temp=249;
do clear_wdt();while(--temp);
}	
while(--delayms);
busy=0;
}

Link to post
Share on other sites
OK tks. To clarify for others who read this thread if they are as BoostC illiterate as I:

To return values from function you must still specify the values, even if not used eg:

to get the value of busy:

delaymsec(0,0); returns true if the function has busy set.

 

Actually the plot thickens (or I)

The function is meant to be re-entrant eg if a interrupt calls the function, and its already being used, it should return busy as true.

So I trust this is secure coding:

 

//From IRQ service routine:

if (delaymsec(0,0)==0) delaymsec(100,0); //allow the function if not being used elsewhere.

 

void delaymsec(unsigned char delayms,bool &busy)
{
unsigned char temp;
if (busy==1 || temp==0) return busy;  //exit function to caller with busy
else busy=1;	
do
{
temp=249;
do clear_wdt();while(--temp);
}	
while(--delayms);
busy=0;
}

 

Sorry but your code is wrong. A void function will not return anything. I think you confuse function return value and values of function arguments. In this particular example function won't return anything but it's argument busy will change its value and this new value can be used after the function call.

 

Regards,

Pavel

Link to post
Share on other sites

Regretfully, having trouble with this:

//declaration returns compile error: "Error, missing right paren"

divby2n(unsigned char &avlotemp,unsigned char &avhitemp,unsigned char divisor);

 

//function

divby2n(unsigned char &avlotemp,unsigned char &avhitemp,unsigned char divisor)

{

do

{

avhitemp>>=1;

avlotemp>>=1;

divisor>>=1;

} while (--divisor);

}

Edited by Alistair George
Link to post
Share on other sites

Well it should be:

//incomplete declaration returned compile error: "Error, missing right paren"
void divby2n(unsigned char &avlotemp,unsigned char &avhitemp,unsigned char divisor);

//function
void divby2n(unsigned char &avlotemp,unsigned char &avhitemp,unsigned char divisor)
{
do
{
avhitemp>>=1;
avlotemp>>=1;
divisor>>=1;
} while (--divisor);
}

which Boost C V6.96 had no problem compiling.

 

Without that 'void' any strict ANSII compiler could be expected to complain. the implied 'int' type of an un-typed function is only for pre-ANSII K&R compatibility, don't expect it to work as a prototype.

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...
×
×
  • Create New...