Function in C

Function in C

Function in C

Function in C:-

A function is a set of statement those 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 to enough to solve the current problem then immediately the developer write 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 function present in the program but all the function are copied may or may not be the function to the program.

  1. Dynamic library

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

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

Execution of C program begins from main().

Prototype:-

Prototype is known as function declaration. It is used to prevent to pass 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 the clear image of the program.

Modular style gives the 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 function parameter, it is changed for the current function only. It will not change the value of 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

In call by reference Original value is modified because we pass 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 variable only can be accessed in the same function where it can be defined.I t only accesses in the same function, and in other function calling convention is used.

Global Variable

It can be access in all the function.

Example:-

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

Difference between Actual parameter and formal parameter:-

Actual parameter allocates memory but 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 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.
  • Heap sort.
  • Merge sort.
  • Tower of Hanoi

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

 

basicittopic

basicittopic

A Computer Science Study for IT students and people of IT community