9.6 – Functions putting all together

by subbu on November 17, 2013

Why functions?

  • To develop a complex and big application, the total application has to divide into small and easily manageable parts called modules.
  • A module is a part or unit of total application.
  • In C language a module is defined using function.
  • C language supports modularity using functions so is called modular programming language.

What is a function?

Function is a self-contained block of code that performs a particular task. It is capable to communicate with other units of application.

Advantages of functions:

  • A complex application can be divided into number of simple and easily manageable parts called functions.
  • It is easy to develop, test and debug a small function.
  • It improves the maintainability of application because a problem (bug) in a function can be easily detected and modified.
  • It improves the re-usability because the same function can be reused in different places.
  • It reduces the application development time because multiple modules can be developed at a time and then integrated as a single application.
  • It reduces the required memory to run the application.

Functional parts of a function

Defining and using of any function required three things.

  1. Function definition.
  2. Function calling statement.
  3. Function declaration.

Function definition:

  • Any function has a name, which must be a valid identifier.
  • List of formal arguments are defined, the values of which are assigned by the actual arguments of calling statement.
  • Body of the function is defined with in { }
  • “return” is the statement used to return maximum a single value to the calling function.
  • “return” is also used to terminate the execution of a function.
  • Return type is specified if the function returns a non-integer, void must be specified if a function doesn’t return any value.

Function definition ANSI way

Example:

float adding(float x,float y)
{
  return x+y;
}

Here in this example, the function adding() accepts two float type of values as arguments and return the sum of them as float type.

ANSI vs. K&R way of defining a function:

The first official specification to C language is written by Brian W. Kernighan and Dennis M.Ritchie who is the creator of C language. It is generally known as K&R C.

K&R C way of function definition is,

Function definition K&R way

Here formal arguments are listed in the parenthesis but, declared as a separate statement. The function adding() in K&R style can be defined as

float adding(x,y)
float x,y;                /* declared in a separate statement */
{
  return x+y;
}

Note: Almost all the compilers support both ANSI and K&R style of formal arguments declaration but, most of the programmers follow the ANSI way.

Function calling statement:

  • It is the statement initiates the execution of a function.
  • List of actual arguments are specified to send arguments to the function definition.
  • Here the type, number and sequence of actual arguments must be equal to the type, number and sequence of formal arguments.
  • The calling statement is assigned to the (l-value) variable if the function returns a value.

Function calling statement

Example:

c=adding(a,b);

Here, adding(a,b) is the function calling statement, sends two float type numbers as arguments to the function and assigns the sum of them to the variable c which would be returned by the function.

Function declaration statement or prototype:

  • By default C compiler considers arguments and return type as integers.
  • If we use other types as arguments and return value then the behavior of function must be informed to the compiler by writing prototype.
  • Prototype is also called function declaration statement can be defined either before or within the calling statement.

Prototype or declaration statement

Example:

float adding(float,float);

It informs and improves the knowledge of compiler regarding to the function adding()

So far we have been using words like calling function, called by function, return type, prototype, calling statement, actual arguments and formal arguments etc. The following picture explains what is what.

complete c program

Types of functions

Functions are of two type

  1. Predefined function
  2. User defined functions

Predefined functions:

  • Functions such as printf(), scanf(), getch(), clrscr(), sqrt() and pow() etc. are pre-defined functions also called library functions.
  • These are ready to use functions available as resource with every C compiler.
  • Available functions vary compiler to compiler.
  • Here it is enough to know how to use a function, no need to know the inner implementation.
  • We no need to write prototype and function definition to these functions.
  • These functions are organized in different header files according to the category of functions.
  • Proper header file is added to the program according to the requirement, using a pre-processor statement #include
  • New functions can be added to existed header files
#include<stdio.h>

It imports commonly used standard input output functions into the program

#include<math.h>

It imports mathematical functions into the program

User defined functions:

  • Functions we define according to our requirement within a program are called user defined functions.
  • Every user defined function need function definition, prototype and function calling statement.
  • main() is a user defined function because in every program main() has to write (define) by the user

main() is a user defined function

Previous post:

Next post: