Function in C

A function in C is a block of statements that has a name and can be executed by calling it from some other place in your program. functions are used to divide complicated programs into manageable pieces. Using functions has several advantages:

  • Different people can work on different functions simultaneously.
  • Using functions greatly enhances the program’s readability because it reduces the complexity of the function main.
  • If a function is needed in more than once, you can write it once and use it many times.

There are both library functions, functions that are already written and provided by C, and user-defined functions, functions that you create. Because C does not provide every function that you will ever need, you must learn to write your own functions. User-defined functions in C are classified into two categories:

  • void functions – functions that do not have a return data type. These functions do not use a return statement to return a value.
  • Value-returning functions – functions that have a return data type. These functions return a value of a specific data type using the return statement.

Creating User-Defined Functions

Declare the function:

The declaration, called the function prototype, informs the compiler about the functions to be used in a program, the argument they take and the type of value they return.

Define the function:

The function definition tells the compiler what task the function will be performing. The function prototype and the function definition must be same on the return type, the name, and the parameters. The only difference between the function prototype and the function header is a semicolon. The function definition consists of the function header and its body. The header is EXACTLY like the function prototype, EXCEPT that it contains NO terminating semicolon.

/* This program illustrates how to define
   and call a function */

#include <stdio.h>

void printline(); /* prototype */

int main()
{
    printf("Statement 1\n");
    printline();
    printf("Statement 2\n");
    printline();

    return 0;
}

/* function definition */
void printline()
{
    int i;
    for (i = 0; i < 30; i++)
    {
        printf("_");
    }
    printf("\n");
}
Output

Argument to a function

Sometimes the calling function supplies some values to the called function. These are known as parameters. The variables which supply the values to a calling function called actual parameters. The variable which receive the value from called statement are termed formal parameters.
Consider the following example that evaluates the area of a circle.

Here radius is called actual parameter and r is called formal parameter.

/* This program illustrates actual and 
formal parameter of function */

#include <stdio.h>

void print_area(float); /* prototype */

int main()
{
    float radius;
    printf("Enter radius of circle: ");
    scanf("%f", &radius);
    print_area(radius);

    return 0;
}

/* function definition */
void print_area(float r)
{
    float area;
    area = 3.14 * r * r;
    printf("The area of circle is %f\n", area);
}
Output

Return type of a Function

To let a function return a value, use the return statement. Here is an example of value retuning function that takes two parameters length and width and returns the area of rectangle.

/* This programs demonstratres value returning function */

#include <stdio.h>

int area_rectangle(int, int);

int main()
{
    int l, b, area;
    printf("Enter the length : ");
    scanf("%d", &l);

    printf("Enter the width : ");
    scanf("%d", &b);

    area = area_rectangle(l, b);

    printf("The area of the rectangle : %d", area);

    return 0;
}

int area_rectangle(int length, int width)
{
    return length * width;
}

Output

Global Variable And Local Variable

Local Variable : Variables declared within a function, become local to the function. Local variables have local scope: They can only be accessed within the function. Local variables are created when a method starts, and deleted when the method is completed.

Global Variable : a variable that is declared outside any function is known as a global variable. The scope of such a variable extends till the end of the program. these variables are available to all functions which follow their declaration. So it should be defined at the beginning, before any function is defined.

Unary Scope Resolution Operator (::)

It is possible to declare local and global variables of the same name. C++ provides the unary scope resolution operator (::) to access a global variable when a local variable of the same name is in scope. A global variable can be accessed directly without the unary scope resolution operator if the name of the global variable is not the same as that of a local variable in scope.

Static variables

Static variables are declared by writing keyword static in front of the declaration. If a static variable is not initialized then it is automatically initialized to 0. A static variable is initialized only once and its value is retained between function calls.

/* This program illustrates working of static variable */

#include <stdio.h>

void fun();

int main()
{
    fun();
    fun();
    fun();

    return 0;
}

void fun()
{
    static int a = 10;
    printf("a = %d\n", a);
    a++;
}

Output

Variables and storage Class

The storage class of a variable determines which parts of a program can access it and how long it stays in existence. The storage class can be classified as automatic register static external
Automatic variable:
All variables by default are auto i.e. the declarations int a and auto int a are equivalent. Auto variables retain their scope till the end of the function in which they are defined. An automatic variable is not created until the function in which it defined is called. When the function exits and control is returned to the calling program, the variables are destroyed and their values are lost. The name automatic is used because the variables are automatically created when a function is called and automatically destroyed when it returns.
Register variable:
A register declaration is an auto declaration. A register variable has all the characteristics of an auto variable. The difference is that register variable provides fast access as they are stored inside CPU registers rather than in memory.
Static variable:
A static variable has the visibility of a local variable but the lifetime of an external variable. Thus it is visible only inside the function in which it is defined, but it remains in existence for the life of the program.
External variable:
A large program may be written by a number of persons in different files. A variable declared global in one file will not be available to a function in another file. Such a variable, if required by functions in both the files, should be declared global in one file and at the same time declared external in the second file.

Leave a Comment

Your email address will not be published.