User defined functions in C++

C++ User-defined functions

User-defined functions in C++ are functions that are created by the user to perform a specific task. These functions can be called from anywhere within the program, just like built-in functions. In C++, you can define your own functions to perform specific tasks.

Function definitions can be placed anywhere in a C++ program, but it is common to define all functions at the beginning of the program before the main function. This allows the program to use the functions before they are defined.

Transform in C++ STL

User defined functions in C++

In C++, a user-defined function is a block of code that performs a specific task and can be called multiple times in a program.

Syntax:

return_type function_name(parameter list) {
   // function body
}
  • The return_type specifies the data type of the value that the function returns. If the function does not return a value, the return_type should be void.
  • The function_name is the name by which the function can be called.
  • The parameter list specifies the input arguments that are passed to the function.
  • The function body contains the code that is executed when the function is called.

User-defined functions Types

In C++, there are 4 different types of user-defined functions which are:

1) Function with no parameter and no return value

A user-defined function in C++ that has no parameters and no return value, also known as a void function, is a block of code that performs a specific task but does not take any input and does not return any output.

Example 1:

Run
#include <iostream>
using namespace std;

void print_message() {
   cout << "PrepInsta Prime" << endl;
}

int main() {
   print_message(); // Calling the function
   return 0;
}

Output:

PrepInsta Prime

2) Function with no parameter but return value

A user-defined function in C++ that has no parameters but has a return value, is a block of code that performs a specific task and returns a value of a specific data type but doesn’t take any input.

Example 2:

Run
#include <iostream>
#include <cstdlib>
using namespace std;

int get_random_number() {
   return rand() % 10 + 1;
}

int main() {
   cout << "Random number: " << get_random_number() << endl;
   return 0;
}

Output:

Random number: 4

3) Function with parameter but no return value

A user-defined function in C++ that has parameters and no return value, also known as a void function, is a block of code that performs a specific task and takes input in the form of function parameters, but does not return any output.

Example 3:

Run
#include <iostream>
using namespace std;

void print_string(string input) {
   cout << "Input: " << input << endl;
}

int main() {
   string message = "PrepInsta Prime";
   print_string(message); // Calling the function
   return 0;
}

Output:

Input: PrepInsta Prime

4) Function with parameter and return value

A user-defined function in C++ that has parameters and a return value is a block of code that performs a specific task, takes input in the form of function parameters, and returns a value of a specific data type.

Example 4:

Run
#include <iostream>
using namespace std;

int multiply(int x, int y) {
   return x * y;
}

int main() {
   int a = 2, b = 3;
   int result = multiply(a, b);
   cout << "Result: " << result << endl;
   return 0;
}

Output:

Result: 6

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription