Function

Function

Function is set of instructions to carry out a particular task.
A C program has at least one function main( ). Without main() function, there is technically no C program. A function can be called multiple times to provide reusability and modularity to the C program. In other words, we can say that the collection of functions creates a program. The function is also known as procedureor subroutinein other programming languages.

Why we use function in C programming?

To overcome all the difficulties we use Function in C programming.

Tyes of function in C

There are two types of functions in C on basis of whether it is defined by user or not.

  1. Library Function
  2. User defined Function
1)Library Function

Library functions are the pre-defined function in C programming language.

1.main()
2.printf()function
3.scanf() function

There are many library functions available in C programming to help the programmer to write a good efficient program.

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<math.h>

In C programming you can copy string by just using strcpy() function which is defined under header file "string.h".
These are some other library functions defined in "string.h"

2)User defined Functions

C provides programmer to define their own function according to their requirement is known as "user defined functions".

Advantages of user defined functions
Elements of user defined functions:
  1. Function declaration
  2. Function call
  3. Function definition

1.Function declaration

  Syntax:

Funtion_type function_name(parameter list);

A function declaration consists of four parts. They are,

Example:

int add(int a, int b);

2.Function call

Invoking(calling) of a function is called "function call".

  Syntax:

function_name(Parameter name);

Example:

add(a,b);

3.Function definition

The program module that is written to achieve a specific task is called "function definition".

Function Structure

  Syntax:

Function Type Function name (Parameter List)
                {
                    Local declaration;
                    Function statements;
                    Return statements;
                }
Function Header

The function header consists of three parts:
function type,
function name
list of parameter.

Example:

                    1) Float sub (float x, float y)
        
                    2) int mul (int a, int b) 
                    
Function body

Function body is inside the curly braces. It contain Following three parts :

Syntax with Example:

                    int add(int a, int b)
                    {
                    int sum;
                    sum=a+b;
                    return sum;
                    }
                    
Sample program
Addition of two integer numbers using user defined function.
                                    
                #include<stdio.h> 
                #include<conio.h> 
                
                //Function declaration
                int sum(int a, int b);           
                void main()
                {
	                int a,b,x;
                    clrscr();
                    
                    printf("Enter any two number of type integer/n");
                    scanf("%d%d"&a,&b);
                    
                    // Function call
                    x=sum(a,b); 

                    printf("Sum of two numbers=%d",x);
	                getch();
                }
                
                // Function Definition
                int sum(int a, int b)      
                 
                {
               	    int sum;
               	    sum=a+b;
               	    return(sum);
                }

            

Output-

               Enter any two number of type integer
               10
               11
               Sum of two numbers=21
          

Functions Prototypes (Function Declaration)

On the basis of parameters and return values, the functions declaration classified into following types-

  1. Function with no return type and no arguments.
  2. Function with return type but with no arguments.
  3. Function with no return type but with arguments.
  4. Function with return type and with arguments.
1.Function with no return type and no arguments.

In this method, there is no data transfer between the calling function and called function.Return type is void. So this returns nothing.

Sample program
Swap two numbers using user defined function with no argument and no return type
                
                #include<stdio.h> 
                #include<conio.h> 
                
                void swap();

                void main()
                {
                    clrscr();
                    swap();
                    getch();
                }

                void swap()
                {
                    int num1.num2,temp;
                    num1=10;
                    num2=11;
                    temp=num1;
                    num1=num2;
                    num2=temp;
                    printf("num1=%d" and num2=%d,num1,num2);
                }
            

Output-

               num1=11 and num2-10
          
2.Function with return type but with no arguments.

In this method, there is no data transfer between the calling function and called function.Here we won’t pass any arguments to the function while defining, declaring or calling the function.

Sample program
Implement a program to find maximum of two numbers using function (Prototype: no argument, with return type)
                
                #include<stdio.h> 
                #include<conio.h> 
                
                int max();
                void main()

                {
                    int y;
                    clrscr();
                    
                    y=max();
                    printf("max=%d",y);

                    getch();
                }
                int max()
                {
                    int a,b;

                    printf("Enter any two numbers\n");
                    scanf("%d%d",&a,&b);

                    if(a>b)
                    {
                        return(a);
                    }
                    else
                    {
                        return(b);
                    }
                }
            

Output-

               Enter any two numbers
               10
               11
               max=11
          
3.Function with no return type but with arguments.

In this method, we pass the arguments to the function while calling the function. But, This type of function will not return any value when we call the function from main() function.

Sample program
Implement a program to find minimum of number using function (Prototype: with argument, no return type)
                    
                    #include<stdio.h> 
                    #include<conio.h> 
                    
                    void min(int a,int b);
                    void main()
                    {
                        int a,b;
                        clrscr();
    
                        printf("Enter any two numbers");
                        scanf("%d%d",&a,&b);

                        min(a,b);
                        getch();
                    }
    
                    void min(int a,int b)
                    {
                        if(a < b)
                        {
                             printf("%d is minimum",a); 
                        }
                        else
                        {
                             printf("%d is minimum",b);
                        }
                    }
                

Output-

               Enter any two numbers
               10
               11
               10 is minimum
          
4.Function with return type and with arguments.

In this method, it allows us to pass the arguments to the function while calling the function. This type of function will return some value when we call the function from main() function.

Sample program
Implement a program to find average of three numbers using function (Prototype: with argument, with return type)
                
                #include<stdio.h> 
                #include<conio.h> 
                
                int avg(int a,int b,int c);

                void main()

                {
                    int a,b,c,A;
                    clrscr();

                    printf("enter three numbers\n");
                    scanf("%d%d%d",&a,&b,&c);

                    A=avg(a,b,c);
                    printf("average=%d",A);

                    getch();
                }

                int avg(int a,int b,int c)

                {
                    int average;

                    average=(a+b+c)/3;

                    return(average);
                }
            

Output-

               enter three numbers
               11
               10
               9
               average=10
          
Methods of parameter(Argument) passing
  1. Pass by value(call by value)
  2. Pass by address (call by address)
1.Pass by value(call by value)

In this method, the value of the actual parameters is copied into the formal parameters. The actual parameter is the argument used in the function call whereas and parameter is the argument which is used in the function definition. Here we can't modify the value of the actual parameter by the formal parameter.
By default, C programming uses call by value to pass arguments.

Sample program
                
                #include<stdio.h> 
                
                int sum(int a, int b)
                {
                    int c=a+b;
                    return c;
                } 
                                             
                int main(     
                {
                    int var1 =10;
                    int var2 = 20;
                    int var3 = sum(var1, var2);
                    printf("sum=%d", var3);      
                    return 0;
                }

            

Output-

               sum=30
          
2.Pass by address (call by address)

[We will learn this concept in pointer tutorial.]

Examples For Practice
  1. Implement a program to find maximum of two numbers using function (Prototype: no argument, no return type)
  2. Find Factorial of any number using user defined function.
  3. Find whether given number is Armstrong number or not using user defined function Armstrong Number: If the number is equal to sum of cube of each digits in number.