C Functions

In this tutorial you will learn about the C Functions and its application with practical example.

As we know C Programming Language is a function-oriented programming language, functions are one of the key building blocks of any c program. The function is a 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 anywhere in the program.

Function in C Programming language gives us a way to divide the complete program into subunits that perform a specific task for that program, this way it enhances the modular approach and increases the reusability of the program.

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

Advantages of function

  • It enhances the modularity of the program.
  • It enhances the reusability.
  • It makes development easy as development can be shared in a 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, these functions are available in the C library. These libraries include 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 the point of function call by just writing the function name, followed by an optional list of arguments.

User-defined functions In C –

User-defined functions are custom functions defined by the user itself to perform a custom task that is not available as built-in, in this way users 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 the return value.
  • Functions with arguments and with the 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

Whatever is written within { } 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, the name of the function, and the data types of the parameters of the function. A function declaration is the 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 variable’s name need not be the same as the variables of the 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 a program to swap two numbers with the use of the function –

Output:

 

 

 

In this tutorial we have learn about the C Functions and its application with practical example. I hope you will like this tutorial.