C++ Program to check whether a number is a prime or composite number.

Pass-by-Value

In the pass-by-value approach, the function receives a copy of the argument, and any changes made to the parameter inside the function do not affect the original argument.

Pass-by-Reference

In the pass-by-reference approach, the function receives a reference to the argument, allowing modifications to the argument inside the function to affect the original variable.

Explanation

  • Pass-by-Value: The isPrime function receives a copy of number. It performs the prime-checking logic on this copy. The original variable in main remains unaffected by changes within isPrime.
  • Pass-by-Reference: The isPrime function receives a reference to number. Although the logic is the same as in the pass-by-value version, here it operates directly on the original variable. Any changes to num within isPrime would reflect in main, but in this case, there are no modifications to num.

Sample Output

Assuming you enter 7 as the number:

For Pass-by-Value:

For Pass-by-Reference: