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

Lesson 24: Functions (Basics)

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

πŸ“– What to learn on this page
βœ… Must-know essentials
  • int add(int a, int b) { return a + b; }
  • Declare a prototype before use
  • return yields a value
⭐ Read if you have time
  • void return and parameters
  • Arguments are pass-by-value
  • Local variable scope

Function — group code and give it a name

A function is a named block of code that takes inputs, does some work, and returns a result. It behaves a lot like the functions you learned about in math class.

πŸ“ Analogy: functions in math

Remember f(x) = 2x + 1 from middle school or high school math?
f(x) = 2x + 1
f(3) → substitute 3 for x → 7
f(10) → substitute 10 for x → 21
C functions work the same way: call the name, get a computed result.

✨ f(x) = 2x + 1 in C

// Math: f(x) = 2x + 1
int f(int x) {
    return 2 * x + 1;
}

int main(void) {
    printf("%d\n", f(3));    // -> 7
    printf("%d\n", f(10));   // -> 21
    return 0;
}
Mapping: the math f(x) is a function, and so is the C int f(int x). The call syntax (f(3)) and the meaning ("plug 3 in for x") are identical.

πŸ”— Glossary mapping

Math termC termExample
function namefunction namef, add, sqrt
input / independent variableargument / parameterx in f(x)
function value /
dependent variable
return value7 in f(3) = 7
definitionfunction body{ return 2*x + 1; }
substitutionpassing an argumentf(3) = "let x=3, then compute"

🀝 Two-variable functions too

// Math: g(x, y) = x + y
int add(int a, int b) {
    return a + b;
}
// add(3, 5) is just g(3, 5) β€” returns 8
Return type
int
type of the codomain
Function name
add
like math's g
Parameters
int a, int b
independent variables
return statement
return a+b;
the function's value

⚠️ Where C functions differ from math functions

The analogy is helpful but not perfect. These differences are what make C functions more than just math.
β‘  Arguments and return values have types
In math, f(x) = 2x + 1 works whether x is an integer or a real number. In C, you declare the type of every input and output.
int f(int x) means "takes an int, returns an int." For decimal values, you'd write double f(double x) as a separate function.
β‘‘ Functions can have side effects β€” some return nothing
A math function simply maps input to output. A C function can also print to the screen, modify files, or change variables β€” these are called side effects.
void hello(void) {        // no return (void)
    printf("Hello!\n");   // side effect: output to screen
}
void means "this function returns no value" β€” a concept with no direct equivalent in math.
β‘’ Same input can give different outputs
In math, f(3) is always 7. A C function, however, can return a different value each time it's called:
int r = rand();        // random number (different each call)
int n = getchar();     // user keyboard input
time_t t = time(NULL); // current clock time
These all depend on external state (the random seed, stdin, or the system clock).
β‘£ Arguments are copied (pass by value)
In math, you just "pass x." In C, only the value is copied into the function, so changing the parameter inside the function doesn't affect the caller's variable.
void change(int x) { x = 100; }  // sets local x to 100
int main(void) {
    int a = 5;
    change(a);
    printf("%d\n", a); // -> 5 (unchanged!)
}
To actually modify the caller's variable, you need pointers (covered in a later lesson).
β‘€ Functions can take no arguments
Math rarely uses zero-argument functions, but they're common in C.
int get_year(void) {   // void = no parameters
    return 2026;
}
They're useful for fetching external data or returning a fixed value.
🎯 Takeaway: The "input → work → output" idea is shared with math. C adds types, side effects, and pass-by-value because it runs on a real machine, not in pure math.

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...
πŸ’‘ Try these ideas too

Related Lessons

Functions
Lesson 25: Functions (Advanced)
Deeper look at C functions: pass-by-value, call stack, recursion.
Functions
Lesson 28: 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 23: Quiz (Strings)
Next lesson →
Lesson 25: 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, so the return statement can be omitted.

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

Global variables
Parameters (formal arguments)
Constants

Variables declared in the function definition are called "parameters." The values passed at the call site are called "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" β€” they only exist within that function.

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