We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 6
) ran aa Wy ae
Functions in C++
Statement Purpose:
1. Stage J (Journey)
A function is a group of statements that together perform a task. Every C++ program has
at least one function, which is mainQ, and all the most trivial programs can define
additional functions.
You can divide up your code into separate functions, How you divide up your code among
different functions is up to you, but logically the division usually is so each function
performs a specific task
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 streat(Q) to concatenate two strings, function memepy() to
copy one memory location to another location and many more functions.
Different programming languages name functions differently like a method or a sub-
routine or a procedure etc.
Defining a Function
‘The general form of a C++ function definition is as follows:
return type function_name( parameter list ) {
body of the 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.
+ 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.
43+ Function Body: The function body contains a collection of statements that define
what the function does.
Function Declarations
A function declaration tells the compiler about a function name and how to call the
function, The actual body of the function can be defined separately.
A function declaration has the following parts:
return_type function_name( parameter list };
For the above defined function max(), following is the function declaration:
int max(int num1, int num2);
Parameter names are not importan in function declaration only their type is required,
so following is also valid declaration:
int max(int, int);
Function declaration is required when you define a function in one source file and you
call that function in another file. In such case, you should declare the function at the top
of the file calling the function.
Calling a Function
While creating a C++ function, you give a definition of what the function has to do. To
use a function, you will have to call or invoke that function,
When a program calls a function, program control is transferred to the called function. A
called function performs defined task and when its return statement is executed or
when its function-ending closing brace is reached, it returns program control back to
the main program,
To call a function, you simply need to pass the required parameters along with function
name, and if function returns a value, then you can store returned value.
Recursive Functions
A function that calls itself is known as recursive function, And, this technique is known
as recursion.
The figure below shows how recursion works by calling itself over and over again,eds (oleae eT RL) Leg
void recurse()
{
recurse();
}
int main()
{
recurse();
ee a
<—___ nnn
<—
recursive
call
The recursion continues until some condition is met.
To prevent infinite recursion, if.else statement (or similar approach) can be used
where one branch makes the recursive call and other doesn't
2. Stage a1 (apply)
Lab Activity
1, Write a program which takes two integer numbers and find the maximum
number using functions.
#include
using namespace std;
// function declaration
int max(int num1, int num2);
int main () {
//Nocal variable declar:
int a= 100;
45int b = 200;
int ret;
// calling a function to get max value.
ret = max(a, b);
cout << "Max value is :" << ret << endl;
return 0;
}
// function returning the max between two numbers
int max(int num1, int num2) {
//\ocal variable declaration
int result;
if (num1 > num2)
result = num1;
else
resull
num2;
return result;
}
2. Write a function which calculates the sum of a two values.
include
using namespace std;
int sum(inta, int b=20) {
int result;
result=a+b;
return (result);
}
int main () {
//local variable declaration:
inta= 100;
int b = 200;
int result;
// calling a function to add the values.
result = sum(a, b);
cout << "Total value is :"
<< result << endl;
// calling a function again as follows.
result = sum(a);
46cout << "Total value is :" << result << endl;
return 0;
}
Example 2: Factorial of a Number Using Recursion
// Factorial of n= 1*2*3*.*n
#include
using namespace std;
int factorial(int);
int main()
{
intn;
cout<<"Enter a number to find factorial: ";
cin >> n;
cout << "Factorial of " << n <<" =" << factorial(n);
return 0;
}
int factorial(int n)
{
if(n>1)
{
return n*factorial(n-1);
}
else
{
return 1;
low this example works?
47int main() {
z is teturred
int factorial(int mum) ¢ enclanane
iF (num > 2) a
eturn nutefactorseh it
else [4]
return 1;
} 2°2 + 6jeretumed
Ant factortaa(int nun) ¢
if (num > 1) 2
return num+factorial(nyn-1); 2
else
return 13
} 21 =24s retuned
int factorial(int nun) {
if (num > 1)
retura
else
retura 13
+
Tie returned
int factorial(int nun) ¢
4 (num > 4)
Tetura num+factorie2(inum-1);
else
retura 1;
3. Stage V (verify)
Home Activity
1. Write a function which displays Prime Numbers Between two Intervals
2. Calculate Sum of Natural numbers using Recursion
4. Stage a2 (asses)
Student will submit their tasks to the lab engineers for assessment.
48