What is function, Types of functions and example of functions.

In simple word function is a self-contained sub-program that are designed to perform some specific task. Any C program contain more than one functions. If there exist only one function in a C program then it must be main(). main() is a function from where every C program have to be start or we can say it is the initial point from where every program have to be started.

If a C program contain functions (excluded main function) then that functions can be called directly or indirectly from the main().

Whenever a call or create a function we pass some values to that function and that function perform their specific task and it returns back some value to the main() function.

Advantages of using Functions

A function has following advantages
  1. A large program can be divided into sub-programs and sub-programs easily understandable. A program can be divided into functions, each of which perform some specific task. thus the function is used to divides the work of a program.
  2. As I said sub-programs easily understandable so that's why it is easily modified and easy to debug and tested. It becomes simple to write the program and understand what task is completed by each part of the program.
  3. Function follow the rule of Abstraction because whenever we create a function the details of how the work is done is hidden inside the function body.
  4. Functions can be stored in a library and can be reused.

Types of Functions

C program have two types of function.
  1. Library functions.
  2. User-defined functions.
Library Functions

C program has a service or we can say facility which provide library functions for doing some operations like scanf(), printf() etc. these functions  are presented in the C library and they are predefined which means we can not modify or delete that functions. therefore to use the library function in our C program we have to include to corresponding header file using the preprocessor directive  that is #include.
For example if we want to use printf() function then we have to include the header file #include<stdio.h>

A simple program using library function

Program: Write a program to find the square root of a number?

#include<stdio.h>
#include<math.h>
int main(void)
{
    double num, sqrroot;
    printf("Enter a number : ");
    scanf("%lf", &num);
    sqrroot=sqrt(num);
    printf("Square root of %.2lf is %2lf\n", num, sqrroot");
    return 0;
}

Output

Enter a number : 16
Square root of 16.00 is 4.00

Ok in this program we have used basically three functions that are Library Function.
  1. printf();
  2. scanf();
  3. sqrt();
User-Defined Functions

In a C program a programmer create a function to perform some specific task is called user-defined functions. User-defined functions has no type that are task specific which can be modified by the programmer.
For example if we need to calculate the sum of two number more than times them we can create a function which perform addition of two number several times in program hence creating a function is useful because it save our time space and it is also easy to understand whenever we need to perform addition of two number then we have to just call it and left the addition action on that function. When a function is called sometimes it takes some value that is arguments and returns the result to the calling function.
Lets check a simple demonstration of an user-defined function

Program: Write a program to find out sum of two numbers.

#include<stdio.h>
int main(void)
{
    double num1, num2, sum;
    printf("Enter any two numbers : ");
    scanf("%d%d", &num1, &num2);
    sum=addtion(num1, num2);  //Function call
    printf("Sum of %d and %d is : %d", num1, num2, sum);
    return 0;
}

int addtion(int a, int b)  //Function body or Function definition
{
    int sum;
    sum = a+b;
    return sum;  //Return the sum
}

Output

Enter any two number : 10  20
Sum of 10 and 20 is : 30

Function Definition

Function definition contain the whole code and description of a function. It describe the whole functionality of the function, what task is to be done by the function which parameters it has to be take and what type of result it will returns to the calling function.
Normally a function definition is combined into two parts

  1. A function header.
  2. Function body.
Following is the general syntax of function.
return_type func_name(parameters declaration)
{
    local variable declaration;
    statements;
    --------------------------
    --------------------------
    return (expression);
}

In above code the first line is known as function header, after function header next whole part is function body which is written enclosed in curly braces.

The return_type means the type of value which will be returned by the function. A function can return either one value or no value. If the return type of function is no value means if a function that does not return any value then void should be written in place of return type. If no return type specified in the definition then int return type is assumed.

func_name specify the name of the function and it can be any valid C identifier. After the function name a comma separated list of parameters declaration is given inside parentheses which define the type and name of the parameter.These parameters are also called formal parameters and used to accept the values from the calling function.A function can have any number of parameters even no parameters at all.

The body of function is a block which consist of declarations of variables, and C statements followed by additional optional return statement. The variable which are declared inside the function is known as local variables which means the existence of that variable is only for that function. Local variable can not be accessed directly outside the function.

The function definition can be placed anywhere in the program or function definition can also be placed in different source file. But generally all definitions are placed after the main() function. A function definition can not be placed inside any function. 

Post a Comment Blogger

 
Top