Friday, 19 December 2025

Functions and Program Structure in C++

1. Program Structure in C++

A C++ program follows a well-defined structure. The basic components are:

#include <iostream>
using namespace std;
int main() 
{
    cout << "Hello, World!";
    return 0;
}

Main Parts
Documentation Section
Comments describing the program.

// This program prints Hello World

Preprocessor Directives
Include header files.
#include <iostream>

Global Declarations
Global variables, function declarations.
int sum(int, int);

main() Function
Execution starts here.
int main() 
{
    // statements
    return 0;
}

User-Defined Functions
Functions written by the programmer.

2. Functions in C++
A function is a block of code that performs a specific task and can be reused.

Advantages of Functions
  • Code reusability
  • Reduces program size
  • Easy debugging and testing
  • Improves readability
3. Types of Functions
(a) Library (Built-in) Functions
Provided by C++ libraries.
Examples:
  • cout, cin (iostream)
  • sqrt(), pow() (cmath)
  • strlen() (cstring)
(b) User-Defined Functions
Created by the programmer.

4. Function Components
(a) Function Declaration (Prototype)
int add(int, int);

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

(c) Function Call
int result = add(5, 3);

5. Types of User-Defined Functions
a. No arguments, no return value
void show() 
{
    cout << "Welcome";
}

b. Arguments, no return value

void sum(int a, int b) 
{
    cout << a + b;
}

c. No arguments, return value
int getNumber() 
{
    return 10;
}

d. Arguments and return value
int multiply(int a, int b) 
{
    return a * b;
}

6. Passing Arguments to Functions
(a) Call by Value
A copy of the value is passed.
void change(int x) 
{
    x = 10;
}

(b) Call by Reference
The address is passed.
void change(int &x) 
{
    x = 10;
}

7. Inline Functions
Replaces function call with function code.
inline int square(int x) 
{
    return x * x;
}

8. Recursive Functions
A function calling itself.
int factorial(int n) 
{
    if (n == 0)
        return 1;
    return n * factorial(n - 1);
}

9. main() Function Return Type
int main() returns an integer to OS
"return 0;" means successful execution

10. Example Program
#include <iostream>
using namespace std;
int add(int, int);
int main() {
    int a = 4, b = 6;
    cout << "Sum = " << add(a, b);
    return 0;
}
int add(int x, int y) 
{
    return x + y;
}

1. Arguments in C++
Arguments are values passed to a function when it is called so that the function can perform its task.

There are two types of arguments:
  • Actual Arguments
  • Formal Arguments
2. Actual Arguments
  • The values or variables passed in the function call.
  • They appear in the calling function (usually main()).
  • They send data to the function.
Example
add(5, 10);

Here, 5 and 10 are actual arguments.

3. Formal Arguments
  • The variables are used in the function definition to receive values.
  • They appear in the called function.
  • They receive data from actual arguments.
Example
int add(int a, int b) 
{
    return a + b;
}

Here, a and b are formal arguments.

4. Example Program
#include <iostream>
using namespace std;

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

int main() {
    int a = 10, b = 20;
    cout << add(a, b);
    return 0;
}

int add(int x, int y) { // x, y → formal arguments
    return x + y;
}
  • a, b → Actual arguments
  • x, y → Formal arguments
5. Key Differences
Actual Arguments                  Formal Arguments
Used in function call                 Used in function definition
Send values to function                                     Receive values from caller
Can be constants, variables, or expressions Always variables
Exist in calling function                 Exist in called function

6. Call by Value (Using Actual & Formal Arguments)
void change(int x) 
{
    x = 50;
}

int main() {
    int a = 10;
    change(a);
    cout << a;   // Output: 10
}
  • a → actual argument
  • x → formal argument
Value of a does not change

7. Call by Reference
void change(int &x) 
{
    x = 50;
}
int main() 
{
    int a = 10;
    change(a);
    cout << a;   // Output: 50
}

Changes are reflect in actual argument

8. Exam-Oriented Definition
Actual arguments are the values passed to a function during function call, whereas
Formal arguments are the variables that receive these values in the function definition.

1. Variables in C++
A variable is a named memory location used to store data. Based on their scope, variables in C++ are mainly of two types:
  • Local Variables
  • Global Variables
2. Local Variables
  • Declared inside a function or block.
  • Can be used only within that function or block.
  • Created when the function is called and destroyed when it ends.
  • Have higher priority than global variables inside the function.
Example
#include <iostream>
using namespace std;
void show() 
{
    int x = 10;   // Local variable
    cout << x;
}
int main() {
    show();
    return 0;
}

Here, x is a local variable.

3. Global Variables
  • Declared outside all functions, usually at the top of the program.
  • Accessible by all functions in the program.
  • Exist for the entire program execution.
  • The default value is 0 if not initialized.
Example
#include <iostream>
using namespace std;

int x = 50;   // Global variable

void display() 
{
    cout << x;
}
int main() {
    display();
    return 0;
}

Here, x is a global variable.

4. Local vs Global Variable Example
#include <iostream>
using namespace std;

int x = 100;   // Global variable

void test() 
{
    int x = 20;   // Local variable
    cout << x << endl;
}
int main() {
    test();
    cout << x;
    return 0;
}
Output
20
100

The local variable hides the global variable inside the function.

5. Key Differences
Local Variables                               Global Variables
Declared inside function/block        Declared outside all functions
Accessible only within the block        Accessible throughout the program
Created and destroyed with function    Exists till program ends
No default value                                        The default value is 0
Safer to use                                                 Risky if overused

6. Scope Resolution Operator (::)
Used to access global variables when a local variable has the same name.

Example
#include <iostream>
using namespace std;
int x = 10;
int main() 
{
    int x = 5;
    cout << x << endl;     // Local x
    cout << ::x << endl;   // Global x
    return 0;
}

7. Exam-Oriented Definition
Local variables are declared inside a function and can be accessed only within that function,
whereas global variables are declared outside all functions and can be accessed throughout the program.

0 comments

Post a Comment