Thursday 25 January 2018

Functions in C


A function is a set of statements that take inputs, do some specific computation and produces output....




 Every C program has at least one function, which is main(), and all the most trivial programs can define additional functions.

A function declaration tells the compiler about a function's name, return type, and parameters. A unction definition provides the actual body of the function.You can divide up your code into separate functions. How you divide up your code among different functions is up to you, but logically the division is such that each function performs a specific task.

A function declaration tells the compiler about a function's name, return type, and parameters. A function definition provides the actual body of the function.

The C standard library provides numerous built-in functions that your program can call. For example, strcat() to concatenate two strings, memcpy() to copy one memory location to another location, and many more functions.


Types of function

Depending on whether a function is defined by the user or already included in C compilers, there are two types of functions in C programming
There are two types of function in C programming:

Standard library functions

The standard library functions are built-in functions in C programming to handle tasks such as mathematical computations, I/O processing, string handling etc.
These functions are defined in the header file. When you include the header file, these functions are available for use. For example:
The printf() is a standard library function to send formatted output to the screen (display output on the screen). This function is defined in "stdio.h" header file.
There are other numerous library functions defined under "stdio.h", such as scanf()fprintf()getchar() etc. Once you include "stdio.h" in your program, all these functions are available for use.

User-defined function

As mentioned earlier, C allow programmers to define functions. Such functions created by the user are called user-defined functions.
How user-defined function works?

#include <stdio.h>

void functionName()

{

    ... .. ...

    ... .. ...

}

 

int main()

{

    ... .. ...

    ... .. ...

 

    functionName();

   

    ... .. ...

    ... .. ...

}


The execution of a C program begins from the main() function.
When the compiler encounters functionName(); inside the main function, control of the program jumps to
 void functionName()
And, the compiler starts executing the codes inside the user-defined function.
The control of the program jumps to statement next to functionName(); once all the codes inside the function definition are executed.




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.


While calling a function, there are two ways in which arguments can be passed to a function −
SrN.
Call Type & Description
1
This method copies the actual value of an argument into the formal parameter of the function. In this case, changes made to the parameter inside the function have no effect on the argument.
2
This method copies the address of an argument into the formal parameter. Inside the function, the address is used to access the actual argument used in the call. This means that changes made to the parameter affect the argument.


Code for Call by Value:

#include <stdio.h>
void swapnum( int var1, int var2 )
{
   int tempnum ;
   /*Copying var1 value into temporary variable */
   tempnum = var1 ;

   /* Copying var2 value into var1*/
   var1 = var2 ;

   /*Copying temporary variable value into var2 */
   var2 = tempnum ;

}
int main( )
{
    int num1 = 35, num2 = 45 ;
    printf("Before swapping: %d, %d", num1, num2);

    /*calling swap function*/
    swapnum(num1, num2);
    printf("\nAfter swapping: %d, %d", num1, num2);

}

C Compiler to run above code:http://bit.ly/2uPGImG

Output:

Before swapping: 35, 45
After swapping: 35, 45

Code  for Call by Reference:

#include<stdio.h>

void interchange(int *num1,int *num2)
{
    int temp;
    temp  = *num1;
    *num1 = *num2;
    *num2 = temp;
}

int main() {

    int num1=50,num2=70;
    interchange(&num1,&num2);

    printf("\nNumber 1 : %d",num1);
    printf("\nNumber 2 : %d",num2);

    return(0);
}

Output:

Number 1 : 70
Number 2 : 50


C Compiler to run above code:http://bit.ly/2uPGImG

Advantages of user-defined function

  1. The program will be easier to understand, maintain and debug.
  2. Reusable codes that can be used in other programs
  3. A large program can be divided into smaller modules. Hence, a large project can be divided among many programmers.


2 comments:

  1. Can you Please explain me some examples of call by value and call by reference?

    ReplyDelete
  2. You can follow the code provided in the post

    ReplyDelete