Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Quit from procedure in C

Status
Not open for further replies.

JaMe

Junior Member level 1
Joined
Nov 8, 2006
Messages
19
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
Indonesia
Activity points
1,406
In a function, "return" will let you quit from a function. What is used if it is in procedure ?
 

In the C language all procedures are functions. The closest analogy to a procedure is a function that returns a void type (empty value).

e.g.

void myProcedure()
{
/* do something */
}


If you want to return from a void function you just use "return;" without specifying any return value, since one is not expected by the calling function.

void myProcedure(int aValue)
{
if (aValue < 0)
return;
else
printf("aValue looks good to me: %d\n",aValue);
}
 

    JaMe

    Points: 2
    Helpful Answer Positive Rating
There is no need to write return. void myProcedure(int aValue)
{
if (aValue < 0)
//return; comment out
else
printf("aValue looks good to me: %d\n",aValue);
}
some compilers will actually complain.
 

You can use "break;" if your function don't return anything

if you use void with a return ! some compiler will generate error
 

In the C language all procedures are functions.
If you use void with a return ! some compiler will generate error.
So no need to write return
 

Just Use Break command

Example
int a = 0;
for(a = 0; a < 10; a ++)
{
printf("\t%d", a);
if(a == 5)
{break;}
}

The break will exit from the for loop... As for function you just need to put a condition and break it.
 

In C, procedure is a function that returns nothing. You have to declare it as

void MyRoutine() //with or without parameter
{
...some statements...
return;
}

A function that returns nothing will return control to the caller once end of the function is reached (}), or if it encounter early return; statement. break will only exit from a loop statement, e.g.

void MyRoutine(void)
{
int i;

for(i=0; i<=100; i++){
if(i==50)
break;
...some statements...
} // End of loop

...some statements again...
} //End of function

When i reaches 50, control exits from the loop, the statements after the loop will be executed, and then control will be passed back to the caller. So break-ing will not return control to the caller unless it is placed before end of function -- which is no need if it is.

Caution: declaring a function as

MyRoutine(void);

doesn't imply that the function returns nothing, instead, the compiler assumes return type to be of type int. So you MUST declare the return type as void if that is what you meant.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top