User-Defined Functions
·
Functions
created by the programmer to perform a specific task.
·
Help
in organising code, improving readability, and avoiding repetition.
Key Components of
a User-Defined Function
A user-defined function in C++
typically consists of the following parts:
- Return
Type:
Specifies the type of value the function will return
(e.g., int, double, void if no value is returned).
- Function
Name: A
unique identifier for the function, following C++ naming rules.
- Parameters
(Optional):
Variables passed to the function to work with (can be none, one, or
multiple).
- Function
Body: The
block of code that defines what the function does, enclosed in curly
braces {}.
Steps to Use a
User-Defined Function
I. Declaration: Tell the compiler about the
function (usually done before main() or in a
header file). declared before they
are defined. This is called a function
prototype.
II.
Definition: Write the actual code for the
function.
III.
Calling: Invoke the function in your
program to execute its code.
Example:
#include
<iostream>
using
namespace std;
//
Function prototype
void
sayHello();
int
main() {
sayHello(); // Calling function before
definition
return 0;
}
//
Function definition
void
sayHello() {
cout << "Hello, World!"
<< endl;
}
Syntax of a
User-Defined Function
returnType functionName(parameter1,
parameter2, ...) {
// Function body (statements)
return value; // (if applicable)
}
Example:- Simple Function
#include <iostream>
using namespace std;
// User-defined function
void greet() {
cout << "Hello, Welcome to C++ Programming!" <<
endl;
}
int main() {
greet(); // Function call
return 0;
}
🔹 Output:
Hello, Welcome to C++ Programming!
Types of
User-Defined Functions
- Function
without Parameters and without Return Value
- Function
with Parameters but without Return Value
- Function
without Parameters but with Return Value
- Function
with Parameters and with Return Value
1. Function without Parameters and
without Return Value
#include <iostream>
using namespace std;
void showMessage() { // No parameters, no return
cout << "This is a user-defined function!" <<
endl;
}
int main() {
showMessage();
return 0;
}
2. Function with Parameters but
without Return Value
#include <iostream>
using namespace std;
void addNumbers(int a, int b)
{ // Parameters but no return value
cout << "Sum: " << a + b << endl;
}
int main() {
addNumbers(5, 7); // Function
call with arguments
return 0;
}
🔹 Output:
Sum: 12
3. Function without Parameters but
with Return Value
#include <iostream>
using namespace std;
int getNumber() { // No parameters, but returns a value
return 42;
}
int main() {
int num = getNumber();
cout << "Number: " << num << endl;
return 0;
}
🔹 Output:
Number: 42
4. Function with Parameters and
with Return Value
#include <iostream>
using namespace std;
int multiply(int x, int y) { // Parameters and return value
return x * y;
}
int main() {
int result = multiply(4, 6);
cout << "Multiplication: " << result <<
endl;
return 0;
}
🔹 Output:
Multiplication: 24
Default Parameters: You can assign default values to
parameters:
int multiply(int a, int b = 2) {
return a * b;
}
// Calling multiply(5) returns 10,
multiply(5, 3) returns 15
Pass by Value vs. Pass by Reference: Parameters can be passed by value
(copy) or reference (original variable).
void increment(int x) { x++; } //
Pass by value
void increment(int &x) { x++; }
// Pass by reference
Advantages of
User-defined Functions
- Reusability: Functions can be reused in
different parts of the program.
- Modularity: Functions break down complex
problems into smaller, manageable tasks.
- Readability: Functions make the code
easier to read and understand.
- Maintainability: Functions make it easier to
manage and update the code.
- Easy
Debugging:
Isolates errors within a function instead of the whole program.
Encapsulation: Helps in breaking complex problems into smaller parts.
Drawbacks of User-defined Functions
- Overhead: Calling functions can
introduce overhead, especially in performance-critical applications.
- Complexity: Too many functions can
sometimes make the code complex to follow.
- Debugging: Debugging issues within
functions can be challenging, especially if functions are deeply nested. ====================================================
0 Comments