Thread: Function Call

  1. #1
    Registered User
    Join Date
    Jun 2017
    Posts
    14

    Function Call

    Hello,

    Below is a sample data acquisition code. It compiles fine. I would like to make it a function and call it within another main function (program). So I did this:

    int main(void) ---> void DAQ()

    It gives me bunch of errors. Can anyone please tell me where my mistake is?

    Cheers



    Code:
    int main(void)
    {
    	int32       error=0;
    	TaskHandle  taskHandle=0;
    	int32       read;
    	float64     data[1000];
    	char        errBuff[2048]={'\0'};
    
    
    	/*********************************************/
    	// DAQmx Configure Code
    	/*********************************************/
    	DAQmxErrChk (DAQmxCreateTask("",&taskHandle));
    	DAQmxErrChk (DAQmxCreateAIVoltageChan(taskHandle,"Dev1/ai0","",DAQmx_Val_Cfg_Default,-10.0,10.0,DAQmx_Val_Volts,NULL));
    	DAQmxErrChk (DAQmxCfgSampClkTiming(taskHandle,"/Dev1/PFI7",1000.0,DAQmx_Val_Rising,DAQmx_Val_FiniteSamps,1000));
    
    
    	/*********************************************/
    	// DAQmx Start Code
    	/*********************************************/
    	DAQmxErrChk (DAQmxStartTask(taskHandle));
    
    
    	/*********************************************/
    	// DAQmx Read Code
    	/*********************************************/
    	DAQmxErrChk (DAQmxReadAnalogF64(taskHandle,-1,10.0,DAQmx_Val_GroupByChannel,data,1000,&read,NULL));
    
    
    	if( read>0 )
    		printf("Acquired %d samples\n",(int)read);
    
    
    Error:
    	if( DAQmxFailed(error) )
    		DAQmxGetExtendedErrorInfo(errBuff,2048);
    	if( taskHandle!=0 ) {
    		/*********************************************/
    		// DAQmx Stop Code
    		/*********************************************/
    		DAQmxStopTask(taskHandle);
    		DAQmxClearTask(taskHandle);
    	}
    	if( DAQmxFailed(error) )
    		printf("DAQmx Error: %s\n",errBuff);
    	printf("End of program, press Enter key to quit\n");
    	getchar();
    	return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I don't see where you call this DAQ function in the main function. What are the error messages?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2017
    Posts
    14
    The main function is quite long, therefore I just pasted the function here. Even though you dont call a function in the main function, it still should compile fine. I am using gcc compiler on windows. My errors are as follows:

    -------------------------------------------------------------------------------------------------
    Main2.c:320:2: error: unknown type name 'int32'
    int32 error=0;
    ^
    Main2.c:321:2: error: unknown type name 'TaskHandle'
    TaskHandle taskHandle=0;
    ^
    Main2.c:322:2: error: unknown type name 'int32'
    int32 read;
    ^
    Main2.c:323:2: error: unknown type name 'float64'
    float64 data[1000];
    ^
    Main2.c:329:2: warning: implicit declaration of function 'DAQmxErrChk' [-Wimplicit-function-declaration]
    DAQmxErrChk (DAQmxCreateTask("",&taskHandle));
    ^
    Main2.c:329:15: warning: implicit declaration of function 'DAQmxCreateTask' [-Wimplicit-function-declaration]
    DAQmxErrChk (DAQmxCreateTask("",&taskHandle));
    ^
    Main2.c:330:15: warning: implicit declaration of function 'DAQmxCreateAIVoltageChan' [-Wimplicit-function-declaration]
    DAQmxErrChk (DAQmxCreateAIVoltageChan(taskHandle,"Dev1/ai0","",DAQmx_Val_Cfg_Default,-10.0,10.0,DAQmx_Val_Volts,NULL));
    ^
    Main2.c:330:65: error: 'DAQmx_Val_Cfg_Default' undeclared (first use in this function)
    DAQmxErrChk (DAQmxCreateAIVoltageChan(taskHandle,"Dev1/ai0","",DAQmx_Val_Cfg_Default,-10.0,10.0,DAQmx_Val_Volts,NULL));
    ^
    Main2.c:330:65: note: each undeclared identifier is reported only once for each function it appears in
    Main2.c:330:98: error: 'DAQmx_Val_Volts' undeclared (first use in this function)
    DAQmxErrChk (DAQmxCreateAIVoltageChan(taskHandle,"Dev1/ai0","",DAQmx_Val_Cfg_Default,-10.0,10.0,DAQmx_Val_Volts,NULL));
    ^
    Main2.c:331:15: warning: implicit declaration of function 'DAQmxCfgSampClkTiming' [-Wimplicit-function-declaration]
    DAQmxErrChk (DAQmxCfgSampClkTiming(taskHandle,"",10000.0,DAQmx _Val_Rising,DAQmx_Val_FiniteSamps,1000));
    ^
    Main2.c:331:59: error: 'DAQmx_Val_Rising' undeclared (first use in this function)
    DAQmxErrChk (DAQmxCfgSampClkTiming(taskHandle,"",10000.0,DAQmx _Val_Rising,DAQmx_Val_FiniteSamps,1000));
    ^
    Main2.c:331:76: error: 'DAQmx_Val_FiniteSamps' undeclared (first use in this function)
    DAQmxErrChk (DAQmxCfgSampClkTiming(taskHandle,"",10000.0,DAQmx _Val_Rising,DAQmx_Val_FiniteSamps,1000));
    ^
    Main2.c:332:15: warning: implicit declaration of function 'DAQmxCfgAnlgEdgeStartTrig' [-Wimplicit-function-declaration]
    DAQmxErrChk (DAQmxCfgAnlgEdgeStartTrig(taskHandle,"APFI0",DAQm x_Val_Rising,1.0));
    ^
    Main2.c:333:15: warning: implicit declaration of function 'DAQmxSetAnlgEdgeStartTrigHyst' [-Wimplicit-function-declaration]
    DAQmxErrChk (DAQmxSetAnlgEdgeStartTrigHyst(taskHandle, 1.0));
    ^
    Main2.c:338:15: warning: implicit declaration of function 'DAQmxStartTask' [-Wimplicit-function-declaration]
    DAQmxErrChk (DAQmxStartTask(taskHandle));
    ^
    Main2.c:343:15: warning: implicit declaration of function 'DAQmxReadAnalogF64' [-Wimplicit-function-declaration]
    DAQmxErrChk (DAQmxReadAnalogF64(taskHandle,-1,10.0,DAQmx_Val_GroupByChannel,data,1000,&read,NU LL));
    ^
    Main2.c:343:53: error: 'DAQmx_Val_GroupByChannel' undeclared (first use in this function)
    DAQmxErrChk (DAQmxReadAnalogF64(taskHandle,-1,10.0,DAQmx_Val_GroupByChannel,data,1000,&read,NU LL));
    ^
    Main2.c:349:6: warning: implicit declaration of function 'DAQmxFailed' [-Wimplicit-function-declaration]
    if( DAQmxFailed(error) )
    ^
    Main2.c:350:3: warning: implicit declaration of function 'DAQmxGetExtendedErrorInfo' [-Wimplicit-function-declaration]
    DAQmxGetExtendedErrorInfo(errBuff,2048);
    ^
    Main2.c:355:3: warning: implicit declaration of function 'DAQmxStopTask' [-Wimplicit-function-declaration]
    DAQmxStopTask(taskHandle);
    ^
    Main2.c:356:3: warning: implicit declaration of function 'DAQmxClearTask' [-Wimplicit-function-declaration]
    DAQmxClearTask(taskHandle);
    ^
    Main2.c:362:9: warning: 'return' with a value, in function returning void
    return 0;
    ^
    ---------------------------------------------------------------------------------------------------------------------------------------------------

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I'm confused: you said that originally, "it compiles fine", but this is an error that is unrelated to moving code into a function:
    Code:
    Main2.c:320:2: error: unknown type name 'int32'
    int32 error=0;
    Perhaps the mistake is that when moving the code fragment into a function, you somehow deleted or misplaced the header file inclusion that declares the types such as int32.

    Likewise, this looks like a mistake related to a missing header inclusion:
    Code:
    Main2.c:329:2: warning: implicit declaration of function 'DAQmxErrChk' [-Wimplicit-function-declaration]
    DAQmxErrChk (DAQmxCreateTask("",&taskHandle));
    Presumably once you include the correct header at the right place, this warning will vanish.

    This looks like a copy/paste error:
    Code:
    Main2.c:329:2: warning: implicit declaration of function 'DAQmxErrChk' [-Wimplicit-function-declaration]
    DAQmxErrChk (DAQmxCreateTask("",&taskHandle));
    In the main function, you would return 0 since main is declared with a return type of int, but which moving code out of the main function, you cannot blindly retain this return statement when the new function has a return type of void.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jun 2017
    Posts
    14
    Ohh thanks, you were right, I was missing the header file, forgot to add it. My errors reduced to:

    --------------------------------------------------------------------------------------------------------------------------------------
    Main2.c: In function 'NIDAQ':
    Main2.c:330:2: warning: implicit declaration of function 'DAQmxErrChk' [-Wimplicit-function-declaration]
    DAQmxErrChk (DAQmxCreateTask("",&taskHandle));
    ^
    Main2.c:361:9: warning: 'return' with a value, in function returning void
    return 0;
    ^
    C:\Users\cyln~1\AppData\Local\Temp\cciX7oIt.o:Main 2.c.text+0x1231): undefined reference to `DAQmxErrChk'
    C:\Users\cyln~1\AppData\Local\Temp\cciX7oIt.o:Main 2.c.text+0x1283): undefined reference to `DAQmxErrChk'
    C:\Users\cyln~1\AppData\Local\Temp\cciX7oIt.o:Main 2.c.text+0x12cb): undefined reference to `DAQmxErrChk'
    C:\Users\cyln~1\AppData\Local\Temp\cciX7oIt.o:Main 2.c.text+0x12e1): undefined reference to `DAQmxErrChk'
    C:\Users\cyln~1\AppData\Local\Temp\cciX7oIt.o:Main 2.c.text+0x1332): undefined reference to `DAQmxErrChk'
    collect2.exe: error: ld returned 1 exit status
    -------------------------------------------------------------------------------------------------------------------------------------------------------------

  6. #6
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    You're missing this after the includes:
    Code:
    #define DAQmxErrChk(functionCall) if( DAQmxFailed(error=(functionCall)) ) goto Error; else
    From Using NI-DAQmx in Text Based Programming Environments

  7. #7
    Registered User
    Join Date
    Jun 2017
    Posts
    14
    Right, that was my second bug. Now I only get one error:

    ---------------------------------------------------------------------------------------------------------------------------
    Main2.c: In function 'NIDAQ':
    Main2.c:362:9: warning: 'return' with a value, in function returning void
    return 0;
    ^
    ---------------------------------------------------------------------------------------------------------------------------

    The issue is, main function calls a dozen of other funtions and most of them have void type. I cannot return 0 in the main function.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Just remove that return statement.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Jun 2017
    Posts
    14
    woww, thank you so much, it worked!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function call Overhead and Function Call Stack
    By Alam Khan in forum C++ Programming
    Replies: 2
    Last Post: 04-26-2014, 08:28 AM
  2. Function Prototype, Function Call, and Function definition
    By dmcarpenter in forum C Programming
    Replies: 9
    Last Post: 04-09-2013, 03:29 AM
  3. Replies: 3
    Last Post: 09-22-2011, 09:59 PM
  4. Replies: 8
    Last Post: 07-08-2011, 01:16 PM
  5. Replies: 5
    Last Post: 10-17-2006, 08:54 AM

Tags for this Thread