Functions
1. What is a function?
So far, all the code we have written has been placed inside int main() { ... }. This works for small programs, but as the code grows, keeping everything in a single block becomes chaotic.
A function is like a "mini-program" within your code that handles a specific task.
To understand how a function works, imagine a coffee machine. We have a coffee machine, and we need to provide it with water and coffee powder for it to give us a ready-to-drink coffee. A function works exactly the same way. It receives parameters (water and coffee powder) and returns a result.
Why do we use functions? 1. Reusability: If we need to calculate the least common multiple 10 times, we don't want to write the same code 10 times. We write it once and call the function. 2. Order and readability: It allows us to break down a large, complex problem into smaller, easily solvable problems.
2. Function Syntax
The function must be defined and declared before main(). It has the following structure:
- Pre: In the precondition (
pre), we state what the code or parameters must satisfy for the function to execute correctly. If the function always works regardless of the input, we writetrue. - Post: Explains what the function does, what it returns as a result, or which variables it modifies.
- Return type: What will the function return? An
int,double,string,bool, orchar? - Function name: A descriptive name (e.g.,
calculateArea,isEven). - Parameters: The data the function needs to work, placed inside parentheses. If the function doesn't need any data, the parentheses are left empty
(). - return: The keyword that sends the result out of the function. Once
returnis executed, the function stops immediately.
Parameters
Parameters can be passed by:
- Value: A temporary copy is made, and it does not modify anything in main.
- Reference: No copy is made, meaning we modify the actual parameter received from
main.
3. Procedures (void)
Sometimes we want a function to perform a specific action (like printing a message to the console, drawing something, or modifying an external variable) but we don't need it to return any value.
In these cases, instead of using int or string as the return type, we use void ("empty").
void functions do not include the return result; statement. (At most, they can include an empty return; to stop the function early).
4. Examples
1. SumaVariables.cpp
2. MCD.cpp
IMPORTANT NOTE: THIS CODE IS NOT OPTIMIZED, IT IS JUST TO SHOW AN EXAMPLE.
3. IsPrime.cpp
#include <iostream>
using namespace std;
//pre: a >= 2
//post: returns true if the number is prime; if not, returns false.
bool isPrime(int a){
if (a < 2){ // BASE CASE: if 'a' is less than 2, then it is not prime.
return false;
}
int counter = 0;
for (int i = 1; i <= a; i++){ // We check how many divisors does 'a' have.
if (a % i == 0){
counter++;
}
}
if (counter > 2){ // If 'a' has more than 2 divisors, then it is not prime.
return false;
} else{
return true;
}
}
int main(){
int n1;
cout << "Enter an integer number: ";
while (cin >> n1){
cout << endl;
if (isPrime(n1)){
cout << n1 << " is prime." << endl;
} else{
cout << n1 << " is NOT prime." << endl;
}
cout << "Enter an integer number: ";
}
}
4. HiName.cpp
5. Swap.cpp
There is no final.cpp for this topic since it is purely theoretical.