C Programming

Different Types of Function

A function is a code to accomplish a certain operation. It has a name for its identification. Here we are not interested in defining the function. Here we discuss different types of function and their application.

Ways to Define a Function

There are four types of programs available in the C language. They are:

1. Takes Nothing, Returns Nothing.

2. Takes Something, Returns Nothing.

3. Takes Nothing, Returns Something.

4. Takes Something, Returns Something.

1. Takes Nothing, Returns Nothing

Programming Example 1

#include <stdio.h>

void main()
{
    void add(void);
add ();
}
void add ()
{

int a, b, c ;
printf( "Enter two numbers" ) ;
scanf("%d %d",&a,&b) ;
    c= a + b ;
printf( "sum is = %d", c) ;
}

Output

Explanation

Whenever we want to compile a program, the compiler has to know the meaning of every word written in a program which means it must declare every function or variable in the program. For this Function, Declaration is required. In Function Declaration, we write 3 things

  1. Return type
  2. Name of FM
  3. Argument

But in the case of a predefined function, a declaration is written in the Header file. For example: clrscr () – -> Declaration is done in <stdio.h> file and function are defined in the library file. Turbo C++ 3.0 compiler automatically declares user-defined function. But we should declare a function. We only write the name and parameter passed in the argument in the calling function.

2. Takes Something, Returns Nothing

Programming Example 2

#include <stdio.h>

void add(int, int);         //function declaration Globally
void main()
{
int x,y;
void add(int, int);     //function declaration locally

printf ("Enter 2 numbers ");
scanf (" %d %d",&x,&y); // Actual arguments
    add (x,y);         
}
void add(int x, int y)      // Formal arguments
{

    int c;
    c= x+y;
printf("sum is %d",c);

}

Output

Explanation

If we want to add the two values of two variables that are declared inside the main () and add this in a different function name adds (), we use the Takes Something Returns Nothing method. In this case, the value of x and y is passed to the add function when add () is calling in the main function. For this, it is called call by value.

Whenever we pass a value as an argument to a calling Function, then these arguments are called Actual Argument.

And in a Function definition, inside the parenthesis of a function, we declare the variable that will receive the value of the variable passed by the Calling Function, which is called a Formal Argument.

The name of the Actual Argument and Formal Argument variable may be the same because the compiler does not confuse in this case; it knows that variable x, y that is declared inside main () and x , y declared in add () are different variables.

In Calling Function, we just pass the value of variables that are declared inside main (). For this, we write add ( x, y) ;

3. Takes Nothing, Returns Something

Programming Example 3

#include <stdio.h>

int add (void);
void main ()
{
int s;
s= add();
printf ("sum is %d",s);
}
int add ()
{

int a,b,c;
printf ("enter two numbers ");
scanf ("%d %d", &a,&b);
c= a+b;
return c;
}

Output

Explanation

Takes Nothing means, when even main () call a function to add (), that means it does not take any argument. Its means that add () take the value of variables from the user and perform the add.

Return something means add (); here, return the added result to the main () to use the keyword Return. When add () return the value, these value has to be kept in an int data type variable (s) that is declared in main (). Because here Return Type is int. That’s mean s= add () is replaced by, s= value of return.

For this, in the function definition of add (), here we have to mention the return type of add (), here is int. In the case of a return, we return only one value. So, return (a, b, c)-> is wrong. It means it only returns the value of c because it is written at least inside the parenthesis.

When any function returns any value, then it is to be understood that control also goes return to the calling function.

If we write any code after the return statement, that particular code has not been any effect on the program as the control shifts to the calling function.

Any function cannot access the variable declared by the other function. Because any variable that is declared inside a function is treated as a local variable.

So, in main (), we have to print the value of s instead of c because c is the variable that is declared inside the add ().

4. Takes Something, Returns Something

Programming Example 4

#include <stdio.h>

int add (int, int);
void main ()
{
    int s,x,y;
printf ("Enter 2 numbers ");
scanf ("%d %d ",&x,&y);
    s= add(x,y);
printf ("sum is %d", s);
}
int add(int a, int b)
{
int c;
c= a+b;
return c;
}

Output

Explanation

Takes Something Returns Nothing program is the same as the Takes Nothing Returns Something; the only difference is that, as it is by takes, something is in nature, so the value of two numbers has to be passed in the calling function.

That means the user takes the value from the keyboard used by the main (). That value is passed to the add ().

So, it is on the type of call by value example. Otherwise, the return procedure is the same as the Takes Nothing, Returns Something Procedure.

Conclusion

Learning about different types of functions in C, we have concluded that every type of function gives the same result. But each of them has a different mechanism. Each of them is needed according to the requirement of the program. So we have to know each type of function.

About the author

Bamdeb Ghosh

Bamdeb Ghosh is having hands-on experience in Wireless networking domain.He's an expert in Wireshark capture analysis on Wireless or Wired Networking along with knowledge of Android, Bluetooth, Linux commands and python. Follow his site: wifisharks.com