Nearby lessons

80 of 124

C - Types of Functions

C functions fall into clear categories — library versus user-defined, and four combinations of "takes arguments" and "returns a value". Learn all four forms and when each is the right choice.

Library vs User-Defined

The first and broadest division:

HeaderProvidesExamples
<stdio.h>Input and outputprintf, scanf, fopen
<string.h>String handlingstrlen, strcpy, strcmp
<math.h>Mathematicssqrt, pow, sin
<stdlib.h>Utilities and memorymalloc, atoi, rand
<ctype.h>Character testsisalpha, toupper
Example01
CCode Cell
1#include <stdio.h>
2#include <math.h>
3 
4int cube(int n) { return n * n * n; } /* user-defined */
5 
6int main()
7{
8 printf("Library sqrt(25) = %.1f\n", sqrt(25.0));
9 printf("Mine cube(3) = %d\n", cube(3));
10 return 0;
11}
Output
Library  sqrt(25) = 5.0
Mine     cube(3)  = 27

Form 1 — No Arguments, No Return

A self-contained action. Useful for banners, menus and fixed messages:

Example02
CCode Cell
1#include <stdio.h>
2 
3void printBanner(void)
4{
5 printf("========================\n");
6 printf(" WELCOME TO C \n");
7 printf("========================\n");
8}
9 
10int main()
11{
12 printBanner();
13 return 0;
14}
Output
========================
   WELCOME TO C
========================

Form 2 — Arguments, No Return

Takes input, produces an effect rather than a value. Typical for display routines:

Example03
CCode Cell
1#include <stdio.h>
2 
3void printTable(int n, int upto)
4{
5 int i;
6 for (i = 1; i <= upto; i++)
7 printf("%d x %d = %d\n", n, i, n * i);
8}
9 
10int main()
11{
12 printTable(7, 5);
13 return 0;
14}
Output
7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35

Form 3 — No Arguments, Returns a Value

Produces a value from somewhere other than its parameters — input, a global, or a constant:

Example04
CCode Cell
1#include <stdio.h>
2 
3int getUserAge(void)
4{
5 int age;
6 printf("Enter your age: ");
7 scanf("%d", &age);
8 return age;
9}
10 
11float getPi(void) { return 3.14159f; }
12 
13int main()
14{
15 int age = getUserAge();
16 
17 printf("You are %d\n", age);
18 printf("Pi is %.5f\n", getPi());
19 return 0;
20}
Output
Enter your age: 25
You are 25
Pi is 3.14159

Form 4 — Arguments and a Return Value

The most useful form: inputs in, result out, no side effects. Easy to test and easy to reuse:

Example05
CCode Cell
1#include <stdio.h>
2 
3int add(int a, int b) { return a + b; }
4float average(int a, int b) { return (a + b) / 2.0f; }
5int max(int a, int b) { return a > b ? a : b; }
6 
7int main()
8{
9 printf("add(12, 8) = %d\n", add(12, 8));
10 printf("average(12, 8) = %.1f\n", average(12, 8));
11 printf("max(12, 8) = %d\n", max(12, 8));
12 return 0;
13}
Output
add(12, 8)     = 20
average(12, 8) = 10.0
max(12, 8)     = 12

The Four Forms Summarised

FormSignatureTypical use
No args, no returnvoid f(void)Banners, menus
Args, no returnvoid f(int)Printing, updating
No args, returnsint f(void)Reading input, constants
Args and returnsint f(int)Calculations — prefer this
In simple words: favour the fourth form. A function that takes everything it needs as arguments and hands back a value can be tested in isolation and reused anywhere. The other three depend on the world around them.

Recursive Functions

A function that calls itself. Every recursive function needs a base case that stops the descent:

Example07
CCode Cell
1#include <stdio.h>
2 
3int fibonacci(int n)
4{
5 if (n <= 1) return n; /* base case */
6 return fibonacci(n - 1) + fibonacci(n - 2); /* recursion */
7}
8 
9int sumDigits(int n)
10{
11 if (n == 0) return 0;
12 return (n % 10) + sumDigits(n / 10);
13}
14 
15int main()
16{
17 int i;
18 
19 printf("Fibonacci: ");
20 for (i = 0; i < 8; i++) printf("%d ", fibonacci(i));
21 
22 printf("\nsumDigits(9875) = %d\n", sumDigits(9875));
23 return 0;
24}
Output
Fibonacci: 0 1 1 2 3 5 8 13
sumDigits(9875) = 29

static Functions — File-Private

A static function is visible only inside its own .c file. Use it for helpers that are not part of your module's public interface:

Example08
CCode Cell
1#include <stdio.h>
2 
3/* Internal helper - not visible to other .c files */
4static int isValid(int n)
5{
6 return n > 0 && n <= 100;
7}
8 
9/* Public entry point */
10int processScore(int score)
11{
12 if (!isValid(score)) return -1;
13 return score;
14}
15 
16int main()
17{
18 printf("processScore(85) = %d\n", processScore(85));
19 printf("processScore(150) = %d\n", processScore(150));
20 return 0;
21}
Output
processScore(85)  = 85
processScore(150) = -1

Functions Grouped by Purpose

A practical way to organise a program: pure calculations, input, output, and validation each in their own group:

Example09
CCode Cell
1#include <stdio.h>
2 
3/* Calculation */
4float calculateArea(float l, float w) { return l * w; }
5 
6/* Validation */
7int isPositive(float n) { return n > 0; }
8 
9/* Output */
10void displayResult(float area)
11{
12 printf("Area = %.2f sq units\n", area);
13}
14 
15int main()
16{
17 float length = 12.5f, width = 8.0f;
18 
19 if (isPositive(length) && isPositive(width))
20 displayResult(calculateArea(length, width));
21 else
22 printf("Dimensions must be positive\n");
23 return 0;
24}
Output
Area = 100.00 sq units

Common Mistakes

  • Mixing calculation with printing — a function that computes and prints cannot be reused where you need only the number.
  • Recursion with no base case — the stack fills and the program crashes.
  • Using globals instead of parameters — makes the function untestable and order-dependent.
  • Making everything void — returning a value is usually more flexible.
  • Forgetting the header — using sqrt without <math.h>.
Separate computing from printing. void showArea(float l, float w) that prints internally is stuck: you cannot sum areas, store them, or unit-test them. Write float area(float l, float w) and let the caller decide what to do with the result.
📝 Key Takeaways
  • Library functions come from headers; user-defined ones you write.
  • 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 recursive function calls itself.
  • static limits a function to its own source file.

🧠 Test Your Knowledge

4 Questions
Progress: 0 / 4