Click here to Skip to main content
15,887,436 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
AnswerRe: ( C/C++ historical question) was there a point in time where adding a return at the end of a void function was required ? Pin
Gerry Schmitz21-Dec-23 8:02
mveGerry Schmitz21-Dec-23 8:02 
AnswerRe: ( C/C++ historical question) was there a point in time where adding a return at the end of a void function was required ? Pin
glennPattonWork322-Dec-23 1:16
professionalglennPattonWork322-Dec-23 1:16 
AnswerRe: ( C/C++ historical question) was there a point in time where adding a return at the end of a void function was required ? Pin
mdblack9822-Dec-23 2:10
mdblack9822-Dec-23 2:10 
GeneralRe: ( C/C++ historical question) was there a point in time where adding a return at the end of a void function was required ? Pin
Owen Lawrence22-Dec-23 5:19
Owen Lawrence22-Dec-23 5:19 
AnswerRe: ( C/C++ historical question) was there a point in time where adding a return at the end of a void function was required ? Pin
Juan Pablo Reyes Altamirano22-Dec-23 5:49
Juan Pablo Reyes Altamirano22-Dec-23 5:49 
GeneralRe: ( C/C++ historical question) was there a point in time where adding a return at the end of a void function was required ? Pin
jschell22-Dec-23 7:09
jschell22-Dec-23 7:09 
AnswerRe: ( C/C++ historical question) was there a point in time where adding a return at the end of a void function was required ? Pin
Ralf Quint22-Dec-23 7:27
Ralf Quint22-Dec-23 7:27 
AnswerRe: ( C/C++ historical question) was there a point in time where adding a return at the end of a void function was required ? Pin
Member 1616867422-Dec-23 13:35
Member 1616867422-Dec-23 13:35 
A long way (below) to get to the short version of the answer: No, it was never required and quite on purpose!! C was sort of ALGOL like and to get around one of the most common mechanical errors in assembly language, we forced ALGOL like structure. Some of those errors in assembler came from multiple entry and exit points from subroutines (what we call typed and untyped functions in C). Having an explicit "return" instruction even in an untyped function in C allowed for multiple exits from a function but only *one* entry. This fixed a lot of errors we used to get in assembly language. My opinion is that this is a semi-reasonable thing to do in C. When K&R wrote their initial "White Book" (mostly good for toilet paper!), they intentionally made the closing brace the equivalent of an explicit "return();" or "return;" statement. Remember that C was written to replace assembler on a PDP-8 or PDP-11 from Digital Equipment Corp. Such a machine might have used dual 186 kilobyte 8 inch single sided floppy disks and have maybe 16kB of main memory. To that end, a lot of things like implicit "return" and other things were implemented to save bytes on those floppy disks when the source files were saved. Same reason original C only allowed significance of 6 character function and variable names!!!

Too Long; Didn't Read Version:
I have been programming in "C" since BSD Unix and System III Unix first came to be available in the wild. Pretty sure early HPUX was a BSD release and where I did most work. Your question as posed cannot be answered because C and C++ are totally different languages and I would go so far as to say "C++" has *never* been a language! Stroustrop *never* got a standard for the initial C++ and standards such as C++17 (not sure I have the number right) look *nothing* like the C++ "things" I used when first using the prototype language versions. My point: C++17 is *NOT* C++ but a separate newer and incompatible language with numerous upgrades, stupid ideas, mistakes, and other differences from C++ as described by Stroustrop in his book. I think I gave my copy of the book to Half Price Books when we downsized our house. It was not uncommon for some of those languages to *require* and explicit return statement or even to require *just one* exit from a function. In some languages, an "untyped function" was instead called a "procedure". Just semantics... Now we have compilers with type checking and *many* other mechanical aids to make arguably correct code that we couldn't do in the 80s.

So, now that history lesson is over, I'll answer your question for C only from a 45 or so year perspective. The 1980's were a time when we in the business were struggling with ways to mechanically make sure our code was correct and error free. It spawned a *LOT* of languages designed for just that goal: Modula2, Pascal, Ada, PLM86, Pascal86, TurboPascal, PLM, etc. Most were based on ALGOL. A lot of those languages didn't ever make it to both microcomputers (remember when we had 65536 bytes of RAM and as much as 128k couldn't even be accessed) or to Unix. Much *real* code was being done in native assembly language, FORTRAN, or COBOL!!!! My ex-wife spent an entire 30 year career writing COBOL. DuPont ran entire chemical plants in the 80s on PDP-11s with 10s of thousands of lines written in FORTRAN.

In C (and to a small extent C++), the "return()" statement or the end brace, restores the previous stack pointers and then pops the address of the calling subroutine as part of executing the assembly language return from subroutine instruction. If you turn off optimization, the following code generates *2* rts instructions with one of those being dead code that can never be executed!

void test_program(void)
{
return;
}

test_program:
; set up the function stack as part of opening curly brace
push sp;
; return statement
pop sp;
rts;
; ending curly brace
pop sp;
rts;
; end of function
AnswerRe: ( C/C++ historical question) was there a point in time where adding a return at the end of a void function was required ? Pin
sx200826-Dec-23 19:17
sx200826-Dec-23 19:17 
GeneralRe: ( C/C++ historical question) was there a point in time where adding a return at the end of a void function was required ? Pin
jschell27-Dec-23 5:59
jschell27-Dec-23 5:59 
GeneralRe: ( C/C++ historical question) was there a point in time where adding a return at the end of a void function was required ? Pin
sx200829-Dec-23 16:59
sx200829-Dec-23 16:59 
QuestionGood alternatives to STM32CubeIDE? Pin
CPallini21-Dec-23 1:03
mveCPallini21-Dec-23 1:03 
AnswerRe: Good alternatives to STM32CubeIDE? Pin
Mike Hankey21-Dec-23 3:24
mveMike Hankey21-Dec-23 3:24 
GeneralRe: Good alternatives to STM32CubeIDE? Pin
CPallini21-Dec-23 3:32
mveCPallini21-Dec-23 3:32 
GeneralRe: Good alternatives to STM32CubeIDE? Pin
honey the codewitch21-Dec-23 4:28
mvahoney the codewitch21-Dec-23 4:28 
AnswerRe: Good alternatives to STM32CubeIDE? Pin
honey the codewitch21-Dec-23 4:27
mvahoney the codewitch21-Dec-23 4:27 
GeneralRe: Good alternatives to STM32CubeIDE? Pin
CPallini21-Dec-23 7:15
mveCPallini21-Dec-23 7:15 
GeneralRe: Good alternatives to STM32CubeIDE? Pin
honey the codewitch21-Dec-23 7:16
mvahoney the codewitch21-Dec-23 7:16 
AnswerRe: Good alternatives to STM32CubeIDE? Pin
Nagy Vilmos22-Dec-23 1:52
professionalNagy Vilmos22-Dec-23 1:52 
GeneralRe: Good alternatives to STM32CubeIDE? Pin
CPallini22-Dec-23 4:14
mveCPallini22-Dec-23 4:14 
GeneralKnow Thyself Pin
Steve Raw20-Dec-23 21:00
professionalSteve Raw20-Dec-23 21:00 
GeneralRe: Know Thyself Pin
jschell21-Dec-23 4:33
jschell21-Dec-23 4:33 
GeneralRe: Know Thyself Pin
Steve Raw21-Dec-23 5:20
professionalSteve Raw21-Dec-23 5:20 
AnswerRe: Know Thyself Pin
David O'Neil21-Dec-23 7:13
professionalDavid O'Neil21-Dec-23 7:13 
GeneralRe: Know Thyself Pin
Steve Raw21-Dec-23 14:04
professionalSteve Raw21-Dec-23 14:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.