C Functions

Beginner
23 min read
Updated: Nov 2026
1 example
17 guides

Definition and Usage

A function is a named block of code you can call many times. Functions split a program into small, testable pieces. This section covers declaring and defining functions, passing data by value and by reference (with pointers), working with arrays and variable arguments, scope and lifetime, the static and extern keywords, and recursion.

Static and extern live here.

Open "static and extern" for persistent local variables and file-private functions, and "Pass by reference" for the pointer technique that lets a function change its caller's variables.

Learning Path

Work through the stages in order, or jump to a topic from the index below (17 tutorials).

C Examples

A quick taste of functions. Compile with gcc file.c -std=c11 -Wall -o out.

Example 1 — Define and call a function

A prototype, a call from main, and the definition.

C
#include <stdio.h>

double average(int a, int b);

int main(void) {
    printf("average = %.1f\n", average(7, 10));
    return 0;
}

double average(int a, int b) {
    return (a + b) / 2.0;
}

How It Works

  1. The prototype tells the compiler the function's name, parameters, and return type.
  2. main passes the arguments 7 and 10.
  3. Dividing by 2.0 keeps the fractional part, so the result is 8.5.

C Functions Topics

Every guide in this section with a one-line summary (17 tutorials).

Writing functions

6 tutorials

Declare, define, call, and return from your own functions.

TopicDescriptionTutorial
Function basicsCreate reusable blocks of code with a return type, name, and parameters.Try it
Declaration and definitionTell the compiler a function exists, then provide its body.Try it
Function callsInvoke a function with arguments and use its result.Try it
Parameters and argumentsUnderstand formal parameters, actual arguments, and conversions.Try it
Return valuesSend results back with return, including early returns.Try it
Function prototypesDeclare a function signature before its first use.Try it

Passing data

5 tutorials

Pass by value, pass by reference, arrays, and variable argument lists.

TopicDescriptionTutorial
Pass by valueC copies arguments, so changes inside a function do not reach the caller.Try it
Pass by referencePass addresses so a function can modify the caller's variables.Try it
Arrays as parametersArrays decay to pointers, so pass the length too.Try it
Variadic functionsAccept a variable number of arguments with stdarg.h.Try it
Inline functionsSuggest that the compiler expand a small function at its call site.Try it

Scope and storage

5 tutorials

Where names are visible, how long objects live, and what static does.

TopicDescriptionTutorial
Variable scopeWhere a name is visible: block, function, and file scope.Try it
Lifetime and linkageHow long an object lives and which files can see its name.Try it
Storage classesauto, register, static, and extern at a glance.Try it
auto and registerThe default local storage class and the register hint.Try it
static and externPersistent static locals, file-private names, and sharing globals with extern.Try it

Recursion

1 tutorial

Functions that call themselves.

TopicDescriptionTutorial
RecursionFunctions that call themselves, with a base case to stop.Try it

Edge Cases & Pitfalls

Common mistakes when learning functions in C.

proto

Missing prototype

Calling a function before it is declared causes warnings or errors. Declare it above main or in a header.

value

Expecting changes to persist

Arguments are copied. To change the caller's variable, pass its address.

ret

Not returning a value

Falling off the end of a non-void function and using the result is undefined behavior.

Key Takeaways

  • Reuse: functions remove duplicate code.
  • Copies: arguments are passed by value.
  • Pointers: use addresses to modify caller variables.

One line: A function is a named block of code you can call many times.

Frequently Asked Questions

Functions make code reusable, easier to test, and easier to read. Each function should do one clear job.
Not directly. Return a struct, or pass pointers so the function can write several results back to the caller.
A parameter is the variable in the function definition. An argument is the value you pass when calling it.

Next: Function Basics

Create reusable blocks of code with a return type, name, and parameters.

Continue →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful