FUNCTION
Function:
Function is a named block of codes that perform some action.
The statements written in function are executed when it is called by its
name.
Each function has a unique name.
Functions provide a structured programming approach.
Benefits of Functions:
Easier to Code
Easier to Modify
Easier to Maintain and Debug
Reusability
Less Programming Time
Enhance Readability
1) Pre-defined or Build-in
Function
The functions that are the part of programming language are
called pre-defined functions.
Whenever it is needed, The user can modify the function.
Example
s:
Function Purpose
std::cout Print messages to the screen
std::cin Get input from the user
std::sqrt(x) Find the square root of x
std::abs(x) Get the absolute value of x
std::pow(base, exp) Raise base to the power of exp
Function to calculate the power of
base with an exponent
#include <iostream>
using namespace std;
#include <cmath> // Include the cmath library for math functions
int main()
{
int base = 2;
int exponent = 3;
int result = pow(base, exponent); // Calculate 2 raised to the power of 3
cout << base << " raised to the power of " << exponent << " is " <<
result << endl;
return 0;
}
2) User-defined
Function
The functions that are defined by users according to their
needs are called user-defined functions.
Whenever it is needed, The user can modify the function.
Example:
sum(a,b)
maximum(num1,num2)
Function that find maximum of two
numbers
#include <iostream>
using namespace std;
// User-defined function to find the maximum of two numbers
int findMaximum(int a, int b)
{
return (a > b) ? a : b; // Return the larger number
}
int main()
{
int num1, num2;
cout << "Enter the first number: ";
cin >> num1;
cout << "Enter the second number: ";
cin >> num2;
int maxNum = findMaximum(num1, num2); // Call the user-defined function
cout << "The maximum number is: " << maxNum << endl;
}
Function
Declaration:
The purpose of function declaration is to tell the compiler about
the function name, return type, and parameters.
Also called function prototype OR function signature.
It ends with semicolon.
It is usually written just before the main function.
Syntax:
returnDatatype functionName(parameters);
Function
Definition:
Set of statements that explain what a function does is called function
definition.
It is written before main() function OR after main() function.
Function declaration is not required if function definition is written before the
main function.
Function declaration is compulsory if function definition is written after the
main function.
Syntax:
returnDatatype functionName(parameters)
{
Body of function
}
Function
Calling:
The statements that activates a function is known as function
calling.
A function is called by its name.
If there are an any parameters, these are written in
parentheses and separated by commas.
If no parameters, then empty parentheses are used.
A simple function with no
parameters
#include<iostream>
using namespace std;
int show() // function definition
{
cout<<"It is a function"; // function
definition
}
int main()
{
show(); // function calling
}
Parameters:
Parameters are the values provided to a function when a
function is called.
Input
A
Function
Output
Syntax:
Function-name (pm);
Syntax for Multiple
parameters:
Parameters are separated by comma.
Function-name (pm1,pm2,pm3);
If the function is very simple then the syntax
willFunction-name
be: ();
Ways of providing
parameters
There are two ways of providing parameters:
Constant Function-name(10,20);
values: Int a=10;
Variables as Int b=20;
parameters: Function-name(a,b);
Important Note:
Sequence and types of
parameters in function call must
be similar to the sequence and
types of parameters in function
Example: declaration.
void calc(int a, float b);
Calc(10,15.5);
Int a=10;
Float b=15.5;
Calc(a,b);
Types of
parameters
There are two types of function
parameters:
1. Formal parameters
2. Actual parameters
Formal
parameters:
Parameters used in the header of function definition are
called formal parameters.
These parameters are used to receive values from the
function calling.
void calc(int x, int y);
Actual
parameters:
Parameters used in the header of function calling
are known as actual parameters.
calc (10,20);
Calc (a,b);
Passing parameters to
functions
Providing parameters to a function is
called” Passing parameters to
function”.
There are two types of parameters
passing:
1. Parameters pass by Value
2. Parameters pass by Reference
Pass by value:
In call by value the actual value of the
function cannot be changed, if you
change the value of the function
parameters it will only change it for
current function.
Different memory locations for actual and formal
parameters.
Syntax:
Return-type function-name(p1,p2);
{
// block of code
}
Call by reference:
A parameter passing mechanism in which
address of actual parameter is passed.
The ampersand & sign is used with formal
parameters to passs parameters by reference.
There is only one memory location for both
formal and actual parameters.
x a