HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to CVariablesData TypesOperatorsStringsArraysConditions (if / switch)Loops (for / while)FunctionsPointers
REFERENCE
📖 Overview
🖥️ Output & Input
📦 Variables
🔢 Numbers & Math
✍️ String
📋 Array
🔁 Condition & Loop
🧩 Function
👉 Pointer
C›Functions

Functions

Function definition, parameters, and return types; defining before main.

Simple enough for a 5-year-old to understand

Functions and return

Functions are defined BEFORE main (otherwise a prototype is required). Write the return type, name, and parameters.

Example code
1
#include <stdio.h>
2
int square(int x) {
Takes an int, returns an int.
FunctionTakes an int, returns an int.
3
return x * x;
Result.
FunctionResult.
4
}
5
int main() {
6
printf("%d\n", square(5));
25
Output / Input25
7
return 0;
8
}
FunctionOutput / Input
💡 Hint: If you write the function AFTER main, declare its prototype first: int square(int);

🎯 Exercise & Practice

Test what you've learned — answer, write code, check instantly. (1 left)

1.Write a function named addUp that takes two ints and returns their sum; print addUp(8, 9).

🎯 Expected output: 17

Loading editor…

About this topic

In C, functions are declared along with their return type and parameter types. Since the compiler reads code from top to bottom, it needs to recognize a function before calling it. That is why function prototypes appear at the beginning of the file in C programs.

In C, parameters are always passed by VALUE: the function works with a copy of the variable you send. Modifying the copy does not affect the original. If you want to modify the original, you need to pass its address, and this is the most common use case for pointers in C.

Arrays seem to be the only exception to this rule, but they are not. When passing an array, the address of the first element is actually copied; the function accesses the actual array through that address. Therefore, changes made to an array parameter are reflected to the caller.

What you will learn

  • Defining a function and specifying the return type
  • Why a prototype is necessary
  • The consequences of passing by value
  • Modifying a parameter using a pointer
  • The meaning of the void return type

When to use?

If the same code is repeated in two places, it is moved to a function. This is not just about saving lines: bug fixes are made in a single place and all call sites are fixed.

Declaring functions in a header file and writing them in a separate source file is the fundamental organization style of C; the ability to compile large projects relies on this separation.

Common mistakes

Wrong

#include <stdio.h>

int main() {
    printf("%d\n", square(4));
    return 0;
}

int square(int n) {
    return n * n;
}

At the moment it is called inside main, square has not been declared yet. Older compilers would pass with a warning, but in modern standards this is an error. The solution is either to define the function before main or to write its prototype at the beginning of the file.

Correct

#include <stdio.h>

int square(int n);

int main() {
    printf("%d\n", square(4));
    return 0;
}

int square(int n) {
    return n * n;
}

Wrong

#include <stdio.h>

void increase(int value) {
    value = value + 10;
}

int main() {
    int total = 5;
    increase(total);
    printf("%d\n", total);
    return 0;
}

Since the parameter is passed by value, the function works on a copy; total does not change and five is printed to the screen. No error message is received. To change the caller's variable, you need to send its address and write through a pointer inside the function.

Correct

#include <stdio.h>

void increase(int *value) {
    *value = *value + 10;
}

int main() {
    int total = 5;
    increase(&total);
    printf("%d\n", total);
    return 0;
}

Wrong

#include <stdio.h>

int findMax(int a, int b) {
    if (a > b) {
        return a;
    }
}

int main() {
    printf("%d\n", findMax(3, 9));
    return 0;
}

If a is not greater, the function returns nothing. The return value is undefined and whatever is in memory is read. GCC gives a "control reaches end of non-void function" warning, but compilation continues. Every path returning int must contain a return.

Correct

#include <stdio.h>

int findMax(int a, int b) {
    if (a > b) {
        return a;
    }
    return b;
}

int main() {
    printf("%d\n", findMax(3, 9));
    return 0;
}

Frequently asked questions

Can a function return multiple values?

Directly no. There are two ways: collect the results in a structure and return the structure, or take the addresses of the caller's variables as parameters and write to them. The standard library usually uses the second way and keeps the return value for an error code.

What does void mean?

It means there is no return value. Tasks like printing to the screen or updating a variable are done with void functions. When written in the parameter list, it specifies that the function takes no parameters at all; leaving it empty carries a different meaning in old C.

Can I return the array I defined inside a function?

You cannot. Local arrays disappear when the function ends, and returning their address results in an invalid address; the program crashes or reads corrupted data. The correct ways are: allocating memory with malloc or writing to an array provided by the caller.

Summary

A function must be defined or its prototype written before it is called.

Parameters are passed by value; modifying the copy does not affect the original.

To modify the original, its address is passed.

If the return type is not void, every path must contain a return.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code