C Program to Find Total of Even Integers

Functions in C Programming with example

A function is a module or block of program code which deals with particular task. Making C functions is a way of isolating one block of code from other independent blocks of code. Every C Program consists of one or more files, with each file containing zero or more functions, one of them being a main () function. Functions are defined as individual objects that cannot be nested.

Program execution begins with main (), which can call other functions, including library functions such as printf () and sqrt() Functions operate with program variables, and which of these variables is available at particular place in function is determined by scope rules.

Each function has a name or identifier by which is used to refer to it in a program. A function can accept a number of parameters or values which pass information from outside, and consists of a number of statements and declarations, enclosed by curly braces { }, which make up the doing part of the object. The declarations and ‘type of parameter’ statements are formalities which will be described in good time.

The name of function in C can be anything from a single letter to a long word. The name of a function must begin with an alphabetic letter or the underscore ‘_’ character.

Functions in C Example Code

Here is a real example function which adds together two integer numbers a and b and prints the result c.

Add_Two_Numbers (a,b) /* Add a and b */
int a,b;
{ int c;
c = a + b;
printf ("%d",c);
}

Notice the position of the function name and where braces and semi-colons are placed: they are crucial. The details are quickly learned with practice and experience.

A function is called (i.e. control is passed to the function) by using its name with the usual brackets () to follow it, along with the values which are to be passed to the function:

main ()
{ int c,d;
c = 1;
d = 53;
Add_Two_Numbers (c,d);
Add_Two_Numbers (1,2);
}

The result of this program would be to print out the number 54 and then the number 3 and then stop. Here is a simple program which makes use of some functions in a playful way.

A function definition starts with the type of the function. If no value is returned, then the type is void. If the type is something other than void, then the value returned by the function will be converted, if necessary, to this type. The name of the function is followed by a parenthesized list of parameter declarations.

The function body is a block, or compound statement, and it too may contain declarations. Some examples of function definitions are


void nothing(void) { }
double twice(double x)
{
return (2.0 * x);
}
int all_add(int a, int b)
{
int c;
return (a + b + c);
}

If a function definition does not specify the function type, then it is int by default.

For example, the last function definition could be given by

all_add(int a, int b)
{

However, it is considered good programming practice to specify the function type explicitly.

There are several important reasons to write programs as collections of many C functions. It is simpler to correctly write a small function to do one job. Both the writing and debugging are made easier.

Online Training Tutorials

  • C Program to Find Total of Even IntegersC Tutorial – Learn C Programming Language TutorialLearn C programming is very easy if you follow our popular C tutorial which will take you from the beginning of C programming. This C tutorial is mainly designed for beginners and […]
  • C Program to Find Total of Even IntegersC While Loop with ExampleThe simplest of three loops in C Language is the C while loop. In common language while has fairly obvious meaning: the while-loop has a condition: while […]
  • ERP beginning and its role in business an OverviewIn this article I thought I’ll discuss a bit about an interesting topic that is common to all ERPs.I am certain most of us are aware of what an ERP is and its role in businesses, but have […]
  • SAP SD Interview Questions and AnswersTop SAP SD (Sales and Distribution) Interview Questions and AnswersIf you have finally found your career job in SAP (Sales and Distribution) but are wondering how to crack the SAP SD Interview and what could be the probable SAP SD Interview Questions and […]
  • List of companies using SAP in IndiaThe List of companies using SAP in IndiaAs SAP is ERP solution and one can work as a consultant implementing SAP. If you have a programming background, you can go for technical module and work as technical consultant. we can […]
  • C Program to Print Prime NumbersC Program to Print Prime Numbers up to Given NumberFollowing C program Print Prime Numbers up to Given Number, then display the result on the screen: A for loop is a repetition control structure that allows you to efficiently write a […]
  • rebate processing in sapWhat is Rebate Processing in SAP SD?The rebate processing in sap is a discount given to the customer retroactively. This discount is based on the sales volume over a specific period of time. Rebate can be independent of […]
  • Best ERP SoftwareHow to find Best ERP Software for Small Business?ERP Software: An integrated ERP software system provides functionality which keeps processes, tracks data and makes detailed analysis processes that helps to take quick management […]