Chapter 4 Functions CPP
Chapter 4 Functions CPP
A function is a block of code designed to perform a specific task. Functions improve modularity and code re
Example:
#include <iostream>
void greet() {
int main() {
greet();
return 0;
A function declaration tells the compiler about the function's name, return type, and parameters.
Example:
#include <iostream>
int main() {
return 0;
}
return a + b;
In pass-by-value, the actual value is passed. Changes inside the function do not affect the original variable
Example:
#include <iostream>
void modify(int x) {
x = x + 10;
int main() {
int a = 5;
modify(a);
cout << "Value after modify: " << a << endl; // Output: 5
return 0;
In pass-by-reference, the address of the variable is passed, allowing modifications to the original value.
Example:
#include <iostream>
x = x + 10;
int main() {
int a = 5;
modify(a);
cout << "Value after modify: " << a << endl; // Output: 15
return 0;
#include <iostream>
return a * b;
int main() {
return 0;
#include <iostream>
return a + b;
}
int main() {
return 0;