User Defined Function in C/C++ And How To Use Them In Programming

(Last Updated On: April 6, 2020)

User Defined Function

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.

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:

  1. Function with no argument no return value
  2. Function with no argument with return value
  3. Function with an argument with no return value
  4. 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:

Output:


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:

Output:


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:

Output



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:

Output:

 

Recommended For You

About the Author: Engr Fahad

My name is Shahzada Fahad and I am an Electrical Engineer. I have been doing Job in UAE as a site engineer in an Electrical Construction Company. Currently, I am running my own YouTube channel "Electronic Clinic", and managing this Website. My Hobbies are * Watching Movies * Music * Martial Arts * Photography * Travelling * Make Sketches and so on...

Leave a Reply