C Academy · Lesson

Function Prototypes and Scope

Learn about function declarations, local and global variables, and the scope of variables in C.

Lesson 2 of 310 steps

Function Prototypes and Scope is a free C Academy lesson on CoddyKit. This is lesson 2 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C Academy learning path, and your progress syncs across the web and the CoddyKit app. The C Academy course includes 3 lessons in total.

Function Prototypes and Scope

Function prototypes help declare functions before they are used.

Variable scope determines where a variable can be accessed.

In this lesson, you will learn:

  • How to use function prototypes.
  • The difference between local and global variables.
  • How variable scope affects program behavior.
Function Prototypes and Scope — illustration 1

What is a Function Prototype?

A function prototype is a declaration of a function before it is defined.

Syntax:

returnType functionName(parameterType1, parameterType2);

Example:

int add(int, int);

Example: Function Prototype

This program demonstrates the use of a function prototype.

#include <stdio.h>

int add(int, int); // Function prototype

int main() {
    int sum = add(5, 10);
    printf("Sum: %d\n", sum);
    return 0;
}

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

Local vs Global Variables

Variables in C have different scopes:

  • Local variables: Declared inside a function, accessible only within that function.
  • Global variables: Declared outside functions, accessible throughout the program.

Example: Local Variable

This program demonstrates how a local variable is limited to a function.

#include <stdio.h>

void testFunction() {
    int x = 10; // Local variable
    printf("Inside function: %d\n", x);
}

int main() {
    testFunction();
    // printf("Outside function: %d\n", x); // Error: x is not accessible here
    return 0;
}

Example: Global Variable

This program demonstrates how a global variable is accessible across functions.

#include <stdio.h>

int globalVar = 100; // Global variable

void printGlobal() {
    printf("Global variable: %d\n", globalVar);
}

int main() {
    printGlobal();
    return 0;
}

Scope Rules in C

Scope defines where a variable can be accessed.

  • Local variables exist only inside their function.
  • Global variables exist for the entire program.
  • Block scope variables exist within loops or conditional blocks.

Best Practices

To write clean and efficient code:

  • Use global variables sparingly to avoid unintended modifications.
  • Prefer function parameters over global variables for better control.
  • Always declare function prototypes before use.

Summary

In this lesson, you learned:

  • How function prototypes help declare functions before use.
  • The difference between local and global variables.
  • The scope of variables in C.

Next, we will explore recursion in C!

Function Prototypes and Scope — illustration 10
Free to start

Learn C with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
39
Lessons
144

Frequently Asked Questions

Is the “Function Prototypes and Scope” lesson free?

Yes — the full text of “Function Prototypes and Scope” is free to read here on the web. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C Academy course, upgrade to CoddyKit PRO. The C Academy course includes 3 lessons in total.

What will I learn in “Function Prototypes and Scope”?

Learn about function declarations, local and global variables, and the scope of variables in C. You practise C Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C Academy?

No prior experience is required. C Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 3.

How long does the “Function Prototypes and Scope” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C Academy lesson?

Yes. Every C Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Defining and Calling Functions
  2. Function Prototypes and Scope
  3. Recursion in C
← Back to C Academy