Local & Global Functions

Functions & Variables.

* Before starting to learn about local and global functions in C, you must have a clear concept of local and global variables.

Local & Global Variables

Variables can be categorized into two main types: local variables and global variables. These two types of variables have different scopes and lifetimes, which determine where and how they can be accessed within a program.

Local Variables:

  • Local variables are declared within a specific block of code, such as a function or a compound statement (enclosed within curly braces {}).
  • They have a limited scope, meaning they are only accessible within the block in which they are declared. Outside of this block, the variable is not visible or usable.
  • Local variables are typically used for temporary storage of data within a specific function or block. They are only alive (exist in memory) while the block in which they are declared is executing.

Example of a local variable:

c Copy Code
#include <stdio.h>

int main() {
    int x = 10; // x is a local variable
    printf("The value of x is %d\n", x);
    return 0;
}
Output:
The value of x is 10

* Local variables are automatically initialized with garbage values if not explicitly initialized by the programmer.

Global Variables

  • Global variables are declared outside of any function or block, typically at the beginning of a C program or in a header file that is included in multiple source files.
  • They have a global scope, meaning they can be accessed from any part of the program, including multiple functions.
  • Global variables have a lifetime equal to the entire duration of the program. They are initialized once at the start of the program and remain in memory until the program terminates.

Example of a global variable:

c Copy Code
#include <stdio.h>

int x = 10; // x is a global variable

void func1(){
    printf("The value of x is %d\n", x);
}
void func2(){
    x = 20; // Modifying the global variable
}

int main() {
    func1();
    func2();
    printf("Now The value of x is %d\n", x);
    return 0;
}
Output:
The value of x is 10    
Now The value of x is 20

* It's considered good practice to declare global variables as 'static' if they should only be accessible within the current source file to limit their scope.

Local & Global Functions

In C programming, functions can be categorized into two main types: local functions and global functions. These categories refer to the scope and visibility of functions within a C program.

Local Functions:

  • Local functions are also known as "static functions" or "internal functions."
  • These functions are defined within a specific block of code, typically within a single source file.
  • They are only visible and accessible within the file where they are defined. Other source files cannot see or call these functions.
  • Local functions are declared using the 'static' keyword before the function declaration. This keyword tells the compiler to restrict the scope of the function to the current file.
  • Local functions are useful for encapsulating functionality that should not be exposed to other parts of the program.

Here's an example of a local function in C:

c Copy Code
#include <stdio.h>

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

int main() {
    int result = add(5, 3);
    printf("Result: %d\n", result);
    return 0;
}
Output:
Result: 8

* The 'add' function is defined as a local function within the same source file as 'main'. It cannot be accessed from other source files.

Global Functions:

  • Global functions are functions that are not restricted to a single source file; they are declared outside of any specific block or function.
  • Global functions are accessible from any part of the program, including other source files.
  • These functions are typically declared in header files and defined in separate source files.
  • Global functions are commonly used for providing reusable functionality that can be used throughout a program.

Here's an example of a global function in C:

math.h
h (Header File) Copy Code
 #ifndef MATH_H
 #define MATH_H
 
 int add(int a, int b);
 
 #endif
math.c
c (Source File) Copy Code
#include "math.h"

int add(int x, int y)
{
    return x + y;
}
main.c
c (Source File) Copy Code
#include <stdio.h>
#include "math.h"

int main()
{
    int result = add(10, 15);
    printf("Result: %d", result);
    return 0;
}
Output:
Result: 25

In this example, 'add' is a global functions declared in the header file 'math.h' and defined in the source file 'math.c'. It can be used in other source files, such as 'main.c'.

What is New?

We have just updated this website & still working on adding new content & updating existing features.