User-Defined Function in C++

User-Defined Function in C++
Techiio-author
Written by Sagar RabidasFebruary 10, 2022
10 min read
C++
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Techiio-author
Sagar Rabidas

Software Developer

Today we will discuss User-Defined Function in C++

C++ User-Defined Function

C++ person-defined characteristic we could customers describe their individual/personal capabilities and procedures, capabilities are the building blocks of the program, and it's most essential for modularity, code reusability while programmer can build a consumer-described feature which assists in a specific venture and it makes it easy to create a character name. In a person-described function, once the feature is invoked from the program it executes the code that is described within the body of the function. In a consumer-described function, it collects with a collection of code to perform an undertaking for that code it named as an identifier.

C++ user defined Function

The user-defined function makes programmer build their functions. The most important thing behind these functions is the programmer can create applications with reusable code. Mostly the user-defined functions are with built-in functions.

Syntax:

returntype function_name(parameter-1, parameter-2,..)
{
//body of function
}

Let’s see the simple program for calling the function,

In this simple program we declared the function display(), the return type of this code is void and in the body of function written the welcome address,

For Example

Void display() // function declaration
{
cout<<"Welcome"
}

to make use of this function we need to make a function call,

Program

#include <iostream>
using namespace std;
// to declare the function
void display()
{
cout << "Welcome to Programming";
}
int main() {
// function call
display();
return 0;
}

User-defined Functions Types

In user-defined function there are several types, they are

  • Functions with no parameters and no return value
  • Functions with no parameters and with return value
  • Functions with parameters and with no return value
  • Functions with parameters and with a return value

Functions with no parameters and no return value

Function with no parameter and with no return type which does not return value because its return type is void. In this coding, there are no parameters passed in the function Number_prime() and the function also does not return any value from the function call to the main function.

Program

# include <iostream>
using namespace std;
void Number_prime();
int main()
{
Number_prime(); // no parameters
return 0;
}
void Number_prime() // no return type - its void
{
int number, i, flag = 0;
cout << "Enter Numer to check: ";
cin >> number;
for(i = 2; i <= number/2; ++i)
{
if(number % i == 0)
{
flag = 1;
break;
}
}
if (flag == 1)
{
cout << number << " Not a Prime.";
}
else
{
cout << number << " its Prime Number.";
}
}

Functions with no parameters and with return value

In this program, Number_prime() is called from the main() function without any parameters, here the return type is an integer so it returns the integer values from the user’s input when calling the main() function.

Program

#include <iostream>
using namespace std;
int Number_prime();
int main()
{
int number, i, flag = 0;
number = Number_prime();
for (i = 2; i <= number/2; ++i)
{
if (number%i == 0)
{
flag = 1;
break;
}
}
if (flag == 1)
{
cout<<number<<" Not a Prime";
}
else
{
cout<<number<<" its Prime";
}
return 0;
}
int Number_prime() // it returns integer value
{
int n;
printf("Enter Number: ");
cin >> n;
return n;
}

Functions with parameters and with no return value

In this program, we use the same function Number_prime() to explain the code as in the name function with parameter the function Number_prime() will take with one integer value as its argument without any return value. Here the number will be passed to the function call Number_prime() to check whether the entered number is prime or not.

Program

#include <iostream>
using namespace std;
void Number_prime(int n);
int main()
{
int number;
cout << "Enter Value: ";
cin >> number;
// one parameter is passed to the function Number_prime()
Number_prime(number);
return 0;
}
// return type is void, so it does not return any value
void Number_prime(int n)
{
int i, flag = 0;
for (i = 2; i <= n/2; ++i)
{
if (n%i == 0)
{
flag = 1;
break;
}
}
if (flag == 1)
{
cout << n << " Not a Prime Number";
}
else {
cout << n << " Its Prime Number";
}
}

Functions with parameters and with a return value

In this type, the function is passed with argument and also it returns the value, in this program the user will input the integer value and that value is first stored in the variable and then passed to the function call to check whether the entered value is prime or not. Here the return type of Number_prime() is an integer, it either returns 1 or 0 to the main() function. If the entered value is prime then it returns 1, otherwise, it returns 0. In the main () function call the returned value is stored in the flag based on that it displays the text on the screen.

Program

#include <iostream>
using namespace std;
int Number_prime(int n);
int main()
{
int number, flag = 0;
cout << "Enter positive integer: ";
cin >> number;
// one argument is passed to the function
flag = Number_prime(number);
if(flag == 1)
cout << number << " Not a Prime Number";
else
cout<< number << " its a Prime Number";
return 0;
}
/* it have a return type - integer */
int Number_prime(int n)
{
int i;
for(i = 2; i <= n/2; ++i)
{
if(n % i == 0)
return 1;
}
return 0;
}

Benefits of User-Defined Functions

  • By using a user-defined function it supports code reusability, we can use the code several times and by declaring once.
  • User-defined functions reduce the complexity of huge programs and optimize the code.
  • The user-defined function helps users to build programs easily because every task is divided into several functions.
  • User-defined function enhances readability.

C
C++
UserDefined Function
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance