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 ?
 

slcoleman

Newbie level 3
Joined
Aug 17, 2007
Messages
3
Helped
1
Reputation
2
Reaction score
0
Trophy points
1,281
Activity points
1,310
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

keinstein

Junior Member level 1
Joined
Jun 18, 2007
Messages
17
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,376
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.
 

Cortex

Full Member level 3
Joined
Mar 26, 2005
Messages
182
Helped
12
Reputation
24
Reaction score
4
Trophy points
1,298
Location
Morocco
Activity points
2,733
You can use "break;" if your function don't return anything

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

jayavardhan

Full Member level 2
Joined
Sep 1, 2006
Messages
126
Helped
3
Reputation
6
Reaction score
0
Trophy points
1,296
Location
India
Activity points
1,851
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
 

scootyty

Newbie level 4
Joined
Nov 22, 2007
Messages
6
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,308
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.
 

Darth Maul

Junior Member level 1
Joined
Jul 19, 2007
Messages
18
Helped
2
Reputation
4
Reaction score
0
Trophy points
1,281
Location
Yogyakarta, Indonesia
Activity points
1,436
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.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top