× About Us How it works Pricing Student Archive Subjects Blog Contact Us

Enrich your knowledge with our informative blogs

What are the major types of functions in program?

A Function contains a set block of statements that are written outside of the main program and are executed to perform a desired set of operations.  

This function may or may not take data from the primary function.  

Using functions the modularity of the program is boosted as a large program gets divided into smaller sections. This makes the code optimized, reusable and scalable too.  

Functions are known by different names in different programming languages such as methods, subroutines, procedures etc.  

There are primarily two types of Functions: 

1). In-built or Pre-defined Functions or Library functions  

All the programming languages contain some built-in functions such as standard input/output functions, maths functions etc. These functions are listed in files called as system libraries. 

To use a function, you need to add that library in your code. This helps in writing the code error free.  

The system library is usually included in the code before the main() function begins. 

For example: sqrt(); function is used to find the square root of any number and is defined in the library  math.h header file in C programming language. 

This function can easily be called as: 

x = sqrt (y); (y should be positive) 

Here, square root of y is calculated and the returned value is stored in the variable x. 

2). User-defined functions  

These functions are defined and created by the programmer or the person doing the coding.  

Functions are properly created and tested as per a working and optimal algorithm before going for the final builds.  

You don’t have to include any header file and the functions are defined in the same file containing the main() function.  

Now depending on the arguments passed and return values the functions fall in four different categories: 

1. Without Parameters and without return values 

As, there is no return value, the return type of the function becomes “void”. 

For example: Let’s code a program in C to add two numbers without passing any arguments. 

#include <stdio.h>
#include <conio.h>
void main()
  {
    void sum();    //declaring the function
    sum();          // calling the function
    getch();
  }
void sum(){
  int a,b,c;
  printf(“Enter two numbers: \n”);
  scanf (“%d%d”, &a, &b);
  c= a+b;
  printf(“Sum = %d”, c); // printing the answer
}
Output Screen:
Enter two numbers:
12 45
Sum = 57

Let’s understand the flow of control: 

  1. sum() is defined with return type void  as nothing is intended to be returned. 
  2. The function sum() is called from the main function. 
  3. Control pauses the main() function and shifts to the first line of the function. 
  4. The addition is performed and the result is printed.  
  5. Control jumps back to the main() function without taking anything back. 

2. Without parameters but with return values 

User creates a function that returns a value but no parameters are passed. 

Understand the same addition program defined using without using parameters that returns an integer value.  

#include <stdio.h>
#include <conio.h>
void main()
  {
    int sum();    //declaring the function
    int c;
    c = sum();    // function is called and value is returned
    printf(“Sum = %d”,c);
  }
int sum(){
  int a,b,c;
  printf(“Enter two numbers: \n”);
  scanf (“%d%d”, &a, &b);
  c= a+b;
  return c; // the addition of two number is returned
}
Output Screen:
Enter two numbers:
34 56
Sum = 90

Flow of control: 

  1. sum() is declared with a return type int. This means that the function will be returning an integer value. 
  2. The function sum() is called from the main function with no parameters. 
  3. The assignment operator “ = “ is ensuring that the returned value is captured and assigned to a variable.   
  4. Control pauses the main() function and shifts to the first line of the function. 
  5. The addition is performed and the result is sent back to the main function.  
  6. The value is captured in a variable and printed on the console.  

3. With parameters and without return values 

Here parameters are sent in the function but nothing is returned. This makes the return type “void”. 

One thing is to remember here is to ensure that the datatypes of the variables that are passed should be same as that defined in the function. Else the compiler will give an error.  

Check the same addition function made using the parameter values but does not returns anything. 

#include <stdio.h>
#include <conio.h>
void main()
{
  void sum (int, int);    //declaring the function
  int a,b;
  printf(“Enter two numbers”);
  scanf(“%d%d”, &a, &b);
  sum(a,b);
  getch();
}
void sum(int a, int b){
  int c;
  c= a+b;
  printf(“Sum = %d”,c); // nothing is returned
}
Output Screen:
Enter two numbers:
34 89
Sum = 123

Things to consider: 

Since the function sum() is uses two parameters that are integers. The function definition should also contain only two parameters that are integer. 

Any mismatch will give error. The function will not work. 

Always remember:   

void sum (int, int);  and void sum (int, int, int); and int void (int,int);  are all different. You need to be very careful about what exactly you want your function to do. 

4. With both parameters and return values  

Now, let’s see how to make a function that performs addition using return type as well as with parameters.  

#include <stdio.h>
#include <conio.h>
void main()
{
  int sum (int, int);    //declaring of function
  int a,b,c;
  printf(“Enter two numbers”);
  scanf(“%d%d”, &a, &b);
  c = sum(a,b);       // calling the function
  printf(“Sum=%d”,c);
  getch();
}
int sum(int a, int b){
  int c;
  c = a+b;
  return c;     // value returned
}
Output Screen:
Enter two numbers:
45 67
Sum = 112

Flow of control:  

  1. Here copies of the parameters are passed to the called function from the calling function. 
  2. The copies are received as parameters in the called function. 
  3. Body is executed and the sum is returned to the called function where it is displayed using a printf() function.    

 

Read More – Coding and Programing Questions

View More – Useful links for Your Child’s Development 

Unveil your gateway to a lucrative career!
Unveil your gateway to a lucrative career!

Unleash the power of true logic building with Real-time instructions and live coding exposure.

Book A Demo Class

Tel Guru
Tel Guru

Register For The Demo Class

[footer-form]