You are on page 1of 10

Exp 5

Aim: Write C++ programs


Theory:
Functions:
C++ functions are a group of statements in a single logical unit to perform some
specific task.

Along with the main function, a program can have multiple functions that are designed
for different operation.

The results of functions can be used throughout the program without concern about
the process and the mechanism of the function.

C++ Functions
In POP (Procedural Oriented Programming) language like C, programs are divided
into different functions but in OOP (Object Oriented Programming) approach
program is divided into objects where functions are the components of the object.

Generally, C++ function has three parts:

 Function Prototype
 Function Definition
 Function Call

C++ Function Prototype


While writing a program, we can’t use a function without specifying its type or without
telling the compiler about it.

So before calling a function, we must either declare or define a function.

Thus, declaring a function before calling a function is called function declaration or


prototype which tells the compiler that at some point of the program we will use the
function of the name specified in the prototype.

Syntax

Return_type function_name(parameter list)

Note: function prototype must end with a semicolon.


Here, return_type  is the type of value that the function will return. It can be int,float or
any user-defined data type.

Functiom_name means any name that we give to the function. However, it must not
resemble any standard keyword of C++.

Function Declaration:
A function declaration tells the compiler about the number of
parameters function takes, data-types of parameters and return type
of function. Putting parameter names in function declaration is
optional in the function declaration, but it is necessary to put them in
the definition.
It is always recommended to declare a function before it is used. We
can do both declaration and definition at the same place.
C++ also allows to declare and define functions separately, this is
especially needed in case of library functions. The library functions
are declared in header files and defined in library files.

Parameter Passing to functions


The parameters passed to function are called actual parameters.
The parameters received by function are called formal parameters.
There are two most popular ways to pass parameters.
Pass by Value: In this parameter passing method, values of actual
parameters are copied to function’s formal parameters and the two
types of parameters are stored in different memory locations. So any
changes made inside functions are not reflected in actual parameters
of caller.
Pass by Reference Both actual and formal parameters refer to same
locations, so any changes made inside the function are actually
reflected in actual parameters of caller.
Parameters are always passed by value.

Main Function:
The main function is a special function. Every C++ program must
contain a function named main. It serves as the entry point for the
program. The computer will start running the code from the
beginning of the main function.
Types of main Function:
1) The first type is - main function without parameters
2) Second type is - main function with parameters
The reason for having the parameter option for the main function is
to allow input from the command line.
When you use the main function with parameters, it saves every
group of characters (separated by a space) after the program name as
elements in an array named argv.
Since the main function has the return type of int, the programmer
must always have a return statement in the code. The number that is
returned is used to inform the calling program what the result of the
program’s execution was. Returning 0 signals that there were no
problems.
Program (i):
Program:
#include <iostream>
using namespace
std;
int add(int x, int y)
{
int z;
z = x +
y;
return z;
}
int main()
{
int n1, n2, s;
cout << "Enter first number:
"; cin >> n1;
cout << "Enter second number:";
cin >> n2;
s = add(n1, n2);
cout << "Sum of the two numbers is = " <<
s; return 0;
}

Flowchart:
int main() int add(int, int)
START START

Declare x, y, z
Declare n1, n2, s

z = x + y;
Read n1, n2

return z;
s= add(n1, n2)

END
Print s

END

Output:

Program (ii):
Program:
#include<iostream>
using namespace std;
void swap(int &a, int
&b)
{
int t;
t=a;
a=b;
b=t;
}
int main()
{
int n1, n2;
cout << "Enter first number:
"; cin >> n1;
cout << "Enter second number:
"; cin >> n2;
cout << "Before swapping" << endl;
cout << "First Number = " << n1 << endl;
cout << "Second Number = " << n2 <<
endl; swap(n1,n2);
cout << "After swapping" << endl;
cout << "First Number = " << n1 << endl;
cout << "Second Number = " << n2 <<
endl; return 0;
}
Flowchart:
int main() void swap(int &a, int &b)

START START

Read n1, n2 Declare t

t=a;
Print Before swapping
a=b;
b=t;
swap(n1,n2)

END
Print After swapping

END

Output:
Program (iii):
Program:
#include<iostream>
using namespace std;
int fact(int n);
int main()
{ int n;
cout << "Enter a number:
"; cin >> n;
cout << "Factorial of " << n << " = " <<
fact(n); return 0;
}
int fact(int n)
{ if(n > 1) {
return n * fact(n - 1);
} else
{ return 1; }
return 0;
}

Flowchart:
int main() int fact(int n)

START START

False
Read n
if n>1

Print fact(n) True


return 1;
return n*fact(n-1);
END

END
Conclusion: Programs have been executed successfully.

You might also like