
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Different Categories of Functions in C Programming
Functions are categorized bases on the presence or absences of arguments and whether they return a value. A user-defined function is one that is defined by the user when writing any program, as opposed to library functions that have predefined definitions.
To meet specific requirements, the user must develop their own functions. Such functions must be properly defined by the user. A function is a block of code designed to perform a specific task. It is written once and can be reused multiple times as needed by the programmer.
-
Functions without arguments but with return values.
-
Functions with arguments but without return values
Functions without arguments but without return values
When a function has no arguments, it does not receive any data from the calling function. Similarly, when a function does not return a value, the calling function does not receive any data from the called function.
Example
This code defines a sum function that takes no arguments, reads two integers from the user, calculates their sum, and prints the result. The main function calls sum.
#include <stdio.h> void sum(); int main() { sum(); return 0; } void sum() { int a, b, c; printf("Enter 2 numbers: "); scanf("%d%d", &a, &b); c = a + b; printf("Sum = %d", c); }
The result is generated as follows &minus
Enter 2 numbers: 3 5 Sum=8
Functions without arguments and with return values
Here is an example of creating functions that do not take any arguments but return a value to calling function. For instance, the getchar function has no parameter but returns an integer representing a character.
Example
Here, the code demonstrates a sum function that takes no arguments, reads two integers from the user, calculates their sum, and returns the result. The main function calls sum and prints the sum.
#include <stdio.h> int sum(); int main() { int c; c = sum(); printf("Sum = %d", c); return 0; } int sum() { int a, b, c; printf("Enter 2 numbers: "); scanf("%d %d", &a, &b); c = a + b; return c; }
We will get the output as follows ?
Enter two numbers 10 20 30
Functions with arguments and without return values
When a function has arguments, this receives any data from the calling function but with no return value. These are void functions with no return values.
Example
This code reads two integers, calculates their sum using the sum function, and prints the result.
#include <stdio.h> void sum(int, int); int main() { int a, b; printf("Enter 2 numbers: "); scanf("%d%d", &a, &b); sum(a, b); return 0; } void sum(int a, int b) { int c; c = a + b; printf("Sum = %d", c); }
The output is obtained as follows ?
Enter two numbers 10 20 Sum=30
Functions with arguments and with return values
This function takes an integer argument, performs some operations, and returns the integer value. This demonstrates the basic function usage in C.
Example
This code demonstrates two integers, calculates their sum using sum function, and prints the result. The main function specifies the input and output, while sum performs the addition.
#include <stdio.h> int sum(int, int); int main() { int a, b, c; printf("Enter 2 numbers: "); scanf("%d%d", &a, &b); c = sum(a, b); printf("Sum = %d", c); return 0; } int sum(int a, int b) { int c; c = a + b; return c; }
The result is generated as follows ?
Enter two numbers 10 20 Sum=30