Fire Tablet Alexa Display Black

Wednesday, May 9, 2012

Functions in C

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 ;
}
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.

Function Prototypes

In C, you can add a function definition anywhere within the program (except within another function). The only condition is that you must tell the compiler in advance that the function exists somewhere later in the code. You'll do this with a function prototype at the beginning of the program. The prototype is a statement that looks similar to the first line of the definition. In C, you don't have to give the names of the parameters in the prototype, only the data types. The following is what the function prototype would look like for the doubleAndAdd function:
int doubleAndAdd(int, int);
Imagine function prototypes as the packing list for your program. The compiler will unpack and assemble your program just as you might unpack and assemble a new bookshelf. The packing list helps you ensure you have all the pieces you need in the box before you start assembling the bookshelf. The compiler uses the function prototypes in the same way before it starts assembling your program.
If you're following along with the sample.c program we looked at earlier, open and edit the file to add a function prototype, function definition and function call for the doubleAndAdd function shown here. Then, compile and run your program as before to see how the new code works. You can use the following code as a guide to try it out:
#include
int doubleAndAdd(int, int);
int main()
{
printf("This is output from my first program!\n");
printf("If you double then add 2 and 3, the result is: %d \n", doubleAndAdd(2,3));
return 0;
}
int doubleAndAdd(int a, int b)
{
return ((2*a)+(2*b));
}
So far we've looked at some basic structural elements in a C program. Now, let's look at the types of data you can work with in a C program and what operations you can perform on that data.

No comments:

Post a Comment

am glad to view your comment!!