[C-prog-lang-l] Non-returning function with "working" code

Jan Pechanec jp at devnull.cz
Wed Mar 4 16:38:52 CET 2020


Hi,

there was a question today why using a function that does not return a 
value while it should have does not prevent the code from working.

Like this:

$ cat main.c 
#include <stdio.h>

int
addnum(int n1, int n2)
{
	int n = n1 + n2;
}

int
main(void)
{
	printf("%d\n", addnum(1, 99));
}

$ gcc main.c
$ ./a.out 
100


As I said at class, it works by accident.  To see what really is going 
on, I put the explanation to the notes for today's class.  I put it in 
a separate input module (we use small self-contained modules to build 
the resulting notes), accessible here:

https://github.com/devnull-cz/c-prog-lang/blob/master/modules/why-does-it-work-1.md

Thanks for the question!

Cheers, Jan.

-- 
Jan Pechanec <jp (at) devnull (dot) cz>
http://www.devnull.cz


More information about the c-prog-lang-l mailing list