Could someone answer a couple of questions about this code?

I got this program to work by "copy catting" some similiar programs, but I want to understand exactly what I did.

Below my function void PrintAdvertising(Advertising sAd)

So this function is for printing results from calculations done using variables from my struct called Advertising; correct?

the Advertising in the parenthesis is the name of my struct, correct?

and sAd is where I am a little confused is it a variable I am declaring? when I first tried to build my prgram the compiler complained about my variables until I added the sAd. before them. I think I remember that the period is a member selection operator. So am I right in thinking that Advertising is the struct and sAd would be a member (one days revenue) using the Advertising struct?

One more question since my struct is global the variables of the struct are global correct?

If i declared my struct with in a function (like my void function PrintAdvertising) then the struct variables scope would only be local to that function correct? and can you use a prototype on a struct like you can a function?

Thank you in advance!

here's the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>

using namespace std;

struct Advertising
{
    int nAds;                 //#of ads shown to readers
    float fClickThrough;      //% of click through
    float fRevenue;           //Average renenue per ad
};

void PrintAdvertising(Advertising sAd)
{
    using namespace std;
    cout << "Number of Ads:" << sAd.nAds << endl;
    cout << "Percentage of click through:" << sAd.fClickThrough << endl;
    cout << "Average Revenue per ad:" << sAd.fRevenue << endl;


    cout << "total daily revenue:" << (sAd.nAds * sAd.fClickThrough * sAd.fRevenue) << endl;
}

int main()
{
    using namespace std;
    Advertising sAd;

    cout << "How many ads shwn today:";
    cin >> sAd.nAds;
    cout << "What was the percentage of click through:";
    cin >> sAd.fClickThrough;
    cout << "What was the average per ad:";
    cin >> sAd.fRevenue;

    PrintAdvertising(sAd);
    return 0;
}



In the PrintAdvertising() function, Advertising is the type of your variable sAd. This function is actually taking the entire structure on its stack: You should use a pointer or a reference.

In main(), sAd is an instance of your structure Advertising. The "dots" reference members of this structure.
1
2
3
4
5
6
7
8
9
10
Advertising sAd; //is analogous to
int n; //or
double x; //or
char c; //or
bool b;

//formats for declaring/creating variables are
DataType identifier_name; //or
DataType identifier_name( /*constructor parameters*/... ); //or sometimes
DataType identifier_name = { /*initializer list*/... };
Last edited on
So another question for you MathHead;
when declaring/creating a variable like this, would you then,(or could you then) control your your result type by casting?
sAd is the data type Advertising. Consider the following:

1
2
3
4
int x; //declaring an integer x
float y; //declaring a floating point number y
string z; //declaring a string z
Advertising sAd; //declaring Advertising sAd, you made the data type Advertising 


You can prototype a structure (and indeed, in some situations you need to).

The Advertising structure is global, but the variable sAd is local to main(). You could only reference sAd inside main(). When you define what Advertising is, all you are doing is saying if at any point in the program you declare a data type Advertising, this is what I want that data type to be, and this is what I want it to do. On line 5 you haven't actually made anything yet, you are just setting the stage so you can later use it.

On line 12, the function void PrintAdvertising(Advertising sAd) is a function that takes a data type Advertising, and does not return any values. The name sAd on this line is irrelevant, you can pass it any variable of data type Advertising. So the following would work:
1
2
3
4
Advertising sAd, Joe, x, rex, bar;
PrintAdvertising(Joe);
PrintAdvertising(bar);
PrintAdvertising(x);
All that function needs is a data type Advertising, what you name the variable is irrelevant.

The dot operator (.) accesses a member variable within the structure. It allows you to use the data variables within the structure. So the following:
1
2
3
4
Advertising bar;
bar.nAds = 5; //sets the nAds variable to 5
bar.fClickThrough = 0.6; //sets the fClickThrough variable to 0.6
Advertising.fRevenue = 2.3; //ERROR: Advertising is not a variable 


Last point: sAd is not global, the variable sAd of data type Advertising is declare in main(), so it is only useable inside of main(). The function PrintAdvertising has you pass in a data type Advertising, that the author chose to name sAd, so from within that function, whenever you refer to sAd, you are not referring to the one declared in main(), you are referring to whichever one the user chose to pass into the function.



when declaring/creating a variable like this, would you then,(or could you then) control your your result type by casting?

I don't understand what you mean by "result type". You are creating a variable, and storing it in memory. All variables have a type, and an identifier (i.e. name, way to refer to the memory).

When you deal with parameters it's the same, but the variables' value is copied from the passed arguments.
Example:
1
2
3
4
5
6
7
8
9
10
11
double f(double x) {
	return x*x - 4;
}

//program starts here
int main() {
	double num = 1.1;
	double ans = f(num);
	cout << ans << endl;
	return 0;
}

This code works much like the program below...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main() {
	double num = 1.1;
	double ans;
	 // ans = f(num)
	 {
		 double return_value;
		 {
			 //parameters
			 double x = num;
			 //function's body
			 return_value = x*x – 4;
		 }
		 ans = return_value;
	 }
	cout << ans << endl;
	return 0;
}
First, I wanted to say thank you to everyone for being so helpful. Second, you are all very intelligent!

Third I want to show another program that is similar and make a few assumptions from what you all have told me and see if i'm correct. (it helps my understanding this way.)


So, below, the function void Mutliply(Fraction sF1, Fraction sF2)

my function name is Multiply, it is returning nothing (void), Fraction is my type, sF1 and sF2 are my variables that are of the type "Fraction". So (Fraction sF1, Fraction sF2) are my parameters. Correct???????

Then, sF1.nNumerator are variables also but are variables of the Multiply function, where as sF1 or sF2 are variables of the main function. Correct????

Finally Mathhead was talking about arguments, is an argument the value of a variable at a given moment?????




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>

using namespace std;

struct Fraction
{
    int nNumerator;
    int nDenominator;
};

void Multiply(Fraction sF1, Fraction sF2)
{

    cout << static_cast<float>(sF1.nNumerator * sF2.nNumerator) / (sF1.nDenominator * sF2.nDenominator);

}

int main()
{
    Fraction sF1;
    cout << "Input the 1st numerator:";
    cin >> sF1.nNumerator;
    cout << "Input 1st denominator:";
    cin >> sF1.nDenominator;

    Fraction sF2;
    cout << "Input 2nd numerator:";
    cin >> sF2.nNumerator;
    cout <<"Input 2nd denominator:";
    cin >> sF2.nDenominator;

    Multiply(sF1, sF2);
    return 0;
}




An argument is another name for a function parameter.
I think I can help you better understand by using simple data types for a moment. I think you are getting confused by having the same variable names in main as well as the function, I am hoping to better explain scope.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

void Foo(int x)
{
  x = 3;
}

void Foo2(int x)
{
   x = x + 3;
   cout << x;
}

int main()
{
   int bar = 1;
   Foo(bar);
   cout << bar; //this will output 1
   int x = 2;
   Foo2(x); //this will call Foo2, then output 5 while in Foo2
   cout << x; //this will output 2
}


A variable is local to the brackets containing it, and can only be used by the brackets containing it. When you call a function, you create a new variable, even if it has the same name as the variable you pass in, that functions separate. So when I call function Foo(int), it doesn't modify the variable bar that I pass in, because it takes the value of bar, and assigns it to the new variable that is local to Foo(int), x. So when I cout bar, bar is still 1, because Foo(int) didn't modify it. When I am passing in bar, I am not passing in a variable, I am passing in the value of bar, 1. Then Foo(int) takes the value of 1, and assigns it to the variable x.

When I call Foo2(int) it creates a new variable (x) that has the value that is passed in (the value of x, which is 2). Even tho they have the same name, the x declared in main() is local to the brackets of main(), and since the function is outside the brackets of main(), it can't 'see' the x variable declare in main(). This is why you pass in a variable when you call Foo2(int). Once inside Foo2(int), if you modify x, it is only referring to the x that was declared by the parameter list. So when I cout x on line 11 it refers to the x that is local to Foo2(int), which is 5. Then, we Foo2(int) exits, and back in main(), when I cout x, it uses the x that is local to main(), which has the value 2.

Going back to your code with fractions, and to answer your questions:

You are correct in that multiply is the function name, that returns nothing, and takes 2 arguments. Everything in your 4th paragraph is correct.

The fifth paragraph, you have 2 separate sF1 variables, 1 in main, and another in the multiply(Fraction, Fraction) function. sF1.nNumerator is called a member (I believe it could be called a member variable, someone correct me if I'm wrong) of Fraction. Each Fraction has a member called nNumerator of the type int.

Last edited on
An argument is another name for a function parameter.

Um...
1
2
3
4
5
6
7
8
9
template <typename R, typename P>
R func(P parameter);

int main() {
  ...
  func( argument );
  ...
}
...


They are related, but not the same. (I guess the difference is just semantics.)
Here is a good definition:

http://en.wikipedia.org/wiki/Parameter_(computer_programming)#Parameters_and_arguments

Thanks, MathHead200: I re-learned something I had forgotten!
Just wondering remojr76, when you multiply two numbers you get a number back so shouldn't
void Mutliply(Fraction sF1, Fraction sF2); be Fraction Mutliply(Fraction sF1, Fraction sF2);
Topic archived. No new replies allowed.