Introduction to User-Defined Function and its Requirements

Defining a Function

A function refers to a group of statements that perform a task together. Furthermore, while defining a function, a function header and a function body must be included. Moreover,  every C++ program consists of at least one function, main(), and the additional functions are defined by all the most trivial programs.

defining a function

Parts of a Function

There are various types of functions in Python and C++. Below are the four important parts of a function in the C++ programming language:

  • Return Type − Return type means that a value may be returned by a function. Furthermore, the return_type refers to the data type of the function returns value. Moreover, the performance of some functions can take place on the desired operations without returning a value. In this case, the return_type would be the keyword void.
  • Function Name − This is the function’s actual name. Furthermore, the parameter list and the function name together result in the formation of the function signature.
  • Parameter − A parameter is similar to a placeholder. Furthermore, when the invoking of a function takes place, one would pass a value to the parameter. This value is called by the experts as argument or the actual parameter.
  • Function Body − The function body consists of a collection of statements. Most noteworthy, these statements define the function’s working. It defines what a function does. As such, it is important for defining a function.

Browse more Topics under User-Defined Function and its Requirements

Function Declaration

A function declaration tells the compiler regarding a function name as well as about the way to call the function. Moreover, the defining of the actual body of the function can take place separately. This is important for defining a function.

A function declaration in C and C++ has the following parts −

return_type function_name( parameter list );

For the defined function max() which is above, below is the function declaration −

int max(int num1, int num2);

Parameter names do not play an important role in the function declaration. Moreover, there is only a need for their type. Consequently, the following is also a valid declaration −

int max(int, int);

A function declaration is needed when the process of defining a function takes place in one source file. Moreover, it is possible to call that function in another file. In such a situation, one must declare the function at the top of the file calling the function.

Calling a Function

While creating a C++ function, one must give a definition regarding what the function has to do. Furthermore, in order to use a function, one would have to invoke or call that function.

When a program calls a function, transferring of the program control takes place to the called function. Furthermore, a called function undertakes the performance of the defined task. Moreover, when its function-ending closing brace is reached or when it’s return statement is executed, the program control is returned back to the main program by it.

To call a function, one would have to pass the required parameters along with the function name. Also, in case the function returns a value, then one can store the returned value. For example −

                    

#include 

using namespace std;



// function declaration

int max(int num1, int num2);



int main () {

// local variable declaration:

int a = 100;

Moreover, int b = 200;

int ret;



// calling a function to get max value.

ret = max(a, b);

cout << "Max value is : " << ret << endl;



return 0;

}



// function returning the max between two numbers

int max(int num1, int num2) {

// local variable declaration

int result;



if (num1 > num2)

result = num1;

else

result = num2;



return result;

}

Here the max() function is kept along with the main() function and the source code is compiled.

Defining Values For Parameters

When defining a function takes place, it is possible to specify a default value for all the last parameters. Furthermore, the use of this value will take place if the corresponding argument is left blank at the time of calling to the function.

The way this is done is by using the assignment operator and assigning values for the arguments that exist in the function definition. Furthermore, in case the passing of a value for that parameter does not take place when the function is called, use of the default given value would take place. However, if a value is specified, the passed value is used while the default value is ignored.

Consider the following example

                    

#include 

using namespace std;



int sum(int a, int b = 20) {

int result;

result = a + b;



return (result);

}

int main () {

// local variable declaration:

int a = 100;

Moreover, int b = 200;

int result;



// calling a function to add the values.

result = sum(a, b);

cout << "Total value is :" << result << endl;



// calling a function again as follows.

result = sum(a);

cout << "Total value is :" << result << endl;



return 0;

}

When the above code is compiled and executed, it produces the following result −

                    

Total value is :300

Total value is :120

FAQs For Defining a Function

Question 1: What is the importance of functions?

Answer 1: The importance of functions can be described as follows:

  • Functions can cause a reduction of code redundancy. Furthermore, if the performance of functionality takes place at multiple places in software, then one can create a function and call it everywhere. Most noteworthy, this also helps in maintenance as the change would take place at one place in case the future changes take place to the functionality.
  • Functions facilitate the making of code modular.  Also, if the division of the code takes place into functions, the task of reading and using the code would become very simple.
  • Functions would also provide abstraction. For example, one can use library functions without having to worry about the internal working.

Question 2: What is meant by a member function in C++?

Answer 2: A member function of a class refers to a function that has its prototype or definition within the class definition like any other variable. Furthermore, its operation takes place on any object of the class of which it is a member. Moreover,  it has access to all the class’s members for that object.

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

Your email address will not be published. Required fields are marked *

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.