Skip to main content

20. What is Function

 

C Functions

In C, we can divide a large program into the basic building blocks known as function. The function contains the set of programming statements enclosed by {}. A function can be called multiple times to provide reusability and modularity to the C program.

Function Aspects

  • Function declaration: A function must be declared globally in a C program to tell the compiler about the function name, function parameters, and return type.
  • Function call: Function can be called from anywhere in the program. The parameter list must not differ in function calling and function declaration. We must pass the same number of functions as it is declared in the function declaration.
  • Function definition: It contains the actual statements which are to be executed. It is the most important aspect to which the control comes when the function is called. Here, we must notice that only one value can be returned from the function.

Types of Functions

There are two types of functions in C programming :

  1. Library Functions: These are the functions which are declared in the C header files such as scanf(), printf(), gets(), puts() etc.
  2. User-defined Functions: These are the functions which are created by the C programmer, so that he/she can use it many times. It reduces the complexity of a big program and optimizes the code.

Return Value

A C function may or may not return a value from the function. If we don't have to return any value from the function, use void for the return type.

Different aspects of function calling

A function may or may not accept any argument. It may or may not return any value. Based on these facts, there are four different aspects of function calls.

1. Function without argument and without return value

#include <stdio.h>

void printName();
void main()
{
   printf("Hello ");
   printName();
}
void printName(){
   printf("Deepak");
}

Output :

Hello Deepak

2. Function without argument and with return value

#include <stdio.h>

float square();
void main()
{
   printf("Going to calculate the area of the square.\n") ;
   float area = square();
   printf("The area of the square is : %f\n", area);
}
float square(){
   float side;
   printf("Enter the side of square in metres : ");
   scanf("%f", &side);
   return (side*side);
}

Output :

Going to calculate the area of the square.
Enter the side of square in metres : 2.5
The area of the square is : 6.250000

3. Function with argument and without return value

#include <stdio.h>

void sum(int, int);
void main()
{
   int a, b;
   printf("Going to calculate the sum of numbers.\n");
   printf("Enter two numbers : ");
   scanf("%d %d", &a, &b);
   sum(a, b);
}
void sum(int a, int b){
   printf("The sum is %d.", a+b);
}

Output :

Going to calculate the sum of numbers.
Enter two numbers : 4 10
The sum is 14.

4. Function with argument value and with return value

#include <stdio.h>

int add(int, int);
void main()
{
   int x, y, result;
   printf("Going to calculate the sum of numbers.\n");
   printf("Enter two numbers : ");
   scanf("%d %d", &x, &y);
   result1 = add(x, y);
   printf("The sum is : %d.", result);
}
int add(int x, int y){
   return (x+y);
}

Output :

Going to calculate the sum of numbers.
Enter two numbers : 7 8
The sum is : 15.

C Library Functions

Library Functions are the inbuilt functions in C that are grouped and placed at a common place called the library. Such functions are used to perform some specific operations. The library functions are created by the designers of the compilers. All C standard library functions are defined inside the header files saved with the extension .h. We need to include these header files in our program to make use of the library functions defined in such header files. For example, to use the library functions such as printf/scanf we need to include stdio.h in our program which is the header file that contains all the library functions regarding standard input/output.

Function Call

There are two methods to pass the data into the function in C language, i.e. call by value and call by reference.

Call by Value in C

  • The value of the actual parameters are copied into the formal parameters. In other words, we can say that the value of the variable is used in the function call in the call by value method.
  • We can not modify the value of the actual parameter by the formal parameter.
  • Different memory is allocated for actual and formal parameters since the value of the actual parameter is copied into the formal parameter.
  • The actual parameter is the argument which is used in the function call whereas formal parameter is the argument which is used in the function definition.

#include <stdio.h>

void change(int)
void main()
{
   int x = 100;
   printf("Before function call x = %d\n", x);
   change(x);
   printf("After function call x = %d\n", x);
}

void change(int num){
   printf("Before adding value inside function num = %d\n", num);
   num = num + 100;
   printf("After adding value inside function num = %d\n", num);
}

Before function call x = 100
Before adding value inside function num = 100
After adding value inside function num = 200
After function call x = 100

#include <stdio.h>

void swap(int, int);
void main()
{
   int a = 10;
   int b = 20;
   printf("Before swapping the values in main a = %d, b = %d\n", a, b);
   swap(a,b);
   printf("After swapping values in main a = %d, b = %d\n", a, b);
}
void swap(int a, int b){
   int temp;
   temp = a;
   a = b;
   b = temp;
   printf("After swapping values in function a = %d, b = %d\n", a, b);
}

Before swapping the values in main a = 10, b = 20
After swapping values in function a = 20, b = 10
After swapping values in main a = 10, b = 20

Call by Reference in C

  • The address of the variable is passed into the function call as the actual parameter.
  • The value of the actual parameter can be modified by changing the formal parameters since the address of the actual parameters is passed.
  • The memory allocation is similar for both formal parameters and actual parameters. All the operations in the function are performed on the value stored at the address of the actual parameters, and the modified value gets stored at the same address.

#include <stdio.h>

void change(int *)
void main()
{
   int x = 100;
   printf("Before function call x = %d\n", x);
   change(&x);
   printf("After function call x = %d\n", x);
}

void change(int * num){

   printf("Before adding value inside function num = %d\n", *num);
   *num = *num + 100;
   printf("After adding value inside function num = %d\n", *num);
}

Output :

Before function call x1 = 100
Before adding value inside function num = 100
After adding value inside function num = 200
After function call x1 = 200

#include <stdio.h>

void swap(int *, int *);
void main()
{
   int a = 10;
   int b = 20;
   printf("Before swapping the values in main a = %d, b = %d\n", a, b);
   swap(&a,&b);
   printf("After swapping values in main a = %d, b = %d\n", a, b);
}
void swap(int *x, int *y){
   int temp;
   temp = *x;
   *x = *y;
   *y = temp;
   printf("After swapping values in function x = %d, y = %d\n", *x, *y);
}

Output :

Before swapping the values in main a = 10, b = 20
After swapping values in function a = 20, b = 10
After swapping values in main x = 20, y = 10

Comments