Course Content
C++ Introduction
0/1
C++ Variables & Constants
0/1
C++Scope of Variable
0/1
C++ Keywords & Identifiers
0/1
C++ Data Types
0/1
C++ Basic I/O
0/1
C++ Type Conversion
0/1
C++ Operators
0/1
C++ Comments
0/1
C++ If-else
0/1
C++ Ternary Operator
0/1
C++ for Loop
0/1
C++ Ranged for Loop
0/1
C++ while/do-while Loop
0/1
C++ break Statement
0/1
C++ Continue Statement
0/1
C++ switch Statement
0/1
C++ goto Statement
0/1
C++ Functions
0/1
C++ User-defined Functions
0/1
C++ Default Arguments
0/1
C++ Storage Class
0/1
C++ Recursion
0/1
C++ Return by Reference
0/1
C++ Arrays
0/1
C++ Multi-dimentional Arrays
0/1
C++ Arrays & Function
0/1
C++ String
0/1
C++ Structure
0/1
C++ Structure & Functions
0/1
C++ Pointers to Structure
0/1
C++ Pointers
0/1
C++ Void Pointers
0/1
C++ Pointers & Arrays
0/1
C++ Pointers & Functions
0/1
C++ Dynamic Memory Allocation
0/1
C++ OOPs Concepts
0/1
C++ Objects and Class
0/1
C++ Constructors
0/1
C++ Destructors
0/1
C++ Constructor Overloading
0/1
C++ Objects & Function
0/1
C++ Enumeration
0/1
C++ Inheritance
0/1
C++ Inheritance Access Control
0/1
C++ Inheritance Types
0/1
C++ Polymorphism
0/1
C++ Function Overloading
0/1
C++ Function Overriding
0/1
C++ Operator Overloading
0/1
C++ Friend Function
0/1
C++ Virtual Function
0/1
C++ Abstract Class & Pure Virtual Function
0/1
C++ Encapsulation
0/1
C++ Abstraction
0/1
C++ Templates
0/1
C++ Exception Handling
0/1
C++ Multithreading
0/1
C++ Standard Library
0/1
C++ Programming Tutorials
About Lesson

C++ User Defined Function Types

In this tutorial, you will learn about different approaches you can take to solve a single problem using functions.


User Defined Function Types

There can be 4 different types of user-defined functions, they are:
1) Function with no argument and no return value
2) Function with no argument but return value
3) Function with argument but no return value
4) Function with argument and return value

Below, we will discuss about all these types, along with examples.
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.


1. Function with No arguments passed and no return value

  • In this type of function each function are independent.
  • They need the data, data values, calculate the result and display the same block.
  • These functions can either be used to display information or they are completely dependent on the user.

Example 1: Function with No arguments passed and no return value

// This program to check if you are 18 or not using goto statement.

# include <iostream>
using namespace std;
void prime();
int main(){
    // No argument is passed to prime()
    prime();
    return 0;
}

// Return type of function is void because value is not returned.
void prime(){
    int num, i, a = 0;

    cout << "Enter a positive integer enter to check: ";
    cin >> num;

    for(i = 2; i <= num/2; ++i)
    {
        if(num % i == 0)
        {
            a = 1; 
            break;
        }
    }
    if (a == 1)
    {
        cout << num << " is not a prime number.";
    }
    else
    {
        cout << num << " is a prime number.";
    }
}

Output 1

Enter a positive integer enter to check: 2
2 is a prime number.

Output 2

Enter a positive integer enter to check: 4
4 is not a prime number.

Working of above program

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.


2. Function with No arguments passed but a return value

  • In this type of function the number of argument are pass through the calling function to the called function but the called function returns value.
  • The variable required for the called function that are declared and initialize the same called function module. The called function is independent.

Example 2: Function with No arguments passed but a return value

#include <iostream>
using namespace std;
int prime();
int main()
{
    int num, i, a = 0;
    // No argument is passed to prime()
    num = prime();
    for (i = 2; i <= num/2; ++i)
    {
        if (num%i == 0)
        {
            a = 1;
            break;
        }
    }
    if (a == 1)
    {
        cout<<num<<" is not a prime number.";
    }
    else
    {
        cout<<num<<" is a prime number.";
    }
    return 0;
}

// Return type of function is int
int prime()
{
    int n;

    printf("Enter a positive integer to check: ");
    cin >> n;

    return n;
}

Output 1

Enter a positive integer enter to check: 2
2 is a prime number.

Output 2

Enter a positive integer enter to check: 4
4 is not a prime number.

Working of above program

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.


3. Function with Arguments passed but no return value

  • This type of function, arguments are passed through the calling function to called function but does not returns value.
  • Such type of function practically dependent on each other.

Example 3: Function with No arguments passed but a return value

#include <iostream>
using namespace std;
void prime(int n);
int main(){
    int num;
    cout << "Enter a positive integer to check: ";
    cin >> num;
    
    // Argument num is passed to the function prime()
    prime(num);
    return 0;
}
// There is no return value to calling function. Hence, return type of function 
is void.
void prime(int n){
    int i, a = 0;
    for (i = 2; i <= n/2; ++i)
    {
        if (n%i == 0)
        {
            a = 1;
            break;
        }
    }

    if (a == 1)
    {
        cout << n << " is not a prime number.";
    }
    else {
        cout << n << " is a prime number.";
    }
}

Output 1

Enter a positive integer enter to check: 3
3 is a prime number.

Output 2

Enter a positive integer enter to check: 6
6 is not a prime number.

Working of above program

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.


4. Function with Arguments passed and a return value.

  • This is the best type, as this makes the function compeletely independent of inputs and outputs, and only the logic is defined inside the function body.
  • Both functions are dependent on each other.

Example 4: Function with Arguments passed and a return value.

#include <iostream>
using namespace std;

int prime(int n);
int main(){
    int num, a = 0;
    cout << "Enter positive integer to check: ";
    cin >> num;

    // Argument num is passed to check() function
    a = prime(num);
    if(a == 1)
        cout << num << " is not a prime number.";
    else
        cout<< num << " is a prime number.";
    return 0;
}
/* This function returns integer value.  */
int prime(int n)
{
    int i;
    for(i = 2; i <= n/2; ++i)
    {
        if(n % i == 0)
            return 1;
    }

    return 0;
}

Output 1

Enter a positive integer enter to check: 3
3 is a prime number.

Output 2

Enter a positive integer enter to check: 6
6 is not a prime number.

Working of above program

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 a, and the corresponding text is printed onto the screen.

Exercise Files
No Attachment Found
No Attachment Found
error: Content is protected !!