How C Programming Works

Functions in C

Most computer programming languages allow you to create functions of some sort. Functions let you chop up a long program into named sections so that you can reuse those sections throughout the program. Programmers for some languages, especially those using object-oriented programming techniques, use the term method instead of function.

Functions accept parameters and return a result. The block of code that comprises a function is its function definition. The following is the basic structure of a function definition:

<return type> <function name>(<parameters>)



return <value appropriate for the return type>;


At a minimum, a C program has one function named main. The compiler will look for a main function as the starting point for the program, even if the main function calls other functions within it. The following is the main we saw in the simple C program we looked at before. It has a return type of integer, takes no parameters, and has two statements (instructions within the function), one of which is its return statement:

int main()


printf("This is output from my first program!\n");

return 0;


Functions other than main have a definition and one or more function calls. A function call is a statement or part of a statement within another function. The function call names the function it's calling followed by parentheses. If the function has parameters, the function call must include corresponding values to match those parameters. This additional part of the function call is called passing parameters to the function.

But what are parameters? A parameter for a function is a piece of data of a certain data type that the function requires to do its work. Functions in C can accept an unlimited number of parameters, sometimes called arguments. Each parameter added to a function definition must specify two things: its data type and its variable name within the function block. Multiple parameters are be separated by a comma. In the following function, there are two parameters, both integers:

int doubleAndAdd(int a, int b)


return ((2*a)+(2*b));


Next, let's continue our look at functions by zooming out to look at how they fit within a larger C program.