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 […]
  • Python DictionaryPython DictionaryWe also have a Python dictionary that you may use since once you master it, a whole universe of ultra-cool will be at your disposal. The dictionary is the greatest storage device ever. In […]
  • Product Costing in SAPProduct Costing in SAP OverviewProduct costing is one of the important tools in SAP environment. It explains how to determine the cost of product or a unit. Product Costing is used to know the unit cost of the goods […]
  • SAP FICO Interview QuestionsSAP FICO Interview Questions and Answers – TechnosapDear Readers. we can list of SAP FICO interview questions that have been designed for SAP FICO programmers who are preparing interviews on SAP FICO interviews. Here, we have added some […]
  • Spring TutorialSpring Tutorial – Spring Framework TutorialsWhat does Spring mean? Although Spring was initially designed to make Java Enterprise Edition (JEE) programming simpler, it's not quite a straightforward framework. It's very large. This […]
  • System MonitoringSAP System Monitoring Transaction CodesSystem Monitoring The Following are the transaction codes SM12:  This Traction is used for Check for Lock Entries. There may be old locks still in place from transactions that did not […]
  • Clearing in SAPWhat is Clearing in SAP?Clearing in SAP refers to squaring-off open debit entries with that of open credit entries. Clearing is allowed in GL accounts maintained on an ‘open item’ basis and in all customer/vendor […]
  • SAP MM TutorialSAP MM Tutorial – Learn SAP Material Management OnlineSAP MM Tutorial - To learn some of the free SAP MM (Material Management) topics and how to deal material management and inventory management. SAP MM stands for Material Management and […]