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.
- Advantages of function
- Type Of Function –
- User-defined functions In C –
- Function Type Based on Structure of function
- Functions with no arguments and no return value in C
- Functions with arguments and no return value in C
- Function with no arguments but with return value in C
- Functions with arguments and with return value in C
- Structure of a Function
- Function Header
- Function Body
- Function in Action
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:
1 2 3 4 |
function_name () { Function Body } |
Functions with arguments and no return value in C
Syntax:
1 2 3 4 |
function_name(parameter list) { Function Body } |
Function with no arguments but with return value in C
Syntax:
1 2 3 4 5 |
return_type function_name() { Function Body return statement; } |
Functions with arguments and with return value in C
Syntax:
1 2 3 4 5 |
return_type function_name(parameter list) { Function Body return statement } |
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
1 |
return_type function_name(parameter list) |
It has three main parts –
- The name of the function
- The parameters of the function enclosed in parenthesis
- 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:
1 |
return_type function_name(parameter list); |
Example:
1 |
int factorial(int n1,float f1); |
The variable’s name need not be the same as the variables of the parameter list of the function. Another method can be
1 |
int factorial(int , float); |
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 –
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
#include <stdio.h> /* functions declaration */ void swap(int *x, int *y); void main() { int x = 10; int y = 20; printf("x,y before swapping\n"); printf("x = %d\n",x); printf("y = %d\n",y); // using function swap swap(&x,&y); printf("x,y after swapping\n"); printf("x = %d\n",x); printf("y = %d\n",y); } /* functions implementation */ void swap(int *x, int *y){ int temp = *x; *x = *y; *y = temp; } |
Output:
1 2 3 4 5 6 |
x,y before swapping x = 10 y = 20 x,y after swapping x = 20 y = 10 |