Hello readers, in this article we will discuss 4 types of user defined functions in c. If you don’t know the basics of C language, the Structure of C language, or in general what programming is then I recommend you to read our previous article on the mentioned topics. But now, let’s focus on types of user defined functions in c.

4 Types of User Defined Functions in C - Learn Now

Types of user defined Functions in C

There are mainly 4 types of user defined functions in C language. They are listed below.

  • Without Return Type Without Argument
  • With Return Type Without Argument
  • Without Return Type With Argument
  • With Return Type With Argument

Before diving into the topic, it is very important to know there are three major parts while using user defined function in C. They are:

  • Declaration of Function
  • Calling function
  • Definition of Function

Declaration of Function

return_type function_name(data type of arguments);

//examples
void sum(int, int);

Here, the void is the return type of the function i.e. no data will be returned by the function as we used void but we can use other return types like int, float, etc. to return data. The sum is the name of the function and int, int is the data type of the arguments, you can also use void for no arguments.

Calling a Function

function_name(arguments);

//example
sum(a, b); //with argument
sum(); //without argument

Here, the sum is the name of the function, and a, and b are the arguments that can be passed to the function.

Definition of Function

function_name(arguments with data types){
   //Block of Statements
}

//example
sum(int a, int b){
 return a+b;
}

Here, the sum is the function name, and a, and b are the arguments passed to the function. You may use sum() if you don’t want any arguments in the function.

Now, let’s see the types with their examples.

Without Return Type Without Argument

This is the type of the user defined functions in c that doesn’t return any value after the execution and doesn’t take any argument.

Example

//User defined function without return type without argument
#include<stdio.h>
#include<conio.h> //declare function
void sum(void);
void main(){
    sum(); //call the function
    getch();
}
//definition of function
void sum(){
    int a,b;
    printf("Enter two numbers\n");
    scanf("%d%d", &a,&b);
    printf("Sum is %d", a+b);
}

In this example, first of all, we declared the function without return type and without argument i.e. void sum(void). Then inside the main() function, we called the sum function with sum(). As soon as the function is called the program entered the sum function and it calculated the sum of numbers supplied by the user.

With Return Type Without Argument

This is the type of user defined functions in c that returns a value after the execution but doesn’t take any argument.

Example

//user defined function with return type without arguments
#include<stdio.h>
int sum(void); //declaration of function
int main(){
    printf("Sum is %d", sum()); //call and print the returned value
    return 0;
}
//definition of funtion
int sum(){
    int a, b;
    printf("Enter two numbers\n");
    scanf("%d%d", &a,&b);
    return a+b; //return the value
}

In this example, we declared a function with return type and without argument i.e. int sum(void). Then inside the main() function we gave a print command and printed the value returned by the sum() function. While giving the print command, we also called the sum() function so, the function will be executed before the print command. Then we returned the value of the sum of two numbers supplied by the user with the return command.

Without Return Type With Argument

This is the type of user defined functions in c which doesn’t return any value but takes some arguments.

Example

//Without return type with arguments
#include<stdio.h>
#include<conio.h>
void sum(int, int); //declaration of function
void main(){
    int a,b;
    printf("Enter two numbers\n");
    scanf("%d%d", &a,&b);
    sum(a,b); //calling the function with arguments
    getch();
}
//definition of the function
void sum(int a, int b){
    printf("Sum is %d", a+b);
}

In this example, first, we defined a function without return type but accept two integer values as arguments i.e. void sum(int, int). Then inside the main() function, we asked for the input from the user and then called the function with arguments i.e. sum(a,b). While defining the function, we also gave arguments in the parenthesis i.e. sum(int a, int b) so that we can use those values inside the sum() function. Finally, we printed the value of the sum inside the function.

With Return Type With Argument

This is the type of user defined functions in c which returns some value after the execution as well as takes some arguments.

Example

//with return type and with argument
#include<stdio.h>
int sum(int, int); //declare the function
int main(){
    int a,b;
    printf("Enter two numbers\n");
    scanf("%d%d",&a,&b);
    printf("Sum is %d", sum(a,b)); //call and print the returned value with arguments
}
//definition of the function
int sum(int a,int b){
    return a+b; //return the value
}

In this example, we declared a function that returns an integer value as well as takes two integers as arguments i.e. int sum(int, int). Inside the main() function, we asked for the input from the users and called the sum() function with arguments (input by the user) i.e. sum(a, b). Finally, the function sum() is defined with arguments i.e. sum(int a, int b) which returns the value of a+b and the value get’s printed inside the main() function.

So, this is it for this article but I recommend you to check the following articles to get more knowledge regarding C programming.

If you don’t have a C compiler set up on your device, you can use an online compiler as well.

Before You Leave

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *