I have a TCP/IP application in C. I have 1 header file for handling TCP/IP related things & 1 main file to call all functions. Since problem is happening on client side, I am posting my client side code only.
Here is program.c
#include "tcpHeader.h"
#include <pthread.h>
#include <stdio.h>
int main(int argc, char **argv){
// all code regarding connecting to server on TCP on another thread.
// Join here after completing the thread execution
// It works completely fine upto the next line.
testFunctionFromMyHeader();
// after executing below line, program do not continue with 'void' return type of the function.
// But it works if I change return type to 'int'
waitForAMessageFromServerAndSendConfirmation(12345678901LL);
// program expects me to return an integer on last function call
// & all other calls from now.
// even if I don't need integer return type.
testFunctionFromMyHeader();
return 0;
}
Here is tcpHeader.h
#ifndef CLIENT_TCP
#define CLIENT_TCP
// all required headers.
// all global variables
void testFunctionFromMyHeader(){
printf("Test Function");
}
void waitForAMessageFromServerAndSendConfirmation(unsigned long long args){
// wait until receiving data from server in recv() function.
// received 'args' is used in processing the data.
// process the data received
// send the confirmation message to server in send() function.
// Server also received this confirmation without any problem on server side.
// all code in this function also worked properly.
// Even the next printf line is also executed.
printf("All operation completed.");
// with 'void' return type, next line does not make any difference.
// But if I return an 'int' then it works.
return NULL;
}
#endif
It stops on waitForAMessageFromServerAndSendConfirmation(unsigned long long) in program.c
but If I do like this in program.c, then it works.(Return type on functions from tcpHeader.h are also changed to appropriate types)
int confirmation = waitForAMessageFromServerAndSendConfirmation(12345678901LL);
if( confirmation == 0 ){ // 0 or any int value
int testFunctionReturnedValue = testFunctionFromMyHeader();
.
.
.
// keep going like this with int return type FOR ALL FUNCTIONS
}
Help me to identify the problem. I don't want to use int return types everywhere.
- Why do it force me to have 'int' return type on waitForAMessageFromServerAndSendConfirmation(unsigned long long) function in my header file
- Why do it force me to have 'int' return types on all functions called after that one.
When a function is declared as void
it means the function will not return anything.
If you use a return statement with a value in such a function, e.g. return 1;
, you attempt to have it return something. But it is defined as not to return something.
Declaring a funcion as void
has two intentions:
Tell the user of the function not to expect a return value.
Tell the author of the function not to return anything. If the author wants to communicate something, he should do that using other means (e.g. have a pointer argument to a variable of the caller in which to place a result value, or adapt the function signature to return something other than void
).
For example:
void myFunc1(int*result) {
*result= 1; // put value 1 in callers variable
// there is no return statement that returns a value
}
void myFunc2(int*result) {
*result= 1; // put value 1 in callers variable
return; // leave the function. There is no value to return.
}
or:
int myFunc(int in) {
return 2*in; // leave the function and return a value.
}
int confirmation = waitForAMessageFromServerAndSendConfirmation(12345678901LL);
The above says to expect the function to return something and to assign that to variable conformation
. However,
void waitForAMessageFromServerAndSendConfirmation(unsigned long long args) {
//....
return NULL;
says the function will not return anything, but you try to let it return something.
because of these contradictions, your compiler complains.