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

Lesson 13: Functions (Basics)

Defining and calling C functions, with parameters and return values explained visually.

Function — group code and give it a name

int add(int a, int b) {
  return a + b;
}
int main(void) {
  int result = add(3, 5);
  printf("%d\n", result); // -> 8
}
Return type
int
Function name
add
Parameters
int a, int b
return statement
return a+b;

Step execution — function call

function_demo.c

Call stack

Variable state

NameScopeValue

Standard output

 

Try it yourself — functions

my_func.c
Output
Click "Run" to execute...
Advertisement

Related Lessons

Functions
Lesson 14: Functions (Advanced)
Deeper look at C functions: pass-by-value, call stack, recursion.
Functions
Lesson 15: Prototype & Macro
Learn C function prototypes and the #define macro.
Getting Started
Lesson 3: Variables
What are variables in C? Learn int, double, and char with visual diagrams.
← Previous lesson
Lesson 12: Strings
Next lesson →
Lesson 14: Functions (Advanced)

Review Quiz

Check your understanding of this lesson!

Q1. What does a return type of void mean?

Returns an integer
Returns nothing
Error

void means "empty" — the function returns no value. The return statement can be omitted.

Q2. What are the variables declared in a function header called?

Global variables
Parameters (formal arguments)
Constants

Variables in the function definition are called "parameters". Values passed at the call site are "arguments".

Q3. What is the scope of a variable declared inside a function?

The whole program
Only within that function
The whole file

Variables declared inside a function are "local" and only exist within that function.

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.