C Functions

As we know C Programming Language is function oriented programming language, functions are one of the key building block of any c program. Function is way to wrap up the set of statements to perform the specific task and give it a name, so that it can be invoked from any where in the program.

Function in C Programming language give us a way to divide the complete program into subunits that perform a specific task for that program, this way it enhance the modular approach and increase the re usability of the program.

We pass information in function call as its parameter and the function can either returns some value to the point it where it called from or returns nothing.

Advantages of function

  • It enhance the modularity of the program.
  • It enhance the re usability.
  • It makes development easy as development can be shared in team.
  • It reduces the coupling.
  • It reduces duplication.

Type Of Function –

  • Built in function
  • User defined functions

Built In Function In C-

Built in functions are the functions available in C Programming Language to perform some common and standard tasks, this functions are available as C library. These libraries includes the functions for file access, mathematical computations, graphics, memory management etc.
A built in function can be accessed simply by including the relative header file and at point of function call by just writing the function name, followed by an optional list of arguments.

User defined functions In C –

User defined function are custom function defined by user it self to perform as custom task that is not available as built in, in this way user can define and write subprograms as functions to perform a task relevant to their programs.

Function Type Based on Structure of function

  • Functions with no arguments and no return value.
  • Functions with arguments but no return value.
  • Function with no arguments but with return value.
  • Functions with arguments and with return value.

Functions with no arguments and no return value in C

Syntax:

Functions with arguments and no return value in C

Syntax:

Function with no arguments but with return value in C

Syntax:

Functions with arguments and with return value in C

Syntax:

Structure of a Function

There are two main parts of the function. The function header and the function body.

Function Header

In the first line of the above code

It has three main parts –

  1. The name of the function
  2. The parameters of the function enclosed in parenthesis
  3. Return value type

Function Body

What ever is written with in { } in the above example is the body of the function.

Function Prototype or Function Declaration

A function prototype or declaration is made by declaring the return type of the function, name of the function and the data types of the parameters of the function. A function declaration is same as the declaration of the variable. The function declaration is always terminated by the semicolon. A call to the function cannot be made unless it is declared. The general form of the declaration is:-

Syntax:

Example:

The variables name need not be same as the variables of parameter list of the function. Another method can be

The variables in the function declaration can be optional but data types are necessary.

Function in Action

Below is program to swap two number with the use of function –

Output: