Function
Subroutine
Function
A function is a group of statements that together perform a
task. Every C++ program has at least one function, which
is main(), and all the most trivial programs can define
additional functions.
Function
A function is a named block of code that perform some
a c t i o n . The statement written in function are executed
when it is called by name. Each action has unique name .
Function are the building block of c++ programs. They are
used to perform the tasks that are
r e p e a t e d many times.
Function
A function is a named block of code that perform some a c t i o n .
The statement written in function are executed when it is called
by name. Each action has u n i q u e n a me .
Function are the building block of c++ programs. They are
used to perform the tasks that are repeated many times.
The idea is to put some commonly or repeatedly done task
together and make a function so that instead of writing the same
code again and again for different inputs, we can call the function.
Importance of Function
• A program may need to repeat the same piece of code at
various places.
• It may be required to perform certain task repeatedly.
• The program may become very large if functions are not
used.
• The real reason for using function is to divide program into
different parts.
C++ Function
• C++ allows the use of both internal (user-defined) and
external ( B u i l t in ) functions.
• E x t e r n a l f u n c t i o n s are usually grouped into
specialized libraries (e.g., iostream, stdlib, math, etc.)
C++ Function
A function declaration tells the compiler about a function’s name,
return type, and parameters. A function definition provides the actual
body of the function.
The C++ standard library provides numerous built-in functions that
your program can call. For example, function strcat() to concatenate
two strings, function memcpy() to copy one memory location to
another location and many more functions.
A function is known with various names like a method or a sub-
routine or a procedure etc.
Defining a Function
• The general form of a C++ function definition is:
return_type function_name (parameter_list)
{
body of the function;
}
Defining a Function
A C++ function definition consists of a function header and a
function body. Here are all the parts of a function:
• Return Type- a function may return a value.
The return_type is the data type of the value the function
returns. Some functions perform the desired operations
without returning a value. In this case, the return_type is the
keyword void.
Defining a Function
• Function Name − This is the actual name of the function. The
function name and the parameter list together constitute the
function signature.
• Parameters − A parameter is like a placeholder. When a
function is invoked, you pass a value to the parameter. This
value is referred to as actual parameter or argument. The
parameter list refers to the type, order, and number of the
parameters of a function. Parameters are optional; that is, a
function may contain no parameters.
Defining a Function
• Function Body − The function body contains a collection of
statements that define what the function does.
Calling a function
We can call the function like this:
function_name(parameters);
Example # 1
#include <iostream>
using namespace std;
int max(int x, int y)
{
if (x > y)
return x;
else
return y;
}
int main() {
int a = 10, b = 20;
// Calling above function to find max of 'a' and 'b'
int m = max(a, b);
cout << "m is " << m;
return 0;
}
Output
m is 20
Example # 2
#include <iostream>
using namespace std;
/* This function adds two integer values * and returns the result */
int sum(int num1, int num2)
{
int num3 = num1+num2;
return num3;
}
int main()
{
//Calling the function
cout<<sum(1,99);
return 0;
}
Output
100
Types of function
1) Built-in functions
2) User-defined functions
Types of function
1) Built-in functions
Built-in functions are also known as library functions. No
need to declare and define these functions as they are
already written in the C++ libraries such as iostream, cmath
etc. You can directly call them when needed.
Example of Built-in function
This example uses a built-in function pow(x,y) which is x to the
power y. This function is declared in cmath header file so the file
is included in using #include directive.
Example of Built-in function
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
/* Calling the built-in function pow(x, y) which is x to the power y */
cout<<pow(2,5);
return 0;
}
Output
32
Example of Built-in function
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double num, squareRoot;
cout << "Enter number: ";
cin >> num;
squareRoot = sqrt(num);
cout << "The square root of " << num << " is: " << squareRoot;
return 0;
}
Output
Enter number: 49
The square root of 49 is: 7
Types of function
2) User-defined functions
Example of User-defined function
#include <iostream>
using namespace std;
void sayHello()
{
cout << "Hello!";
}
int main()
{
sayHello();
return 0;
}
Output
Hello!
Example of User-defined function
#include <iostream>
#include <cmath>
using namespace std;
int sum(int a, int b)
{
int c = a+b;
return 0;
}
Example of User-defined function
int main()
{
int x, y;
cout<<"enter first number: ";
cin>> x;
cout<<"enter second number: ";
cin>>y;
cout<<"Sum of these two :“ <<sum(x,y);
return 0;
}
Output
enter first number: 22
enter second number: 19
Sum of these two : 41