Contents
Functions in C programming
Concept of function
A function is a self-contained program segment or sub-program that performs some well-defined task. The C program consists of one or more functions. The execution of each function begins as a sequence of function call appears in the main() function. If we declare a function once we can call it from a different place many times. After execution of that function, the control will return to the point from where the function was called.
<>
Advantage of function
- It facilitates a top-down programming approach to solve a problem. In this programming style, the high-level logic of the overall problem is solved first while the details of each lower-level function are addressed later.
- Program development becomes easier and faster.
- The function reduces program complexity and increases the readability of the program.
- The function increases code reusability by avoiding rewriting of same code again and again.
- The length of the program can be reduced by using functions at appropriate places.
- The function helps us to understand the program logic involved in the program.
- Recursive call is possible through function.
- The function can be stored in a library and reusability can be achieved.
Types of Function
In the C program, we can use types of functions. They are as:
- Library function
- User-defined function
a. Library Function
Library functions are readymade expressions or built-in formulas used to
perform any programming task quickly. C has the facility to provide library
functions for performing some operations. These functions are present in the
C library and they are predefined. We only used these functions in our
program by including the respective header files. For example, sqrt() is a mathematical library function that is used for finding out the square
root of any number. The function scanf() and
printf() are input/ output function defined in stdio.h header
file.
To use the library function in our program we should know:
- Name of function and its purpose.
- Types and number of arguments it accepts.
- Type of the value it returns.
- Name of the header file to be included.
// program to find the square root of any numbver using library function in C programming #include <stdio.h> #include <conio.h> #include <math.h> void main(){ float n, s; printf("enter the number:"); scanf("%f", &n); s = sqrt(n); printf("The square root of %0.2f is %0.2f", n, s); getch(); }
b. User Defined Function
User-defined functions also known as customized functions are declared and
defined by the user or programmer. Users can create their own functions for
performing any specific task of the program. To create and use these
functions, we should know about these three things:
- Function definition
- Function declaration
- Function call
i. Function definition
A function definition consists of the whole description and code of a
function. It tells what the function is doing and what are its outputs. A
function definition consists of two parts: a function header and a function
body.
Syntax:
return_type function_name (type1 arg1, type2 arg2, ...){ local variables; statements; ..........; return (expression); }
ii. Function prototype or function declaration
The function prototype is a declaration statement that specifies the
function name, the number and types of arguments, and the return types of
function. The prototype is only needed if the function definition comes
after the main() program. The function declaration is the same
as what we write on the first line in the function definition.
Syntax:
return_type function_name (type1 arg1, type2 arg2, ...)
For example:
int product (int a, int b, int c); float sum (int x, int y, int z); void name(char name[]);
iii. Function call
When a function is called its code gets executed. Function call specifying
its name followed by a list of arguments enclosed in parentheses and
separated by commas. While calling the function we pass actual arguments.
The arguments which are passed in the function call are known as actual
arguments. Since these are the values that are actually sent to the called
function. Actual arguments can be written in the form of variables,
constants or expressions, or any function call that returns a value. The
name of the arguments, which are mentions in the function definition is
called formal or dummy arguments since they are used just to hold the values
that are sent by the calling function.
Example of a function call:
s=sum (a,b); printf("the sum is = %d", sum(20, 60)); p=product (x+y, z);
Difference Between library function and user-defined functions
Library Function | User-defined Function |
Library functions are predefined functions in the C compiler. | A user-defined function is defined by the user to perform a specific task. |
This function requires a header file. | This function requires a function prototype to use. |
No need to declare and define the function to use the library function. | This function required function declaration and definition of function before being used it. |
This function saves us time and is easy to use. | This takes more time than the library function. |
Example: strlen();, strcat();, sqrt();, strcmp();, etc. |
Example: sum (int a, int b);, prime (int a);, product (int a, int b);, etc. |