Function is a group of statement that perform a particular task. C enables its programmers to break up a program into segments commonly known as functions, each of which can be written more or less independently of the others. Every function in the program is supposed to perform a well-defined task. Therefore, the program code of one function is completely insulated from the other functions.

Every function interfaces to the outside world in terms of how information is transferred to it and how

 results generated by the function are transmitted back from it. This interface is basically specified by the function name.


Function Components

 

Function Declaration / Prototype

Before using a function, the compiler must know the number of parameters and the type of parameters that the function expects to receive and the data type of value that it will return to the calling program. Placing the function declaration statement prior to its use enables the compiler to make a check on the arguments used while calling that function. A function declaration statement has the following syntax:

Example -        return_data_type function_name( argument_type);

                        void display();

                        void max(int,int);

 

Function Calling

The function call statement invokes the function. When a function is invoked, the compiler jumps to the called function to execute the statements that are a part of that function. Once the called function is executed, the program control passes back to the calling function. A function call statement has the following syntax:

Example -        function_name(argument);  // variable , value , address

display();

max(i,j);

 

Function Defination

When a function is defined, space is allocated for that function in the memory. Note that the number of arguments and the order of arguments in the function header must be the same as that given in the function declaration statement. The syntax of a function definition can be given as:

Example -        return_data_type function_name(argument);

 {

            ……….............

            statements

            ….…...............

            return(variable);

 }

Return Type: A function may return a value. The return_type is the data type of the value the function returns. Some functions perform the desired operations without returning a value. In this case, the return_type is the keyword void.

 

Function Name: This is the actual name of the function. The function name and the parameter list together constitute the function signature.

 

Parameters: A parameter is like a placeholder. When a function is invoked, you pass a value to the parameter. This value is referred to as actual parameter or argument. The parameter list refers to the type,

order, and number of the parameters of a function. Parameters are optional; that is, a function may contain no parameters.

 

Function Body: The function body contains a collection of statements

that define what the function does.

 

Type of Function

 

Pre-define function

User-defined function

 

Pre-define function

C standard pre-define function contain file the standard function that can be used some function are already defined. Which are used by programmers these function are called pre-define function. Function are divided into 3 part mathematical, string, character function.

            Example – printf() , scanf()

 

Example of Pre-define function

#include<stdio.h>

#include<conio.h>

void display(){

            printf("Hello");

}

void main(){

            clrscr();

            display();

getch();

}

Output

        Hello


User-defined function

In c user can also define own according to their need when a function is called control is transfer to the first in the function body are the executed in the main program when the closing braces is uncounted.

            Example – Myfun() , Add()

 

Example of User-define function

#include<stdio.h>

#include<conio.h>

void display(){

            printf("Hello");

}

void main(){

            clrscr();

            display();

getch();

}

Output

        Hello


Function Arguments 

If a function is to use arguments, it must declare variables that accept the values of the arguments. These variables are called the formal parameters of the function.

Formal parameters behave like other local variables inside the function and are created upon entry into the function and destroyed upon exit.

While calling a function, there are two ways in which arguments can be passed to a function

        1.  Call By Value

        2.  Call By Address / Reference


Example of function with arguments with return

#include<stdio.h>

#include<conio.h>

int sum(int a,int b){

            int c;

            c=a+b;

            return c;

}

void main(){

            int i,j,k;

            clrscr();

            printf("Enter value of i:");

            scanf("%d",&i);

            printf("Enter value of j:");

            scanf("%d",&j);

            k=sum(i,j);

            printf("Sum=%d",k);

getch();

}

Output

        Enter value of i: 12

        Enter value of j: 12

        Sum=24

    

Example of function without arguments with return

#include<stdio.h>

#include<conio.h>

float pi(){

            return 3.14;

}

void main(){

            int r;

            float area;

            clrscr();

            printf("Enter radius:");

            scanf("%d",&r);

            area=pi()*r*r;

            printf("Area=%f",area);

getch();

}

Output

        Enter radius:10

        Area=314.000000


Call by value 

The values of the variables are passed by the calling function to the called function. In this method, the called function creates new variables to store the value of the arguments passed to it. Therefore, the called function uses a copy of the actual arguments to perform its intended task. If the called function is supposed to modify the value of the parameters passed to it, then the change will be reflected only in the called function. In the calling function, no change will be made to the value of the variables. This is because all the changes are made to the copy of the variables and not to the actual variables.


Example of call by value

#include<stdio.h>

#include<conio.h>

void display(int i){

            printf("%d",i);

            i=20;

}

void main(){

            int i=5;

            clrscr();

            printf("%d",i);

            display(i);

            printf("%d",i);

getch();

}

Output

        5          5          5


Call by address 

When the calling function passes arguments to the called function using the call-by-value method,the only way to return the modified value of the argument to the caller is explicitly using the return statement. A better option is to pass arguments using the call-by-reference technique.In this method, we declare the function parameters as references rather than normal variables.When this is done, any changes made by the function to the arguments it received are also visible in the calling function.To indicate that an argument is passed using call by reference, an asterisk (*) is placed after the type in the parameter list.


Example of call by address

#include<stdio.h>

#include<conio.h>

void display(int *p){

            printf("%d",*p);

            *p=20;

}

void main(){

            int i=5;

            clrscr();

            printf("%d",i);

            display(&i);

            printf("%d",i);

getch();

}

Output

        5          5          20


Recursion  

It is a process in which a function call itself again and again till the particular condition satisfies. it is called a recursive call of the function.

The C programming language supports recursion, i.e., a function to call itself. But while using recursion, programmers need to be careful to define an exit condition from the function, otherwise it will go into an infinite loop.

Recursive functions are very useful to solve many mathematical problems, such as calculating the factorial of a number, generating Fibonacci series, etc.

 

Example of recursion

#include<stdio.h>

#include<conio.h>

void display(){

            printf("\ndisplay");

            display();

}

void main(){

            clrscr();

            printf("main");

            display();

getch();

}

Output

        Main

        Display

        Display

        Infainat loop


Example of Factorial number using recursion 

#include<stdio.h>

#include<conio.h>

int factorial(int n){

            int v;

            if(n==1 || n==0){

                        return 1;

            }

            else{

                        v=n*factorial(n-1);

                        return v;

            }

}

void main(){

            int n,f;

            clrscr();

            printf("Enter number:");

            scanf("%d",&n);

            f=factorial(n);

            printf("\nFactorial of %d is=%d",n,f);

getch();

}          

Output

        Enter number: 4

        Factorial of 4 is =24


Function Scope 

There is 4 types of scope in c …

1. local scope 

When ever a variable is declared in a block of statement then variable and function will be called as local variable and function.

 

2. Function scope 

if a variable is declared then the range of that variable is limited to in that function only that variable can be used only in that function.

 

3. File scope 

If a variable is declared outside the function and block then can be used anywhere in the block or function.

 

4. Class scope 

If a variable is declared in a class then that can be used in the same class only it is called as class scope.