πŸ‡―πŸ‡΅ ζ—₯本θͺž | πŸ‡ΊπŸ‡Έ English
Advertisement

Lesson 14: Functions (Advanced)

Deep dive into C functions: pass-by-value, call stack, recursion.

πŸ“– What to learn on this page
βœ… Must-know essentials
  • Split code into functions for reuse
  • Recursion: a function calls itself
  • Recursion must have a base case
⭐ Read if you have time
  • Stack overflow
  • Tail-call optimization
  • Callback functions
πŸ’ͺ Not getting it first time is normal
Recursion β€” a function calling itself β€” feels mysterious until you've seen it a few times.
How to retry
  1. Solidify basic functions first
  2. Trace factorial (n!) on paper: expand fact(5) down to fact(1)
  3. Confirm there's always a base case
  4. Start with small depths (3-5)
  5. Notice the same logic can be written iteratively; write both
πŸ’‘ Tip: 90% of recursion bugs come from "no / unreachable base case" or "forgot to shrink the argument." Check those first.

Functions with return values

A function takes parameters, performs a computation, and can return a value.
int square(int n) {    // return type: int
  return n * n;        // return a value
}
int main(void) {
  int r = square(5);  // call the function; r becomes 25
  printf("%d\n", r);
}
Argument
Value passed to the function. Goes in the parentheses.
Return value
Result the function returns, via the return statement.
name(args)
Call expression. Its value is the return value.

void — "nothing"

Use void to indicate "no parameters" or "no return value".
// no return, no parameters
void sayHello(void) {
  printf("Hello!\n");     // return can be omitted
}

// no return, with parameter
void printBox(int n) {
  for(int i=0; i<n; i++) printf("*");
  printf("\n");
}

// returns a value, no parameters
int getAnswer(void) {
  return 42;
}
main function: int main(void) means "no parameters, returns int".

Multiple parameters & types

You can pass multiple parameters separated by commas. Each one needs its type.
double triangleArea(double base, double height) {
  return base * height / 2.0;
}

int main(void) {
  double area = triangleArea(6.0, 4.0);
  printf("area = %f\n", area); // 12.000000
}
Match the types: Passing a double (e.g. 3.5) to an int parameter truncates the decimal part.

Memory during a call (call stack)

When a function is called, a stack frame is created for it, and released when it returns.
Click "Next step" to follow the function-call flow
Key points:

Try it yourself — advanced functions

funcs.c
Output
Click "Run" to execute...
πŸ’‘ Try these ideas too
Advertisement

Related Lessons

Functions
Lesson 13: Functions (Basics)
Defining and calling C functions.
Functions
Lesson 15: Prototype & Macro
Learn C function prototypes and the #define macro.
Functions
Global Variables & Scope
C global variables, scope, variable lifetime, and static.
← Previous lesson
Lesson 20: Functions (Basics)
Next lesson →
Lesson 22: Prototype & Macro

Review Quiz

Check your understanding of this lesson!

Q1. When you pass an array to a C function, what is actually passed?

A copy of the array
The address of the first element
The array size

In C, an array name decays to a pointer to its first element. The whole array is not copied.

Q2. What is essential for a recursive function?

A global variable
A base case (stopping condition)
A pointer

A recursive function calls itself, so without a base case you get infinite recursion and a stack overflow.

Q3. Which is a typical use of function pointers?

Freeing memory
Implementing callbacks
String manipulation

Function pointers hold the address of a function and are used for callbacks, dispatch tables, and similar patterns.

Share this article
Share on X (Twitter) Share on Facebook Send on LINE Hatena

Recommended Books to Deepen Your Understanding

Combine this interactive site with books for more practice.

πŸ“˜
Painfully Learning C
by MMGames
A classic C book for beginners. Builds solid fundamentals with clear explanations.
View on Amazon
πŸ“—
New Clear C Language (Beginner)
by Bohyoh Shibata
Plenty of diagrams and exercises. Widely used as a university textbook.
View on Amazon
πŸ“™
The C Programming Language, 2nd Ed.
by Brian Kernighan & Dennis Ritchie
Known as K&R. The original C book. Ideal after completing the basics.
View on Amazon

* Links above are affiliate links. Purchases help support this site.