Professional Documents
Culture Documents
CPP 5
CPP 5
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.
Function Prototype
Function Definition
Function Call
Syntax
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.
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
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
END
Conclusion: Programs have been executed successfully.