external definition examples

External Definition Examples: Real-World Uses & Illustrations

By
AD

External definitions play a crucial role in programming and software development, allowing developers to define types and classes that are used across different modules or even applications. They enable code reuse, modularity, and abstraction, making our code bases more maintainable and extensible.

Let's delve into the concept of external definitions with examples drawn from popular programming languages like C, C++, and Python.

External Definitions in C

The C programming language uses header files for external definitions. A variable declared with the "extern" keyword in one file and defined in another is an example of an external definition.

AD
Positive Externality: Definition & Examples
Positive Externality: Definition & Examples
External Conflict - Examples and Definition of External Conflict
External Conflict - Examples and Definition of External Conflict
Difference Between Ectotherms and Endotherms | Definition, Characteristics, Examples, Similarities and Differences
Difference Between Ectotherms and Endotherms | Definition, Characteristics, Examples, Similarities and Differences

Here's an example illustrating this concept:

Variable External Definition

Consider a situation where you want to share a variable between two source files - `main.c` and `external_definitions.c`. In `external_definitions.c`, you first declare the variable `extern int shared_var;`. Then, in `main.c`, you define `shared_var = 10;` and use it in your code.

What Is Enjambment? Definition & 10+ Examples
What Is Enjambment? Definition & 10+ Examples
a poster with the words euphemism and examples
a poster with the words euphemism and examples
Difference Between Internal and External Fertilization | Definition, Mechanism of Fertilization, Role
Difference Between Internal and External Fertilization | Definition, Mechanism of Fertilization, Role

The C linker will resolve the external definition during the linking stage, making `shared_var` accessible throughout both files.

Function External Definition

Functions can also be defined externally. Suppose you have a function `void print_message(char *msg);` declared in a header file `messages.h`. In a separate implementation file `messages.c`, you define the function:

AD

void print_message(char *msg) {
printf("%s\n", msg);
}

Marketing Environment Definition Factors & Examples | Marketing Tutor
Marketing Environment Definition Factors & Examples | Marketing Tutor
Understanding External Claims: Definition, Mechanics, and Examples
Understanding External Claims: Definition, Mechanics, and Examples
What Is an Eponym? Definition & 30+ Examples
What Is an Eponym? Definition & 30+ Examples
Enjambment: Definition and Examples for Writers
Enjambment: Definition and Examples for Writers
Difference Between Internal and External Environment
Difference Between Internal and External Environment
원벳원 토토사이트 후기 – 공식 1BET1 토토 가입코드 및 도메인 정보 - 토토노트
원벳원 토토사이트 후기 – 공식 1BET1 토토 가입코드 및 도메인 정보 - 토토노트

Now, you can use `print_message` in any other C file that includes `messages.h` by invoking a prototype of the function in the header file. The linker handles the external definition of the function.

External Definitions in C++

In C++, external definitions mostly occur through header files and separately compiled translation units, similar to C. However, C++ also supports external declarations and definitions via namespaces and classes, adding another layer of abstraction.

AD

Let's explore C++'s take on external definitions:

Metaphorical Thinking: Virginia Woolf's Metaphorical Term - FourWeekMBA
Metaphorical Thinking: Virginia Woolf's Metaphorical Term - FourWeekMBA
Escapism ⋆.ೃ࿔*:・
Escapism ⋆.ೃ࿔*:・
the words in black and white appear to be written on a piece of paper that says,
the words in black and white appear to be written on a piece of paper that says,
Euphemism, Definition, Types and Examples | Say it indirectly
Euphemism, Definition, Types and Examples | Say it indirectly
the words are written in black and white, with an orange cat sitting on top of it
the words are written in black and white, with an orange cat sitting on top of it
⛧ definitions
⛧ definitions
an image with the words ameliorate written in black and white on it
an image with the words ameliorate written in black and white on it
an image of the words and phrases in this text book are shown on a cell phone screen
an image of the words and phrases in this text book are shown on a cell phone screen
External Conflict Explained: Definition, Types, & Examples
External Conflict Explained: Definition, Types, & Examples
the words are written in different languages
the words are written in different languages
What Is a Double Entendre? Definition & 20+ Examples
What Is a Double Entendre? Definition & 20+ Examples
the words are written in different languages on a white background with black and white lines
the words are written in different languages on a white background with black and white lines
the words omsim are written in black on a white background with an orange border
the words omsim are written in black on a white background with an orange border

Class External Definition

Suppose you want to use a class `Person` defined in one header file `person.h` in another file `main.cpp`. The header file should look like this:

#pragma once
class Person {
public:
void introduce();
private:
std::string name;
};

AD

In `main.cpp`, you can include `person.h` and create instances of `Person`:

#include "person.h"
int main() {
Person p;
p.introduce(); // Uses the external definition
return 0;
}

Namespace External Definition

Namespaces allow organizing code into distinct scopes. If you define a function or class within a namespace in one header file and include the file in another, you're encountering an external definition:

#pragma once
namespace MyNamespace {
void greet(std::string name);
}

#include "my_namespace.h"
int main() {
MyNamespace::greet("User"); // Uses the external definition
return 0;
}

External Definitions in Python

Python's approach to external definitions revolves around modules and packages, which are simply files with a `.py` extension that are imported and used within other Python scripts.

Although modules don't support external definitions in the way C or C++ does with separate compilation units, we can still achieve a form of external definition by defining functions and classes within a module and importing them into another file. Let's illustrate this with an example:

Module External Definition

Create a Python file named `greetings.py` with the following content:

def greet(name):
print(f"Hello, {name}!")

Now, import the greet function in `main.py` and use it:

import greetings
greetings.greet("Alice")

Embracing external definitions in your coding practice promotes modularity and reusability. By encapsulating functionality within modules, classes, or functions, you make your codebase more maintainable and easier to understand.

AD