50.5k views
3 votes
The ability to write functions is one of the more powerful capabilities in C++. Functions allow code to be reused, and provides a mechanism for introducing the concept of modularity to the programming process. In addition to the concept of functions, C++ allows complex programs to be organized using header files (.h files) that contain the prototypes of functions and implementation files that (.cpp files) that contain the implementation (definition) of the functions.

In this programming assignment you will be asked to implement a number of useful functions using a header file and an implementation file. You will place the prototypes for your function in a .h file, and implement these functions in a .cpp file. You will need to write a driver (a program designed to test programs) to test your functions before you upload them.
[edit]Deliverables:
main.cpp
myFunctions.cpp
myFunctions.h
[edit]Function:
[edit]max
Precondition
two integer values exist
Postcondition
The value of the largest integer is returned.
The original integers are unchanged
If the integers have the same value then the value of either integer is returned.
Return
integer
Description
Function returns the value of the larger of two integers.
Prototype:
int max ( int, int )
Sample Parameter Values
m n max(m,n)
5 10 10
-3 -6 -3

User Dodol
by
4.7k points

1 Answer

0 votes

Answer:

The header file in C++ is as follows

myFunction.h

void max(int a, int b)

{

if(a>b)

{

std::cout<<a;

}

else

{

std::cout<<b;

}

}

The Driver Program is as follows:

#include<iostream>

#include<myFunction.h>

using namespace std;

int main()

{

int m,n;

cin>>m;

cin>>n;

max(m,n);

}

Step-by-step explanation:

It should be noted that, for this program to work without errors, the header file (myFunction.h) has to be saved in the include directory of the C++ IDE software you are using.

For instance; To answer this question I used DevC++

So, the first thing I did after coding myFunction.h is to save this file in the following directory ..\Dev-Cpp\MinGW64\x86_64-w64-mingw32\include\

Back to the code

myFunction.h

void max(int a, int b) {

-> This prototype is intialized with integer variables a and b

if(a>b) -> If the value of a is greater than b;

{

std::cout<<a; -> then the value of variable a will be printed

}

else

-> if otherwise,

{

std::cout<<b;

-> The value of b will be printed

}

}

Driver File (The .cpp file)

#include<iostream>

#include<myFunction.h> -> This line includes the uesr defined header

using namespace std;

int main()

{

int m,n; -> Two integer variables are declared

cin>>m; -> This line accepts first integer input

cin>>n; -> This line accepts second integer input

max(m,n);

-> This line calls user defined function to return the largest of the two integer variables

}

User Pktangyue
by
4.0k points