A C++ Function is a collection of statements that are grouped together to perform an operation. A function in C++ is a block of code that, When the function is invoked from any part of the program, it all executes the codes defined in the body of the function
You can insert values or parameters into Function, and they will only be executed when called. They are also referred to as functions. The primary uses of function in C++ are:
Two Types of Function :
1. Pre-defined Function: These are built-in functions in C++ that are available to use.
2. User-defined Function: We can create our own function based on our requirements.
Pre-defined Function :
Pre-defined Function are also known as library functions. We need not to declare and define these functions as they are already written in the C++ libraries such as iostream, cmath , string etc
User-defined Function :
User-defined Function allows the programmer to define their own function.
Syntax:
Return_Type Function_Name ( Parameters )
{
// body of Statement ;
}
Return Type :
Parameter list :
Statement body :
Four Types of User-defined Function
The program defines a function named display() that adds two numbers entered by the user and displays their sum. The function display() is defined with a return type of void, meaning that it does not return any value. The function does not take any parameters, but it declares three integer variables a, b, and c to store user input and the result of the addition.
Within the display() function, the user is prompted to enter two integer values, which are stored in the variables a and b. The program then adds these two values together and stores the result in the variable c. Finally, the program displays the total by printing the value of c to the console.
In the main() function, the display() function is called twice, so the user is prompted to enter two pairs of numbers, and their sums are displayed to the console. Overall, the program defines a function that adds two numbers and displays their sum, and then calls this function twice within the main() function to allow the user to enter two pairs of numbers and view their sums.
#include<iostream> using namespace std; // Return Type Function Name (Parameters) void display() { int a,b,c; cout<<"\nEnter 2 Nos : "; cin>>a>>b; c=a+b; cout<<"Total : "<<c; } int main() { display(); display(); return 0; }
Enter 2 Nos : 23 56 Total : 79 Enter 2 Nos : 12 23 Total : 35To download raw file Click Here
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions