User Defined Function in C/C++ And How To Use Them In Programming
What is a function:
User-Defined Function-A function is a subprogram or module to which any amount of data can be sent but which returns only one value. A function is used to perform some logically isolated tasks. it makes it easier to write programs and keep track of what they are doing. It avoids rewriting the same series of instructions over and over. These instructions are written once but are used at several places in the main program as and when desired.
Amazon Purchase Links:
*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!
Declaring functions:
Before defining a function you must first declare a function. A function cannot be called until & unless being declared this declaration of a function is called its prototype which consists of functions return type function name and parameter list and ends with a semicolon. You can define a function before using it. In this case, there is no need of function prototype. But, it is not considered a good programming practice for many reasons, therefore always avoid it.
C++ Function Prototype
The function prototype is a statement, which must end with a semicolon. As discussed earlier, function prototype consists of function’s return type, function name and the number and type of its parameters. It has the following general format:
return_type function_name ( type parameter, type parameter, ……..);
For example; in the above example, function prototype being used is:
void Funct();
in which, the function’s return type is void. If a function does not return a value, its return type will be void. i.e. void type specifies the empty set. Similarly, a function that has no parameter can use the reserved word void in its parameter list, like,
void Funct(void);
Any valid identifier’s name can be used as a function’s name i.e. it must follow the naming rules for identifiers. Always choose that names which import some meaning. For example:
void Message();
int Cube{ int x);
Each function name follows the parameter list, enclosed in parentheses. Parameter list consists of parameters and their types, separated by commas. Many functions are not required to have parameters (as in the above example), optional void may be used here as:
void Funct(void);
Consider another example:
float Area(int length, int width);
Parameter names are optional in parameter list. It would be sufficient to mention their types only; like:
float Area(int, int);
However, it is recommended to add parameter names in parameter list for prototype
Defining The C++ Functions
Like main(), function definition is a complete function, consisting of function header and function body, which may appear anywhere outside the main() function. The function header must be exactly the same as the function prototype, except no semicolon is used at the end of the definition and second, it is necessary to mention the parameter names in parameter list (if any). The function body consists of a statement or group of statements, enclosed within a block of braces. There is no semicolon after the closing brace, but all statements within the body of the function must be terminated with semicolon. The return statement is used at the end of the function to return a value. The return statement is not required for every function, but it is strongly recommended to always include it in your function subprogram. The general format of function definition
is:
return_type function_name ( type parameter, type parameter, ……..)
{
body of the function;
}
Type of user-defined function:
There are four types of user-defined function which are:
- Function with no argument no return value
- Function with no argument with return value
- Function with an argument with no return value
- Function with an argument with return value
User-defined Function with no argument no return value:
In this type of function the calling function does not pass the argument when its calls the calling function also the called function does not returns value to the calling function. There is no data transfer between calling and called function. Here each function are independent. They need the data, data values, calculate the result and display the same block.
Working principle of user-defined function with no argument no return value:
Calling function :
main()
{
sum();
………………..
……………….
}
called function:
sum()
{
…………………
………………..
}
Example write a user-defined function program to calculate factorial of a number using no argument no return value:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
#include <iostream> void fact(); using namespace std; int main() { fact(); return 0; } void fact() { int n , fact=1; cout<<"Enter a number :"; cin>>n; while(n>0) { fact = fact *n; n--; } cout<<"factorial of a number is "<<fact; } |
Output:
1 2 3 |
Enter a number :5 factorial of a number is 120 |
User-defined Function with no argument with 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 variables required for the called function that are declared and initialize the same called function module. The called function is independent.
Working principle of user defined Function with no argument with return value:
Calling function:
void main()
{
int x;
x=sum();
}
Called function
sum()
{
int p=4, q=3, s;
s=p+q;
return(s);
}
The output is printed at the calling function side. The return statement is required for returning a value from called side to calling side. The data type returning value and the function return type must be same.
Example write a user-defined function program to check the prime number using no argument with return value:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
#include <iostream> using namespace std; int prime_number(); int main() { int num, i, flag = 0; // No argument is passed to prime() num = prime_number(); for (i = 2; i <= num/2; ++i) { if (num%i == 0) { flag = 1; break; } } if (flag == 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_number() { int n; printf("Enter the number to check it is prime or not : "); cin >> n; return n; } |
Output:
1 2 |
Enter the number to check it is prime or not : 4 4 is not a prime number. |
User-defined Function with argument with no return value:
This type of function argument are passed through the calling function to called function but the called function does not return value. such type of function practically depended with each other.
Working principle of user defined Function with argument with no return value:
Calling function:
Void main()
{
Int a, b;
Sum(a,b)
}
Called function:
Void sum(int x, int y)
{
Int add;
Add=x+y;
Cout<<”the sum is :0”<<add;
}
Example write a user defined function program to check prime number using argument with no return value:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
#include <stdio.h> void Display(int n); int main() { int n; printf("Enter a positive integer: "); scanf("%d",&n); // n is passed to the function Display(n); return 0; } // return type is void meaning doesn't return any value void Display(int n) { int i, flag = 0; for(i=2; i <= n/2; ++i) { if(n%i == 0){ flag = 1; break; } } if(flag == 1) printf("%d is not a prime number.",n); else printf("%d is a prime number.", n); } |
Output
1 2 3 |
Enter a positive integer: 4 4 is not a prime number. |
User-Defined Function with argument with return value:
In this type of function, the calling function passes the argument to the called function and called function to send back value to the calling function. Both functions are depended on each other.
Working principle of user defined Function with argument with return value:
Calling function:
void main()
{
int a=5, b=6, y;
y=sum(a,b);
cout<<y;
}
called function:
sum(int a, int b)
{
int add;
add=a+b;
return(add);
}
Example write a user defined function program to check the prime number using argument with return value:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
#include <iostream> using namespace std; int prime_number(int n); int main() { int num, flag = 0; cout << "Enter the number to check it is prime or not: "; cin >> num; // Argument num is passed to check() function flag = prime_number(num); if(flag == 1) cout << num << " is not a prime number."; else cout<< num << " is a prime number."; return 0; } /* This function returns integer value. */ int prime_number(int n) { int i; for(i = 2; i <= n/2; ++i) { if(n % i == 0) return 1; } return 0; } |
Output:
1 2 |
Enter the number to check it is prime or not: 3 3 is a prime number. |