Loading...

C essentialsFunctions

Functions: declaration, definition & calling

12 min lesson

Next
  • A function is a named block of code that does one job, and you can run it whenever you need by calling its name.
  • Functions matter because they let you avoid repeating code, break big problems into small pieces, and give names to ideas so the program reads clearly.
  • Think of a function as a recipe in a cookbook. The recipe has a name, a list of ingredients (parameters), and steps (the body). Calling the function is like saying 'make this dish' and handing over the ingredients.
  • A declaration (also called a prototype) tells the compiler the function's name, return type and parameter types before it is used, like int add(int a, int b);.
  • A definition is the actual body with the code: int add(int a, int b) { return a + b; }.
  • Calling a function means writing its name with arguments in parentheses, like int total = add(3, 4);.
  • Parameters are the named inputs inside the function, and arguments are the actual values you pass in when calling.
  • The return type says what kind of value the function gives back; void means it returns nothing.
  • The computer jumps to the function's code, runs it with the given arguments, then jumps back to where it was called, carrying the return value.
  • A realistic use: a function that computes the total price with tax, called from many places in a shopping program.
  • A surprising detail: C passes arguments by value, so the function gets copies; changing a parameter inside the function does not change the caller's variable.
  • The example program defines a function that adds two numbers and another that prints a greeting, then calls both from main.
Keep in mind
  • Give each function one clear job and a name that says what it does.
  • Declare functions before you use them, or define them above main.
  • Remember that arguments are copied, so changes inside the function stay inside.
  • Use return to send a result back; use void when there is nothing to return.
  • Functions make programs easier to read, test and reuse. You are leveling up!
main.c
#include <stdio.h>

// Declaration (prototype): tells the compiler about add before it is used
int add(int a, int b);

// Definition of a function that prints a greeting; void means no return value
void greet(const char *name) {
    // Print a friendly message using the name parameter
    printf("Hello, %s!\n", name);
}

// Definition of add: returns the sum of two whole numbers
int add(int a, int b) {
    // Send the result back to the caller
    return a + b;
}

int main(void) {
    // Call greet with a name argument
    greet("Ada");

    // Call add with two arguments and store the result
    int total = add(3, 4);
    // Print the returned value with a label
    printf("Total: %d\n", total);

    return 0;
}

What happens: Hello, Ada! Total: 7

Breaking it down

Prototype

int add(int a, int b);

Tells the compiler the name, return type and parameter types of add before its body appears. Without it, main cannot call add if add is defined later.

void function definition

void greet(const char *name) { ... }

Defines a function that takes a string and returns nothing. void in the return type means the function does not send a value back.

Returning a value

return a + b;

Computes the sum and sends it back to the caller. The caller can then store or print it.

Calling a function

int total = add(3, 4);

Runs the add function with arguments 3 and 4. The returned 7 is stored in total.

Calling a void function

greet("Ada");

Runs greet with the string argument "Ada". No value is returned, so nothing is stored.

Parts of a function

PartMeaningExample
Return typeType of value returnedint
NameWhat the function is calledadd
ParametersInputs the function accepts(int a, int b)
BodyCode that runs when called{ return a + b; }
Return statementSends a value backreturn a + b;

A helpful analogy

A function is like a recipe in a cookbook. The recipe has a name (the function name), a list of ingredients (parameters), and steps (the body). Calling the function is like telling the cook 'make this dish' and handing over the ingredients; the cook returns a finished plate (the return value).

Nesting rules

  • 1.Declare or define a function before you call it, or the compiler will complain about an unknown name.
  • 2.Match the argument types to the parameter types; C will convert some but not all safely.
  • 3.Use void in the parameter list when a function takes no arguments, as in int main(void).
  • 4.Every non-void function must return a value on every path, or the program may behave unpredictably.

Common mistakes

Calling a function before it is declared

Avoid

int main(void) { return add(1, 2); } int add(int a, int b) { return a + b; }

Use instead

int add(int a, int b); int main(void) { return add(1, 2); } int add(int a, int b) { return a + b; }

The compiler reads top to bottom. Without a prototype, main does not know add yet and reports an error or warning.

Forgetting to return a value

Avoid

int add(int a, int b) { int s = a + b; }

Use instead

int add(int a, int b) { return a + b; }

A non-void function must return a value. Without return, the caller receives garbage and the compiler warns.

Expecting changes to parameters to affect the caller

Avoid

void bump(int x) { x++; } // caller's value does not change

Use instead

void bump(int *x) { (*x)++; } // pass address

C passes arguments by value, so the function gets a copy. To change the caller's variable, pass a pointer.

Quick tips

  • •Name functions with a verb like computeTotal or printGreeting so their job is obvious.
  • •Keep functions short; if one is longer than a screen, split it into smaller functions.
  • •Declare all prototypes at the top of the file so readers see the API at a glance.
  • •Try changing the arguments in add(3, 4) to see the total change.
  • •Use -Wall -Wextra to catch missing return statements and parameter mismatches.
Previous
Next