What are functions in C++??

Just tell me what're functions??

Here're codes of some functions:

void getInput(double sal[][2], int numEmps);
void findUnluckies(double sal[][2], int numEmps, int lucky[]);
void markIfUnlucky(double sal[][2], int numEmps, int lucky[], int upperBound, int empNbr);


just tell me is there any hard n strict rule of writing a function???Like here's a function named getInput ...there is 1 array n 1 variable..If I would write this function how I'll get an idea I need 1 array n 1 variable??


Last edited on
A function is a collection of instructions.
What you posted in your first post aren't functions. They're declarations of functions (names).

This is a function:
1
2
3
4
5
6
int function1(char a) // this is the name but notice how it doesn't end with semicolon ;
{
    // this is the body of the function, which contains the instructions, in the desired order
    std::cout << a << std::endl;
    return 1;
}


Read this: http://www.cplusplus.com/doc/tutorial/functions/
closed account (zb0S216C)
In layman's terms, it's a group of instruction statements that are executed once the function is invoked (called). The code you posted are function prototypes; a requirement in C++. Prototype functions are function interfaces that describe the datum returned, parameters, and symbol. Prototypes omit the body until a later time.

Wazzak
Tyx for both of you..

Can someone explain these bold lines const int arraySize=100;
double sal[arraySize][2];
int lucky[arraySize] = {0};
int numEmps;


cout << "\n Please enter the total number of employees in your company: ";
cin >> numEmps;
cout<<'\n';

getInput(sal, numEmps);


#include <iostream.h>
void getInput(double sal[][2], int numEmps);
void calcNetSal(double sal[][2], int numEmps);
void findUnluckies(double sal[][2], int numEmps, int lucky[]);
void markIfUnlucky(double sal[][2], int numEmps, int lucky[], int upperBound, int empNbr);
void printUnluckies(int lucky[], int numEmps);
main()
{
const int arraySize=100;
double sal[arraySize][2];
int lucky[arraySize] = {0};
int numEmps;


cout << "\n Please enter the total number of employees in your company: ";
cin >> numEmps;
cout<<'\n';

getInput(sal, numEmps);
void getinp(double sal[][2], int numEmps)
{
for (int i = 0; i < numEmps; i++)

{
cout << "\n Please enter the gross salary for employee no." << i << ": ";
cin >> sal[i][0];

}
}

cout << "\n\n Calculating the net salaries...";
calcNetSal(sal, numEmps);

cout << "\n\n Locating the unlucky employees ... ";
findUnluckies(sal, numEmps, lucky);

cout << "\n\n Printing the unlucky employees ... ";
printUnluckies(lucky, numEmps);
}

void calcNetSal(double sal[][2], int numEmps)
{
for (int i = 0; i < numEmps; i++)

{
if (sal[i][0] >=0 && sal[i][0] <= 5000)
{

sal[i][1] = sal[i][0];
}
else if(sal[i][0] >= 5001 && sal[i][0] <= 10000)
{
sal[i][1] = sal[i][0] - (.05 * sal[i][0]); }
else
if (sal[i][0] >= 10001 && sal[i][0] <= 20000) { sal[i][1] = sal[i][0] - (.10 * sal[i][0]); }
else
if (sal[i][0] >= 20001)
{

sal[i][1] = sal[i][0] - (.15 * sal[i][0]);
} else {
}
}
}
void findUnluckies(double sal[][2], int numEmps, int lucky[])
{
for (int i = 0; i< numEmps; i++)

{
if(sal[i][0] >= 0 && sal[i][0] <= 5000)
{

}
else if(sal[i][0]>=5001 && sal[i][0]<=10000)
{
markIfUnlucky(sal, numEmps, lucky, 5001, i);
}
else if (sal[i][0] >= 10001 && sal[i][0] <= 20000)
{
markIfUnlucky(sal, numEmps, lucky, 10001, i);
}
else if (sal[i][0] >= 20001)
{
markIfUnlucky(sal, numEmps, lucky, 20001, i);
}
}
}
void markIfUnlucky(double sal[][2], int numEmps, int lucky[], int upperBound, int empNbr)
{
for (int i = 0; i < numEmps; i++)
{

if (sal[i][0] < upperBound && sal[i][1] >= sal[empNbr][1])
{
lucky[empNbr] = 1;

break;
}
}
}
void printUnluckies(int lucky[], int numEmps)
{
for (int i = 0; i < numEmps; i++)
{
if(lucky[i] == 1)
{
cout <<"\n Employee No.: " << i;
}
}
}





Last edited on
Can someone explain this program line by line???


No. Please don't ask that. We can't learn instead of you.
Learn, then ask questions about what you don't understand.

Just tell me what're the bold lines doing in this part??

const int arraySize=100;
double sal[arraySize][2];
int lucky[arraySize] = {0};
int numEmps;


cout << "\n Please enter the total number of employees in your company: ";
cin >> numEmps;
cout<<'\n';

getInput(sal, numEmps);
void getinp(double sal[][2], int numEmps)
{
for (int i = 0; i < numEmps; i++)

{
cout << "\n Please enter the gross salary for employee no." << i << ": ";
cin >> sal[i][0];

}
Last edited on
int lucky[arraySize] = {0};
It initializes the first element of the lucky array to 0.

getInput(sal, numEmps);
Calls the getInput function with the variables sal and numEmps as parameters.

Hey look, a nested function inside main(). Why does crap like this even compile?


else if(sal[i][0]>=5001 && sal[i][0]<=10000)
{
markIfUnlucky(sal, numEmps, lucky, 5001, i); what does it mean??
}
else if (sal[i][0] >= 10001 && sal[i][0] <= 20000)
{
markIfUnlucky(sal, numEmps, lucky, 10001, i);
}
else if (sal[i][0] >= 20001)
{
markIfUnlucky(sal, numEmps, lucky, 20001, i);
Last edited on
closed account (zb0S216C)
Thanz wrote:
int lucky[arraySize] = {0}; (sic)

Initializes all elements of the array to zero.

Thanz wrote:
getInput(sal, numEmps); (sic)

This is function call. More specifically, an invocation of getInput( ). During the invocation, two arguments are copied into the corresponding parameters of getInput( ).

Wazzak
Last edited on
Topic archived. No new replies allowed.