ASSIGNMENT
ASSIGNMENT
Q.2 What is User Define Function? Write a Difference Between Structure and Union.
Answer :-Â
A user-defined function in C++ is a function created by the programmer to perform a specific task.
Unlike pre-defined functions (e.g., `main()` or library functions like `sqrt()`), user-defined functions allow developers to encapsulate reusable code blocks, improve code readability, and reduce redundancy.
Structure of a User-Defined Function.
Return Type: Specifies the type of value the function will return (e.g., `int`, `float`, `void`).
Function Name: A unique identifier for the function.
Parameter List: Variables passed to the function as input (optional).
Function Body: Contains the code to be executed when the function is called.
Function Call: Invokes the function to execute its code.
Example of a User-Defined Function in C++
#include <iostream>
using namespace std;
// User-defined function to calculate the sum of two numbers
int addNumbers(int a, int b) {
return a + b; // Return the sum
}
int main() {
int x = 5, y = 7;
int sum = addNumbers(x, y); // Call the function
cout << “The sum is: ” << sum << endl;
return 0;
}
Output :-Â
The sum is: 12
| Aspect | Structure | Union |
|---|---|---|
| 1.Definition | A structure is a user-defined data type that groups related variables of different data types. | A union is a user-defined data type where all members share the same memory location. . |
| 2.Memory Allocation | Each member of the structure has its own memory allocation.. | All members share the same memory space, and the size of the union is equal to its largest member. |
| 3. Usage | Used to store different types of data together, where all members are needed simultaneously. | Used when only one member is needed at a time, to save memory. |
| 4. Access to Members | All members can be accessed and modified independently. | Only one member can be accessed or modified at a time. |
| 5. Syntax | Declared using the struct keyword. | Declared using the union keyword. |
| 6. Example Declaration | struct MyStruct { int x; float y; }; | union MyUnion { int x; float y; }; |
| 7. Example Usage | Members can be accessed and stored independently. | Modifying one member affects the others since they share the same memory. |
Example of Structure :-Â
#include <iostream>
using namespace std;
struct Student {
int id;
float marks;
};
int main() {
Student s1;
s1.id = 101;
s1.marks = 95.5;
cout << “Student ID: ” << s1.id << endl;
cout << “Student Marks: ” << s1.marks << endl;
return 0;
}
Example of Union in C++
#include <iostream>
using namespace std;
union Data {
int intVal;
float floatVal;
};
int main() {
Data d;
d.intVal = 10; // Assign a value to intVal
cout << “Integer Value: ” << d.intVal << endl;
d.floatVal = 5.5; // Assign a value to floatVal, overwriting intVal
cout << “Float Value: ” << d.floatVal << endl;
// Note: Accessing d.intVal now will result in undefined behavior
return 0;
}
Structures are ideal when all members are required simultaneously.
Unions are best suited for memory-efficient storage when only one member is needed at a time.