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

Lesson 25: 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
πŸ’ͺ It's normal not to get it on the first try
Recursion β€” a function calling itself β€” feels mysterious until you've seen it in action a few times.
How to approach it again
  1. Make sure you're solid on 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 to 5)
  5. Notice that the same logic can be written iteratively β€” try writing both versions
πŸ’‘ Tip: 90% of recursion bugs come down to a missing or unreachable base case, or forgetting to shrink the argument. Check those first.

Functions with return values

A function takes parameters, performs a computation, and can return a value to the caller.
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
The value passed to the function. Goes inside the parentheses.
Return value
The result the function sends back, via the return statement.
name(args)
A call expression. Its value is whatever the function returns.

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;
}
The main function: int main(void) means "no parameters, returns an int."

Multiple parameters & types

You can pass multiple parameters separated by commas. Each one needs its own type declaration.
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 (for example, 3.5) to an int parameter truncates the fractional part.

Memory during a call (call stack)

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

Try it yourself — advanced functions

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

Related Lessons

Functions
Lesson 24: Functions (Basics)
Defining and calling C functions.
Functions
Lesson 28: 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 24: Functions (Basics)
Next lesson →
Lesson 26: Elementary Functions (math.h)

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, so the whole array isn't 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 end up with 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. They're commonly used for callbacks, dispatch tables, and similar patterns.

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