Function in C

function in c

Function in C:-

A function is a set of statements that are encapsulated together to perform a particular task.

There are basically 3 types of function:-

  1. User-defined function
  2. Standard Library function
  3. System Calls

User-defined function:-

When the library function is not sufficient enough to solve the current problem then immediately the developer writes the code from the function which is called the user-defined function.

main() is the user-defined function.

Example:-
main()
{
      Bbsr();           
}
int Bbsr()
{
      printf(“I am in bbsr”);
}
int sum(int a,int b)
{
            int c=a+b;
            return c;
}

Standard library function:-

Some name of the standard library function :-

printf(),scanf(),strcmp(),strcpy(),strlen(),strcat(),…..etc.

mathematical library function is log(),pow(),exp(),modf() ….etc.

There are two types of library:-

  1. Static library
  2. Dynamic library
  1. Static library

When a program is linking with static library all the functions present in the program but all the functions are copied may or may not be the function of the program.

  1. Dynamic library

When a program is linking with a dynamic library all the functions are not copied to the program only those functions are used in the program they are copied.

Every function allocates memory in the stack segment in a LIFO(last in first out) order.

Execution of C program begins from main().

Prototype:-

A prototype is known as the function declaration. It is used to prevent passing the illegal value to a function.

The compiler checks the function definition from the prototype.

Difference between monolithic and modular:-

  1. Monolithic style does not give a clear image of the program.

Modular style gives a clear image of the program.

  1. It is difficult to understand the programmed logic and difficult to debug the program.

Modular style is simple to understand the programming logic and simple to debug the program.

Monolithic program:-

Modular program:-

Function calling convention:-

There are 3types of calling convention:-

  1. call by value
  2. call by address
  3. call by reference

call by value:-

If you change the value of the function parameter, it is changed for the current function only. It will not change the value of the variable inside the caller method.

Example:-

#include<stdio.h>  
 main() 
{    
    int x=100;    
    printf("Before the function called x=%d \n", x);    
    change(x);   
    printf("After the function called x=%d \n", x);
 
void change(int n) 
{    
    printf("Before adding the value inside function n=%d \n",n);    

    n=n+100;    

    printf("After addingthe value inside function n=%d \n", n);    

}    
return 0;  
}

Call by address  

Example:-

#include <stdio.h>
void call(int *x)
{
    *x=*x+10;
}
int main()
{
    int a=10;
    printf("a value = %d\n",a);
    call(&a);
    printf("After function calling : a value = %d",a);
    return 0;
}

Call by reference

Incall by reference Original value is modified because we pass the address. Value changed inside the function, is reflected inside as well as outside the function.

Example:-

#include<stdio.h>  
main() 
{    
    int x=100;    
    printf("Before the function called x=%d \n", x);    
    scanf(“%d”,&x)
    printf("After the function called x=%d \n", x);    
   
void change(int *n)
 {    
    printf("Before adding value inside function n=%d \n",*n);    

    (*n) += 100;    

    printf("After adding value inside function n=%d \n", *n);    
}      
return 0;  
}

Local variable

Local variables only can be accessed in the same function where it can be defined. It only accesses in the same function, and in other function calling convention is used.

Global Variable

It can be accessed in all the functions.

Example:-

#include”stdio.h”
Int k=90;    //global variable
main()
{
          Printf(“%d”,k);
}

Difference between Actual parameter and formal parameter:-

The actual parameter allocates memory but the formal parameter does not allocate memory.

Calling programs to pass actual arguments to called the functions. The called function access the information using formal arguments.

Function Recursion          

When a function called by itself is known as function recursion.

Example:-

main()
{
          Bbsr();
}
int Bbsr()
{
        Bbsr();
}

Any problem solves using a loop that problem can be solve using function recursion and vice versa.

But loop does not allocate memory whereas function recursion allocates memory.

Application of function recursion:-

  • function recursion is suitable for tree traversal.
  • It is used in quicksort.
  • Heapsort.
  • Merge sort.
  • Tower of Hanoi

The static command is used to avoid reinitialization, during recursive function called.

Leave a Reply