What is a Function in C++?
Think of an ATM. You enter your pin code and the amount you want to withdraw. The output is the cash and the receipt. A lot of things happen inside the ATM as well, but we do not know about them.
Same is a Function in C++ or any other programming language. A Function takes (not always) some values as inputs, uses them for processing and then provides the result.
Connecting the analogies, the pin code and amount are the inputs. Bank deducts that amount from your bank account(processing) and then the output is the cash and receipt in your hand.
Similarly, we can say that facebook.com is also a function. You visit facebook.com, input your email id and password, some processing happens at the servers and the output you get is access to your own personal account.
Most Functions in C++ take inputs and provide outputs but that is not mandatory. A Function may or may not take inputs. Which means that there could be functions that only perform some task without taking inputs every time they are executed.
Take an example of a table lamp. It does not accept any input when you switch it on(a fan does take input in form of fan speed controlled by the regulator) but provides you with output in form of light with help of some of its internal processes.
What are different attributes of a Function in C++?
1. Function Return Type
It is the data type of the value that the function will return as a result of its execution. If you develop a function that adds two integer numbers and returns their sum, the return type for the function would be ‘int’.
In many cases, a function will not return any value. In such cases, the return type is ‘void’.
A return type of a function comes into play at the time of calling the function, where the result returned by the function is to be stored into a variable. The data types of the - (a) return type of the function and (b) the variable in which the returned value is to be stored - have to be the same.
Example:
int AddTwoNumbers(int a, int b) // Function declaration & definition
{
int c; // Processing, step 1
c=a+b; // Processing, step 2
return c; // Returning the result
}
2. Function Name
It is the name given to a particular function. Names of two or more functions can be same in a program (Function Overloading and Function Overriding), but we suggest you, as a
3. Function Arguments/Parameters
These are the values that are passed into the function when it is used. These values are the inputs to the processing that takes place when the function is called.
If you develop a function to add two numbers then the two numbers will have (not mandatory, alternatives exist) to be provided to the function in form of arguments.
Example:
int AddTwoNumbers(int a, int b) // Function declaration & definition
{
int c; // Processing, step 1
c=a+b; // Processing, step 2
return c; // Returning the result
}
4. Function Body
This part is the definition of the function and includes all the processing that is to be done by the function and the return (if any) of the result.
Example:
int AddTwoNumbers(int a, int b) // Function declaration & definition
{
int c; // Processing, step 1
c=a+b; // Processing, step 2
return c; // Returning the result
}
- function is c++
- function is inaccessible c++
- function is prime c++
- function is protected c++
- virtual function is c++
- function c++ definition
- function c++11
- function c++ pdf
- function c++ return array
- function c++ ppt
- function c++ array
- function c++ average
- function c++ arguments
- function alias c++
- function address c++
- function abs c++
- function atoi c++
- function append c++
- function pointer c++ as argument
- function access c++
- function c++ by reference
- function c++ boost
- function body c++
- function bool c++
- function bind c++
- function beep c++
- function borland c++
- function parameter c++ by reference
- function block c++
- function break c++
- function c++ const
- function c++ char
- function c++ class
- __function__ c++ class name
- function c++ cout
- function c++ const parameter
- function callback c++
- function pointer c++ class
- function pointer c++ class method
- function pointer c++ class member
- function is deprecated c++
- function c++ declaration
- function c++ default argument
- function c++ default value
- function c++ double
- function c++ default parameter
- function find c++
- function fibonacci c++
- function floor c++
- function float c++
- function free c++
- function fabs c++
- function in c++ for string
- __function__ c++ gcc
- function getline c++
- function get c++
- function gcd c++
- function gotoxy c++
- function goto c++
- function gamma c++
- function getch c++
- function generator c++
- function global c++
- function c++ header
- function c++ .h
- function hiding c++
- function handle c++
- function hooking c++
- function header c++ definition
- function hypotenuse c++
- function handler c++
- function hash c++
- function declaration c++ header file
- function c++ identifier not found
- function c++ inline
- function in c++
- function in c++ pdf
- function in c++ ppt
- function in c++ definition
- function inheritance c++
- function in c++ exercises
- function if c++
- function keyword c++
- function keys c++
- function kill c++
- function kbhit c++
- function c++ lecture notes
- __function__ c++ linux
- function in c++ language
- function library c++
- function length c++
- function list c++
- function lambda c++
- function loop c++
- function log c++
- function sleep c++ linux
- function c++ meaning
- function c++ max
- function c++ macro
- function c++ multiple output
- function min c++
- function main c++
- function map c++
- function member c++
- function minimization c++
- function matrix c++
- function c++ notes
- function name c++
- function new c++
- function name c++ macro
- function namespace c++
- function pointer c++ null
- function notation c++
- function n c++
- function nesting c++
- c++ function is not marked const
- function c++ overloading
- function object c++
- function overriding c++
- function of c++
- function of c++ pdf
- function overloading c++ area program
- function overloading c++ ppt
- function overloading c++ different return types
- is_function_pointer c++
- function c++ programming
- function c++ pass by reference
- function c++ prototype
- function c++ pointer argument
- function c++ parameter
- function c++ questions
- function c++ quiz
- function qualifier c++
- function queue c++
- function pointer c++ qt
- function c++ return
- function c++ return 2 values
- function c++ reference
- function c++ return string
- function c++ return vector
- function c++ return pointer
- function c++ return reference
- function c++ return char
- function c++ reverse string
- function c++ std
- function c++ static
- function c++ string parameter
- function c++ size of array
- function signature c++
- function sort c++
- function specialization c++
- function scope c++
- function c++ tutorial
- function c++ template
- function c++ template argument
- function pointer c++ typedef
- function recursion c++ tutorial
- function time c++
- function throw c++
- function table c++
- function traits c++
- function object c++ tutorial
- function is undefined c++
- __function__ c++ unicode
- operator function is unavailable c++
- function using c++
- function unique c++
- function undeclared c++
- function uppercase c++
- function update c++
- function unlink c++
- function usleep c++
- function c++ void
- function c++ visual studio
- function c++ vector
- function c++ variable arguments
- function c++ variable pointer
- function variable c++
- function visual c++
- function virtual c++
- function in c++ video
- function pointer c++ vector
- function c++ wikipedia
- function c++ with examples
- function wrapper c++
- function while c++
- function overloading c++ with example
- function wait c++
- function pointer c++ with arguments
- function overloading c++ wiki
- function x c++
- function c++ youtube
- function overloading c++ youtube
- function 0 c++
- function c++ 11
- function template c++ 11
- function 1 c++
- function object c++ 11
- function prototype c++ 2d array
Join the conversation