Types of User-defined Functions in C++



For better understanding of arguments and return in functions, user-defined functions can be categorised as:
Consider a situation in which you have to check prime number. This problem is solved below by making user-defined function in 4 different ways as mentioned above.

Example 1: No arguments passed and no return value

  1. # include <iostream>
  2. using namespace std;
  3. void prime();
  4. int main()
  5. {
  6. // No argument is passed to prime()
  7. prime();
  8. return 0;
  9. }
  10. // Return type of function is void because value is not returned.
  11. void prime()
  12. {
  13. int num, i, flag = 0;
  14. cout << "Enter a positive integer enter to check: ";
  15. cin >> num;
  16. for(i = 2; i <= num/2; ++i)
  17. {
  18. if(num % i == 0)
  19. {
  20. flag = 1;
  21. break;
  22. }
  23. }
  24. if (flag == 1)
  25. {
  26. cout << num << " is not a prime number.";
  27. }
  28. else
  29. {
  30. cout << num << " is a prime number.";
  31. }
  32. }
In the above program, prime() is called from the main() with no arguments.
prime() takes the positive number from the user and checks whether the number is a prime number or not.
Since, return type of prime() is void, no value is returned from the function.

Example 2: No arguments passed but a return value

  1. #include <iostream>
  2. using namespace std;
  3. int prime();
  4. int main()
  5. {
  6. int num, i, flag = 0;
  7. // No argument is passed to prime()
  8. num = prime();
  9. for (i = 2; i <= num/2; ++i)
  10. {
  11. if (num%i == 0)
  12. {
  13. flag = 1;
  14. break;
  15. }
  16. }
  17. if (flag == 1)
  18. {
  19. cout<<num<<" is not a prime number.";
  20. }
  21. else
  22. {
  23. cout<<num<<" is a prime number.";
  24. }
  25. return 0;
  26. }
  27. // Return type of function is int
  28. int prime()
  29. {
  30. int n;
  31. printf("Enter a positive integer to check: ");
  32. cin >> n;
  33. return n;
  34. }
In the above program, prime() function is called from the main() with no arguments.
prime() takes a positive integer from the user. Since, return type of the function is an int, it returns the inputted number from the user back to the calling main() function.
Then, whether the number is prime or not is checked in the main() itself and printed onto the screen.

Example 3: Arguments passed but no return value

  1. #include <iostream>
  2. using namespace std;
  3. void prime(int n);
  4. int main()
  5. {
  6. int num;
  7. cout << "Enter a positive integer to check: ";
  8. cin >> num;
  9. // Argument num is passed to the function prime()
  10. prime(num);
  11. return 0;
  12. }
  13. // There is no return value to calling function. Hence, return type of function is void. */
  14. void prime(int n)
  15. {
  16. int i, flag = 0;
  17. for (i = 2; i <= n/2; ++i)
  18. {
  19. if (n%i == 0)
  20. {
  21. flag = 1;
  22. break;
  23. }
  24. }
  25. if (flag == 1)
  26. {
  27. In the above program, positive number is first asked from the user which is stored in the variable num.
    Then, num is passed to the prime() function where, whether the number is prime or not is checked and printed.
    Since, the return type of prime() is a void, no value is returned from the function.

    Example 4: Arguments passed and a return value.

    1. #include <iostream>
    2. using namespace std;
    3. int prime(int n);
    4. int main()
    5. {
    6. int num, flag = 0;
    7. cout << "Enter positive integer to check: ";
    8. cin >> num;
    9. // Argument num is passed to check() function
    10. flag = prime(num);
    11. if(flag == 1)
    12. cout << num << " is not a prime number.";
    13. else
    14. cout<< num << " is a prime number.";
    15. return 0;
    16. }
    17. /* This function returns integer value. */
    18. int prime(int n)
    19. {
    20. int i;
    21. for(i = 2; i <= n/2; ++i)
    22. {
    23. if(n % i == 0)
    24. return 1;
    25. }
    26. return 0;
    27. }
    In the above program, a positive integer is asked from the user and stored in the variable num.
    Then, num is passed to the function prime() where, whether the number is prime or not is checked.
    Since, the return type of prime() is an int, 1 or 0 is returned to the main() calling function. If the number is a prime number, 1 is returned. If not, 0 is returned.
    Back in the main() function, the returned 1 or 0 is stored in the variable flag, and the corresponding text is printed onto the screen.

    Which method is better?

    All four programs above gives the same output and all are technically correct program.There is no hard and fast rule on which method should be chosen.The particular method is chosen depending upon the situation and how you want to solve a problem.

Comments

Popular posts from this blog

C++ Templates ?

C++ Program to Print Number Entered by User

Educational Website