Header Ads Widget

Functions in C Programming

In this article you will be introduced to functions (Both standard library functions and user defined functions) in C programming. And also you will learn why functions are used in C programming.
  Generally a difficult problem is divided into sub-problems and then solved. This technique is implemented in C through functions. A program can be divided into one or more functions each of which performs some specific task. So a function is block of statements that performs a specific task. By using function, we can avoid repetition of same code. Functions can be stored in library and re-usability can be achieved.

Types of Functions

1. Standard library functions
2. User defined functions

Standard Library function (Predefined)

Library function is present in C library. To use a library function we have to include corresponding header file using preprocessor directive. Source code of library function is not given to the user. These functions are precompiled and user gets only object code. This object code is linked with object code of our program by linker. To use a library function in our program we should know -
1. Name of function and its purpose
2. Name of header file to be included
3. Types and number of arguments accepted by function
4. Type of value returned by that function.
This is screenshot of C program to find the square root of a number.

In above program sqrt() accepts one argument of floating point type.
Predefined function sqrt() is present in C library. To use this function we have to include corresponding header file with preprocessor directive  #include <math.h> .

User defined Function

User can create their own function for performing any specific task. To create and use this function user should know -

1. Function definition -

  return_type  function_name(type arg1, type arg2, ...)
  {
    local variable declarations;
    statement;
    ......
    return(expression);
  }
Function definition consists of whole description and code of a function. It tells what the function is doing and what are its inputs and outputs. The return type is optional and by default it assumed to be int. If a function does not return any value, then void should be written in place of return_type. Function name should be any valid C identifier. Arguments declared in parentheses are known as formal arguments. Formal arguments are also used as local variables inside that function. If there are no arguments then either the parentheses can be left empty or void can be written inside the parentheses. A function can not be defined in other function.

2. Function declaration -

  return_type  function_name(type arg1, type arg2, ...);
It informs the compiler name of function, number and type of argument received by function and type of value returned by function.

3. Function call -

  function_name(arg1, arg2, ...);  or
  printf("control string",function_name(arg1, arg2, ...));
A function is called by simply writing its name followed by argument list inside parentheses. A function can be called any number of times. When a function is called, the control passes to the called function, which is executed and after this the control is transferred to the statement following the function call in calling function.



Post a Comment

2 Comments