Question:
C programming Simple question about 'function call'. Please help.?
Matt H
2010-03-09 09:05:21 UTC
I basically need to create a program using a function call. I have the "framework" code and I have started writing my code. So, I really don't understand where the functions comes into play in my program.... I have a separate code that has a bunch of pre-written functions that I am suppose to use, do I just copy and paste them?

...it just seems like I can copy and paste the functions code into my program from the other .c file and I just need to combine it with my code and make it work smoothly, does this sound like a correct way to achieve calling a function?
Five answers:
calcprogrammer1
2010-03-09 09:32:24 UTC
In C/C++, you generally use two files together for each code file. You have a .h and a .c (or .cpp if using C++). The .h file stands for header, it doesn't contain any of the code's functionality but provides all of the references to other libraries (other .h files) as well as prototypes any functions used in the associated .c file. The .c file is called the code file, it is where all the actual functionality is written but generally does not prototype variables or state includes (except for one, the .c always has to include its associated .h).



For example, if I had a code with an adding function, this would be the header:



#ifndef ADD_ONE_H //This makes sure that even if you include it multiple times in your code

#define ADD_ONE_H //it only gets included once



#include //this is where you include all the libraries and header files for your project

#include "subtract_one.h" //if including your own files, put them in quotes, not <>'s



using namespace std; //if you are using the standard functions, put this in your header



void addOne(int a); //This is a function prototype, it doesn't have any code yet but will in the c file



#endif //this ends a header file



Ok, now for the .c file, this is where the code goes, it looks like this:



#include "addone.h" //include the header (the stuff above)



void addOne(int a) //note there's no semicolon after this

{

//this is the code for the add one function

cout << (a+1) << endl;

}



Now to use this, in your main file you simply need to include the addone.h file to be able to call the functions in it. For instance:



#include "addone.h"



int main()

{

addone(5); //prints 6 on the screen

return 0;

}



Remember that while you only have to include .h files, you must tell your compiler to compile all the .c or cpp files that your project uses. This varies by compiler, but for gcc:



gcc -o myProgram.o main.c addone.c



where addone.c is the code for the functions you're using and main.c is where your main function is (if using more than one included code file, just keep listing the .c files on the end). There is a shortcut, if all the files are in the same folder, simply do:



gcc -o myProgram *.c



The * operator tells it to compile all files that have a .c extension (or if using C++, do *.cpp for it to compile all c++ code, or if you have both types, gcc *.cpp *.c would work).
Me M
2010-03-09 09:31:23 UTC
In most code, you will have a header file ( with a .h extension) that corresponds to a source file (with a .c extension). If you were given code, you were also probably given header files. In your source file that calls functions, you would include the name of the header file using the #include keyword at the top.

If this is a header file that is given to you and it is not in a system/compiler library, you use quotation marks around the file name. So you would have something like:

#include "filename.h"

at the top of your source file. However, if the header file is in your systems libraries, you use < > brackets around the header file. An example of this is:

#include

You see this in any programs that use common functions like printf. You should not include files in source files that you don't need.



Because it sounds like your code is in different files, you will need to compile each of the source code files. This can be done differently, depending on your compiler. However, if you are using gcc, you will use a command like this:



gcc -o pgmOutputName sourceFile1.c sourceFile2.c sourceFile3.c



However, if the functions you are trying to call are in standard libraries or system libraries (ie: the stdio library that has the printf function), you do not need to list that as one of the source files that needs to be compiled in the gcc command.



I hope I am not confusing you too much.
Chris C
2010-03-09 09:29:45 UTC
The purpose of using a "function call", is so that you can build up a library of routines that you find useful.

So, in an effort to provide programming "tools" (or functions/classes, etc.), you allow them to include the functionality that you've worked through to provide to someone else.



#include

is a way to let anyone else know how they can talk to the code that you've written.

You then only have to provide a library file (i.e. ".lib", ".so", etc.) file that they can link in with the linker. That way, you maintain the rights to the functionality that you've written and don't have to expose all of the inner workings to the users of your library of functions/classes.
thedaza166
2010-03-09 09:11:47 UTC
You do not copy and paste them, you include them in your main programs code. If your function code is in a seperate file you need to look into including files. Search in Google 'C programming include files'



Alternatively you could copy the function into your main program code above the main() and inside main when you want to use it just put the fucntion name like 'thisfunction();' which will send any values you include inside ().
?
2016-10-04 13:35:42 UTC
Did they grant you with header data? you may call purposes from different .c data, you merely ought to enable your one document comprehend of the life of the different document. many times completed with a header document, you will possibly make a header document with the prototypes out of your different document. then you definately do #comprise "header.h" in the two data, this we could your considerable document comprehend of the purposes in the different document. you may then merely call the purposes such as you will possibly in the event that they have been all on your considerable document.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...