Chapter one Function
Chapter one Function
Every C++ program has at least one function, main(), where program
execution begins
The declaration tells the compiler the name, return type, and parameters of
the function
Many of the built-in functions have their function prototypes already written in
the header files you include in your program by using #include
Syntax:
return_type function_name(parameter_type1 parameter_name1,
parameter_type2 parameter_name2, ...);
Parameter names are optional in the prototype (only types are required)
The function prototype and the function definition must agree exactly about the
The header is exactly like the function prototype, except that the parameters
If function definition is done before the main function, then there is no need to put the
prototype
Return Type: the data type of the value the function returns
Both void functions and functions that return a value can have return statements
In the case of a function that returns a value, the return statement specifies the
value returned
In the case of a void function, the return statement simply ends the function
execution and exit it immediately
Every function that returns a value must end by executing a return statement
• The function name and the parameter list together constitute the function signature
The parameter list refers to the type, order, and number of the parameters of a function
Function Body: The function body contains a collection of statements that define what the
function does
Cont…
The following function named calculateMax() takes two parameters num1 and num2 and
returns the maximum
int calculateMax (int num1, int num2) int calculateMax (int num1, int num2)
{ {
int result; or int result=max(num1,num2);//built-in
if (num1 > num2)
return result;
result = num1;
else }
result = num2;
return result;
}
C++ does not allow nested functions
• The definition of one function cannot be included in the body of another function
Function Call
For a function to perform the specified task and return output, it must be
called/invoked
To call a function, pass the required parameters along with function name, and if
function returns a value, then you can store returned value
• If the function takes no parameters, don’t pass any value during the call
When the function returns, execution resumes on the next line of the
calling function
Functions can also call other functions and can even call themselves
Cont…
#include <iostream>
using namespace std;
double calculateArea (double l, double w); // function declaration
int main ()
{
double length= 5; //local variable declaration
double width= 10;
double result= calculateArea (length, width); // calling a function
cout << "Area of a rectangle is : " <<result<< endl; //50
}
double calculateArea (double l, double w) //function header
{
return l*w;
}
Scope of variables
Refers to where in the program a variable is accessible
Two kind:
• Local variable - variables declared within a function (or block)
• Global variable – variables declared outside of every function definition
14
Local Variables
Local variables can be used only by statements that are inside that function
or block of code
When the function returns, the local variables are no longer available
Variables declared within a block are scoped to that block – local to that
block
• They can be accessed only within that block
15
Global Variable
Global variables are defined outside of all the functions, usually on top of the
program.
A local variable with the same name as a global variable hides the global
variable - when used within the function
• Scope resolution operator (::) can be used to access hidden global variable
16
Cont…
#include <iostream>
using namespace std;
int number=10; // Global variable
int main ()
{
int number=20; // Local variable
cout << number; //20
cout << ::number; //10
return 0;
}
17
Functions with default parameters
In C++, it is possible to define default values for all or some parameters of a function
When defining a function with default parameters, you assign default values in the
function declaration
Syntax:
When the function is called without passing arguments, then the default parameters
are used
• However, if arguments are passed while calling the function, the default values are
ignored
18
Cont…
#include<iostream>
using namespace std;
int main()
{
cout << sum() << endl; //30
cout << sum(20) << endl; //45
cout << sum(20,30) << endl; //65
cout << sum(20,30,40) << endl; //90
return 0;
} 18
Cont…
Order of Parameters: any parameter that has a default value must be
declared after any parameters that do not have default values
• any modifications made to the parameter inside the function do not affect
• Any changes made to the parameter inside the function are applied to the original
argument as well
The called function can access the actual variables in the calling program
A recursive function must have at least one termination condition which can be
satisfied
• Otherwise, the function will call itself indefinitely until the runtime stack overflows
25
Factorial using function recursion:
#include <iostream>
using namespace std;
int factorial(int n) {
if (n <= 1)
return 1; Output:
else
return n * factorial(n - 1);
}
int main() {
int number;
cout << "Enter a positive integer:";
cin >> number;
if (number < 0) {
cout << "Invalid input " << endl;
} else {
cout << "Factorial of " << number << " is: " << factorial(number) << endl;
}
26
}