Saturday, 20 December 2025

Multifunction Program in C++

1. Meaning of Default Arguments

Default arguments are values given to function parameters at the time of function declaration.
If no value is passed during the function call, the default value is automatically used.

2. Syntax
return_type function_name(type parameter = default_value);

3. Simple Example
#include <iostream>
using namespace std;
void display(int x = 10) {
    cout << x;
}
int main() 
{
    display();      // uses default value
    display(25);    // uses passed value
    return 0;
}
Output
10
25

4. Multiple Default Arguments
#include <iostream>
using namespace std;
void add(int a, int b = 5, int c = 10) 
{
    cout << a + b + c;
}
int main() {
    add(2);          // 2 + 5 + 10
    add(2, 3);       // 2 + 3 + 10
    add(2, 3, 4);    // 2 + 3 + 4
    return 0;
}

5. Example with Function Declaration
#include <iostream>
using namespace std;
void show(int x, int y = 20);   // Default argument
int main() 
{
    show(5);
    show(5, 10);
    return 0;
}
void show(int x, int y) {
    cout << x << " " << y << endl;
}

6. Advantages
  • Simplifies function calls
  • Reduces code length
  • Improves readability
7. Exam-Oriented Definition
Default arguments are values assigned to function parameters that are used automatically when no corresponding actual argument is passed.

1. Meaning of Multifunction Program
A multifunction program is a C++ program that contains more than one user-defined function, where each function performs a specific task and is called from main() or from another function.

2. Advantages of Multifunction Programs
  • Code reusability
  • Better program structure
  • Easy debugging
  • Improves readability
  • Suitable for large programs
3. Structure of a Multifunction Program
  • Function declarations (prototypes)
  • main() function
  • Function definitions
4. Example: Simple Multifunction Program
#include <iostream>
using namespace std;
// Function declarations
void input(int &, int &);
int add(int, int);
void display(int);
int main() 
{
    int a, b, sum;
    input(a, b);           // Function call
    sum = add(a, b);       // Function call
    display(sum);          // Function call
    return 0;
}
// Function definitions
void input(int &x, int &y) 
{
    cout << "Enter two numbers: ";
    cin >> x >> y;
}
int add(int x, int y) 
{
    return x + y;
}
void display(int s) 
{
    cout << "Sum = " << s;
}

5. Explanation
  • input() → takes input from user
  • add() → calculates sum
  • display() → displays result
  • main() → controls program flow
  • This program uses three user-defined functions, so it is a multifunction program.
6. Another Example: Menu-Driven Multifunction Program
#include <iostream>
using namespace std;
int add(int, int);
int subtract(int, int);
int main() 
{
    int a = 10, b = 5;
    cout << "Addition = " << add(a, b) << endl;
    cout << "Subtraction = " << subtract(a, b);
    return 0;
}
int add(int x, int y) 
{
    return x + y;
}
int subtract(int x, int y) 
{
    return x - y;
}

7. Exam-Oriented Definition
A multifunction program is a program that consists of two or more user-defined functions, each performing a specific task.

8. Important Points for Exams
  • Functions should be declared before main()
  • Use meaningful function names
  • Avoid global variables if possible
  • Follow modular programming concept

0 comments

Post a Comment