C Function Types

A function is a small part of a program which is a collection of codes to perform a specific task. As we have seen in our earlier post that the C program provides the user with the privilege to build our own function as per our requirements. We can define such functions with or without parameters. Such function may or may not return value. The type of function is chosen on the basis of the requirement of the users. In this article, you are going to learn in detail about the types of functions in the C programming language. See below types of function to learn how to build a function of our own to meet our requirements.

No return type and no argument Functions

In this type of function, we won’t pass any parameters or arguments while defining or declaring the functions. This type of function won’t return any value to the main function. We recommend you to use such function in a condition where you don’t expect any return value from the function and some statement needs to be printed. Following is the syntax of the function with no return type and no argument.

Syntax:

void function_name(){
//Body of the function;
}

Now the following example will make you understand more about the function with no return type and no argument.

An example illustrating function with no return type and no argument:

Code:

#include<stdio.h>
void statement();//Function prototype or declaration
int main()
{
	statement();//Function call
	
}
void statement(){
	printf("This is a function with no return type and no arguement.");
}

Output

This is a function with no return type and no argument. In the main() function,
Analysis:
In the above program, we have used the function with no return type and no argument. The main() function of our program only includes a function call. The function call will reach to the function definition. In the function definition, the program finds the printf function so it will print the statement inside the printf function. The function will not return any value so we use void as the return type. This is a very simple function where we implement it just by calling the function.

No return type with the argument Functions

In this type of function, The function will not return any values to the main function but accepts the arguments as input. We will pass arguments while declaring, defining or calling the function. Such type of functions are useful when we are passing some values to the function but we are not expecting any return values from the function. Now let’s see the syntax of the function with the argument but no return type.

Syntax:

void function_name(datatype arguement1, datatype arguement2,…)
{
//Body of the function;
}
Now we will see our first example of making a fucntion having an arguements but no return type.

An example illustrating function with arguments but no return type.

Code:

#include<stdio.h>
void display(int);//Fucntion prototype or declaration
int main(){
int x;
printf("Enter the number that you want to display: ");
scanf("%d", &x);
display(x);//Function call	
}
void display(int a)//Function definition
{
	printf("You entered: %d",a);
}

Output:

Enter the number that you want to display: 3

You entered: 3
Analysis:
The above program is a simple example of a function with an argument but no return type. In such functions, we have to enter the data type of the argument in the function prototype at first. In top-down programming approach, It acts as an indicator that the function will have to accept the argument with a certain datatype. As can be seen in the above program that we have entered the datatype int in the function prototype. In the function call, we have passed ‘x’ with datatype int as a parameter in the function call. This enables the control to pass onto the function definition and where we have just printed the value that we have passed through the function call. So the function won’t be returning any values so we are using void as its return type.

With return type and no argument Functions.

In this type of function, The function won’t take any arguments as input while defining, declaring or calling the function. This function will return some value to the main function. The datatype of the return value will be the same as that of the return type of the function declarations. For example, if the return type of the function is float then the return value will be the float. Similarly, if the return type of the function is int then we can infer that the function will return an int value. Now let’s see the syntax of the function with return type and no argument.

Syntax:

returnType function_name()

{

//Body of the function

}

An example illustrating function with return type and no arguments:

Code:

#include<stdio.h>
int new_fun();
int main()
{
	int y;
		y= new_fun();
	printf("The number after increment becomes: %d",y);
}
int new_fun()
{
	int x;
	printf("Enter a number: ");
	scanf("%d", &x);
	x++;
        return x;
	
}

Output:

Enter a number: 4
The number after increment becomes: 5
Analysis:
The above program illustrates the implementation of the function with return type but no arguments. The function we are using will be returning a value with an int datatype so the return type of our function will be int. As our function won’t be taking any parameters, all the variables that are our function will be using will be initialized inside the function definition. After operations, the function will return the result to the main() function. The main() function will catch the return value and stores it in a variable as soon as the codes inside the function definition are traversed. The value of the variable will be printed inside the main() function.

Return type with the argument Functions.

This type of function will allow us to pass arguments to the function while we call the function. This method will return some values when we call the function from the main() function. The datatype of the return value will depend on the return type of the function definition. For example, if the return type is int then the return value will be int. This type of function is called a fully dynamic function means and are mostly used over other types of functions. This function is the best option for modular programming.

An example illustrating function with return type and argument:

Code:

#include<stdio.h>
int div(int, int);
int main()
{
	int a,b,c;
	printf("Enter two numbers: ");
	scanf("%d%d", &a, &b);
	c= div(a,b);
	printf("The division of two numbers when the first number is divided by the second number is: %d", c);
	
}
div(int p, int q){
	int d;
	d=p/q;
	return d;
}

Output:

Enter two numbers: 10

2

The division of two numbers when the first number is divided by the second number is: 5

Analysis:

The example of the function with return type and arguments is illustrated above. As can be seen in the above code that we have passed the number of arguments to the function as per our requirements. The return type of the function is int which indicates that the function will be returning an integer value to the main function. In the function definition of the above example, we have written the code just to perform an operation of division. Henceforth, all the jobs of initialization and printing are done within the main function.

 

 

SHARE C Function Types

You may also like...

Leave a Reply

Your email address will not be published.

Share