In this tutorial,we will discuss about C++ programming function with Examples.
In this article we will discuss what is the “function” and how its work
Already we learnt in C function, Cpp function is similar to C function.
In C++ language function is a useful segment of statements that together perform a significant task. In another word, function is a group of codes designed to completes a specific task.
Two types of functions are available in C++ like C language
The stranded library function is already built-in C++ language for special purposes. It is called the per-defined function.
A lot of per-defined functions are available in C++ language
C++ supports the creation of user-defined functions using their own ideas to achieve their required programming purpose.
User-defined functions contain a block of statements (group of code) which are written by the programmer to perform a task under a given name of function(identifier).
When the function is called from any part of the program, the codes, including the body of the function are executed. Therefore, when the function is executed, a part of the program is invoked.
How user-defined function works in C++ Programming language
When the program starts, it executes from the main() function. The control of the program moves from the function name(functionName) to the inside the main (int main())function. Then, it moves to function declaration(void functionName) and the following function is executed(functionName())
The control of the program moves back to the main function where the code after the call to the function (functionName()) is executed
Elements of user-defined function
This function has three related elements to establish the function
Example program for user define function
#include <iostream> #include <conio.h> using namespace std; int sumNum(int a, int b);//function declaration(prototype) int main() { int num1,num2,sum; cout << "program sum 2 number using function in C++" << endl; cout << "please enter first number" << endl; cin>>num1; //read number cout << "please enter second number" << endl; cin>>num2; //read number sum=sumNum(num1,num2); cout<<"The total value is"<<sum<< endl; getch(); return 0; } //function definition int sumNum(int a, int b){ int add=a+b; return add; }
When the above code is compiled and executed, it produces the following results
Program sum 2 numbers using function in C++ Please enter first number 45 Please enter second number 76 The total value is :121
Function prototype or declaration helps to announce the compiler about the existence of function with their frame.
Its name, parameter and return type are mentioned.
Here, initial function is declared without body.
Generally, the C++ function defined is as follows
return_type function_name(list of parameter);
Example
int sumNum(int a, int b);
To execute inside the codes of function body, The user-defined function must be called from inside the main method
function_Name(parameter_list);
Example
sum=sumNum(num1,num2);
If the function is without argument,Typically
function is called by their names. But function is with argument we can use two ways to call them.
Function definition describes itself, referred to the function definition when the codes are executed
Return_type function_name(list of parameter){
//function body
}
Example
//function definition
int sumNum(int a, int b){ int add=a+b; return add; }
Passing argument to function
In C++ programming language, argument(list of parameters ) is used to pass data to a function when calling it.
In the above example sumNum() is a function having two variables int a, int b and passed to the function during function call
Return statements in function
A function can return a single value when calling the program using return statements.
In the following program, when we call the add method as follows add() the value of add is returned from the function using the statement given below
return add;
In the above program, the program describes how to return a value from user defined function using return statements.
Similar post
C function
C# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…
Program to count vowels, consonants, words, characters and space in Java In this article, we…
How to print multiplication table using Array in C++ language In this post, we will…
C Program to multiplication table using Array In this tutorial , we will discuss about…
Java program to check odd or even using recursion In this tutorial, we discuss a…