ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

Using User Defined Functions in C/C++ programming Language:

Updated on November 6, 2014

Built-in Functions in C/C++:

The built-in functions are those which are already defined and ready for use. For example there is a function getche(). Which is commonly being used in C/C++ programming by the programmers. We don’t know which type of programming is done in the getche() function but we know its use and we use it in programming. Like this there are many others built-in functions which we are using them in programs to solve the problems. i.e.

asin(), atan(), atexit(), atof(), atoi(), ceil(), clock(), ctime(), div(), exit(), fabs(), floor(), getchar(), getenv() etc

User-defined Functions in C/C++:

The user-defined functions are not specified by names, because it depend on the programmer what he/she want to give it a name. They use names according to their program in which they declared and defined the function. After that they are using their own defined functions.

There are steps of user-defined function use in C/C++. As first of all commonly we follow the following steps:

Declaration of Function: In declaration we just declare the type of function with its name.

Definition of Function: In definition we write a program in the function braces/blocks.

Calling of Function: In calling we just call the function but there are two ways to call a function, as we call a function by its name in 1nd way we send some arguments and in 2nd way we don’t used to send arguments to the function.

Type of User-defined Functions in C/C++ Programming:

  1. Void FunctionName(void)
  2. Argument FunctionName(void)
  3. Void FunctionName(Arguments)
  4. Argument FunctionName(Arguments)

We will follow these four types in this article to create our own functions and use them in the programming. As you can see the first type of user-defined function. i.e.

void FunctionName(void), its mean that this type of function will not send any data and also not return any data to its calling place in the program. In C/C++ programming the keyword void means that do nothing.

argument FunctionName(void), its mean that this type of function will not send any data but it will return some type of data when it will call. And it will return data to its calling place from where it is being called in the program. In C/C++ programming arguments mean that bunch of data, which we will use it in this article to create some programs.

void FunctionName(arguments), its mean that this type of function will send data as arguments but it will not return any kind of data when it will call. As I used word arguments the plural of argument, its mean that we can send multiple type of different and same type arguments to a function.

Argument FunctionName(arguments), its mean that type of function will send and also return bunch of data to its calling place in the program. From where it’s being used to call for do something.

Listing 1: Declaration of User-Defined Functions:

int SumTwoNumbers(void);
void MultiplyTwoNumbers(int,int);
float DivideTwoNumbers(int,int);
void CallAboveThreeFunctions(void);

Description of Declared User-Defined Functions:

I followed the four types of user-defined functions, and I declared four functions. As you can see in the Listing 1 I named the user-defined functions according to my program. First will add two numbers and return the value. 2nd will multiply the tow values but will not return the result as you can see it will get to arguments which are same data type. We can send different data types at the same time to the function.

Listing 2: Definition of User-Defined Functions:

int SumTwoNumbers(void)
{
	int sum = 0;
	sum = a+b;
	return (sum);
}
void MultiplyTwoNumbers(int x, int y)
{
	int multiply = 0;
	multiply = x*y;
	cout << "Multiplication Of Your Two Entered Numbers is:" << multiply << endl;
}
float DivideTwoNumbers(int x,int y)
{
	float divide = 0.0;
	divide = x/y;
	return(divide);
}
void CallAboveThreeFunctions(void)
{
	int sum = SumTwoNumbers();
	float divide = DivideTwoNumbers(a,b);
	cout << "Addition Of Your Two Entered Numbers is: " << sum << endl;
	cout << "Division Of Your Two Entered Numbers is:" << divide << endl;
	MultiplyTwoNumbers(a,b);
}

Description of defined User-Defined Functions:

As you can see in the listing 2, I defined the functions according to the declaration of the functions. And they are following the types by which they declared. Now these all four functions are fully ready to use I mean that now we can call them from the main function. And we can call a user-defined function by a user-defined function as you can see in the fourth one. That is CallAboveThreeFunctions, so its mean that it is possible to call a function by a function as a function also can call itself.

Listing 3: Calling User-defined Functions

int main()
{
	cout << "\nWelcome to itecbaloshi.com...";
	cout << "\nPlease Enter Any Two Numbers to Perform Operations";
	cout << "\nAddition\nMultiplication\nDivide";
	cout << "Enter First Number: ";
	cin >> a;
	cout << "Enter Second Number: ";
	cin >> b;
	CallAboveThreeFunctions();
}

Description of Calling user-defined functions

As you can see in the listing 3, we called the all three functions by just calling one function in the main function. In main function you can see there are two variables a and b but they are not declared. I declared them as global variables. Which you can see them in the Listing 4.

Listing 4: FourTypesOfFunctions.cpp

#include <iostream.h>
using namespace std;
int SumTwoNumbers(void);
void MultiplyTwoNumbers(int,int);
float DivideTwoNumbers(int,int);
void CallAboveThreeFunctions(void);
int a,b;
int main()
{
	cout << "\n\tWelcome to itecbaloshi.com...\n";
	cout << "\n\tPlease Enter Any Two Numbers to Perform Operations";
	cout << "\n\tAddition\n\tMultiplication\n\tDivide";
	cout << "\nEnter First Number: ";
	cin >> a;
	cout << "\nEnter Second Number: ";
	cin >> b;
	CallAboveThreeFunctions();
}
int SumTwoNumbers(void)
{
	int sum = 0;
	sum = a+b;
	return (sum);
}
void MultiplyTwoNumbers(int x, int y)
{
	int multiply = 0;
	multiply = x*y;
	cout << "Multiplication Of Your Two Entered Numbers is:" << multiply << endl;
}
float DivideTwoNumbers(int x,int y)
{
	float divide = 0.0;
	divide = x/y;
	return(divide);
}
void CallAboveThreeFunctions(void)
{
	int sum = SumTwoNumbers();
	float divide = DivideTwoNumbers(a,b);
	cout << "Addition Of Your Two Entered Numbers is: " << sum << endl;
	cout << "Division Of Your Two Entered Numbers is:" << divide << endl;
	MultiplyTwoNumbers(a,b);
} 

Description of the C/C++ program FourTypesOfFunction.cpp

As we can see in the listing 4, there is the full program source code. In which we used all four types of functions. And they are being execute without any error. You can see its output in the Figure 1.

Conclusion:

In this article we learnt about the types of Built-in functions and user-defined functions. And the declaration, definition and also calling of the user-defined functions. I hope you will learn something from this article. Kindly comment if you have any kind of query about the User-defined and built-in functions.

Comments

    0 of 8192 characters used
    Post Comment

    • profile image

      muhammad umer yasin 2 years ago

      nice

    • ronny2005 profile image

      ronny2005 21 months ago from HubPages

      Very detailed explanation! Good effort.

    Click to Rate This Article