Day 44 – Functions in C++
Class 11 Computer Science
What is a Function in C++? (C++ में फ़ंक्शन क्या है?)
A function in C++ is a block of reusable code that performs a specific task. Functions make programs modular and easier to understand.
C++ में एक फ़ंक्शन कोड का पुन: उपयोग योग्य ब्लॉक है जो एक विशिष्ट कार्य करता है। फ़ंक्शन प्रोग्राम को मॉड्यूलर और समझने में आसान बनाते हैं।
Advantages of Functions in C++ (C++ में फ़ंक्शन के लाभ)
- Promotes Code Reusability (कोड का पुन: उपयोग को बढ़ावा देता है)
- Improves Modularity (मॉड्यूलैरिटी को बढ़ावा देता है)
- Makes Debugging Easier (डिबगिंग को आसान बनाता है)
- Enhances Code Readability (कोड की पठनीयता में सुधार करता है)
Structure of a Function in C++ (C++ में फ़ंक्शन की संरचना)
A function in C++ has three main components:
C++ में फ़ंक्शन के तीन मुख्य घटक होते हैं:
- Function Declaration (घोषणा): Specifies the function name, return type, and parameters.
- Function Definition (परिभाषा): Contains the actual logic or code.
- Function Call (कॉल): Invokes the function in the main program.
Example:
#include <iostream>
using namespace std;
// Function Declaration
int add(int a, int b);
// Function Definition
int add(int a, int b) {
return a + b;
}
int main() {
int result = add(10, 20); // Function Call
cout << "Result: " << result;
return 0;
}
Output:
Result: 30
Types of Functions in C++ (C++ में फ़ंक्शन के प्रकार)
- Built-in Functions: Predefined functions like
sqrt(),pow().
उदाहरण:sqrt(),pow() - User-defined Functions: Functions created by the programmer.
उदाहरण:int multiply(int a, int b);
Types of User-Defined Functions (उपयोगकर्ता द्वारा परिभाषित फ़ंक्शन के प्रकार)
- Void Functions: Do not return any value.
Syntax:void functionName() { // Code } - Return Functions: Return a value to the calling function.
Syntax:functionName() { // Code return value; } - Parameterized Functions: Accept parameters to perform operations.
Syntax:functionName( parameter) { // Code return value; }
Examples of Functions in C++ (C++ में फ़ंक्शन के उदाहरण)
Example 1: Function to Add Two Numbers
#include <iostream>
using namespace std;
int add(int a, int b) {
return a + b;
}
int main() {
int x = 10, y = 20;
cout << "Sum: " << add(x, y);
return 0;
}
Output:
Sum: 30
Example 2: Function Without Parameters
#include <iostream>
using namespace std;
void greet() {
cout << "Hello, World!";
}
int main() {
greet();
return 0;
}
Output:
Hello, World!
Example 3: Function with Parameters and Return
#include <iostream>
using namespace std;
float areaOfCircle(float radius) {
return 3.14 * radius * radius;
}
int main() {
float r = 5.0;
cout << "Area: " << areaOfCircle(r);
return 0;
}
Output:
Area: 78.5
Practice Questions
Multiple Choice Questions (MCQs)
- Which of the following is a built-in function?
(a) add() | (b) sqrt() | (c) multiply() | (d) None - What does a void function return?
(a) Integer | (b) Float | (c) Nothing | (d) String - Which keyword is used to return a value from a function?
(a) return | (b) output | (c) void | (d) None - What is a user-defined function?
(a) Provided by libraries | (b) Created by the programmer | (c) Both (a) and (b) | (d) None - What will be the output of this code?
#include <iostream> using namespace std; int multiply(int a, int b) { return a * b; } int main() { cout << multiply(2, 3); return 0; }(a) 5 | (b) 6 | (c) Error | (d) None - Which function does not return any value?
(a) Void function | (b) Parameterized function | (c) Built-in function | (d) None - What is the difference between a void and a return function?
(a) Void has parameters | (b) Void returns nothing | (c) Both return values | (d) None - Which part of a function contains executable code?
(a) Declaration | (b) Definition | (c) Call | (d) None - What does a parameterized function do?
(a) Takes inputs | (b) Returns a value | (c) Prints directly | (d) None - What is the output of this code?
#include <iostream> using namespace std; void display() { cout << "Hello World!"; } int main() { display(); return 0; }(a) Hello World! | (b) Error | (c) Nothing | (d) None
Answers to MCQs:
1: (b), 2: (c), 3: (a), 4: (b), 5: (b), 6: (a), 7: (b), 8: (b), 9: (a), 10: (a)
Short Answer Questions
- What is a function in C++?
Answer: A function is a reusable block of code that performs a specific task. - Write the syntax for declaring and defining a function.
Answer:functionName( parameter) { // Code return value; } - What are the advantages of using functions in C++?
Answer: Code reusability, modularity, improved readability, and easy debugging.
Long Answer Questions
- Explain the types of functions in C++ with examples.
- Write a program to demonstrate the use of parameterized functions in C++.
- Discuss the importance of modular programming and how functions contribute to it.
Post a Comment