Nearby lessons

49 of 72

C - Types of Functions

C functions come in four forms, based on whether they take arguments and whether they return a value. This lesson shows all four forms using the same sum task so the differences are easy to see.

Form 1 — No Arguments, No Return

The function takes nothing in and sends nothing back. It performs a fixed action — here it prints the sum of two values written inside it:

Example01
CCode Cell
1#include <stdio.h>
2 
3void printSum(void) /* no arguments, no return */
4{
5 int a = 10, b = 20;
6 printf("Sum = %d\n", a + b);
7}
8 
9int main()
10{
11 printSum();
12 return 0;
13}
Output
Sum = 30

Form 2 — Arguments, No Return

The function takes input through its parameters but returns nothing. It displays the sum of whatever it is given:

Example02
CCode Cell
1#include <stdio.h>
2 
3void printSum(int a, int b) /* arguments, no return */
4{
5 printf("Sum = %d\n", a + b);
6}
7 
8int main()
9{
10 printSum(10, 20);
11
12 return 0;
13}
Output
Sum = 30

Form 3 — No Arguments, Returns a Value

The function takes nothing but hands a value back to the caller. Here it reads two numbers itself and returns their sum:

Example03
CCode Cell
1#include <stdio.h>
2 
3int getSum(void) /* no arguments, returns a value */
4{
5 int a, b;
6 printf("Enter two numbers: ");
7 scanf("%d %d", &a, &b);
8 return a + b;
9}
10 
11int main()
12{
13 int result = getSum();
14 printf("Sum = %d\n", result);
15 return 0;
16}
Output
Enter two numbers: 10 20
Sum = 30

Form 4 — Arguments and a Return Value

The most useful form: inputs in, result out, no printing inside. Easy to test and easy to reuse anywhere a value is needed:

Example04
CCode Cell
1#include <stdio.h>
2 
3int sum(int a, int b) /* arguments and a return value */
4{
5 return a + b;
6}
7 
8int main()
9{
10 printf("sum(10, 20) = %d\n", sum(10, 20));
11
12 return 0;
13}
Output
sum(10, 20) = 30
📝 Key Takeaways
  • The four forms: no args/no return, args/no return, no args/return, args/return.
  • The args-and-returns form is the most useful and most testable.
  • A void function performs an action; a non-void function produces a value.
  • Separate computing from printing whenever the result may be reused.

🧠 Test Your Knowledge

4 Questions
Progress: 0 / 4