• A function is a group of statements that together perform a task.
Every C++ program has at least one function, which is main ().
• A function takes input, does some specific computation, and
produces output.
• The idea is to put some commonly or repeatedly done tasks
together to make a function so that instead of writing the same
code again and again for different inputs, we can call this function.
• In simple terms, a function is a block of code that runs only when
it is called.
• There are two types of function:
Standard Library Functions: Predefined functions in C++
User-defined Function: Created by users
Library Function
Library functions are also called “built-in Functions“. These functions
are part of a compiler package that is already defined and consists of a
special function with special and different meanings. Built-in Function
gives us an edge as we can directly use them without defining them
whereas in the user-defined function we have to declare and define a
function before using them.
For Example: sqrt(), setw(), strcat(), etc.
C++ User-defined Function
C++ allows the programmer to define their own function.
A user-defined function groups code to perform a specific task and that
group of code is given a name (identifier).
When the function is invoked from any part of the program, it all
executes the codes defined in the body of the function.
,Syntax of Function
return_type function_name (parameter_list)
{
//C++ Statements
}
A simple function example
#include <iostream>
int sum(int num1, int num2)
{
int num3 = num1+num2; return num3;
return num3;
}
int main()
{
cout<<sum(1,99);
return 0;
}
Output:
100
The same program can be written like this:
#include <iostream>
int sum(int,int); // function declaration
int main()
{
cout<<sum(1,99); //function call
return 0;
}
,int sum(int num1, int num2)
{
int num3 = num1+num2; // function definition
return num3;
}
it we will see there are three parts for functions such as function
declaration, function definition and calling of function.
Function Declaration: when we define a function before the main()
function in our program then we don’t need to do function
declaration but if we are writing our function after the main()
function then we need to declare the function first, else we will get
compilation error. Function declaration is also known as function
prototype.
syntax of function declaration:
return_type function_name(parameter_list);
Note: While providing parameter_list we can avoid the parameter
names. That is, int sum(int,int); instead of int sum(int num1,int
num2);.
Function definition: Writing the full body of function is known as
defining a function.
syntax of function definition:
return_type function_name(parameter_list)
{
//Statements inside function
}
, Calling function: We can call the function like this:
function_name(parameters);
Parameters and Arguments
Information can be passed to functions as a parameter.
Parameters act as variables inside the function.
Parameters are specified after the function name, inside the
parentheses.
we can add as many parameters as you want, just separate them
with a comma:
Syntax
void functionName(parameter1, parameter2, parameter3)
{
// code to be executed
}
The following example has a function that takes
a string called fname as parameter. When the function is called, we
pass along a first name, which is used inside the function to print the
full name:
Example
void myFunction(string fname)
{
cout << fname << " Refsnes\n";
}
int main() {
myFunction("Liam");
myFunction("Jenny");
myFunction("Anja");
return 0;