Identify Items in the code

I need to identify some items on the code below, Is pretty much done but I need to make sure I have the correct answers.
Need to identify:
a.- Function prototype, function heading, function body, and function definitions.
b.- Function call statements, formal parameters, and actual parameters.
c.- Value parameters and reference parameters.
d.- Local variables and global variables.

As always, your help will be much appreciated.

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

#include<iostream>

using namespace std;

int one;
void hello(int&, double, char);

int main ()
{

int x;
double y;
char z;

hello(x, y, z);

hello(x, y -3.5, 'S');

}

void hello(int& first, double second, char ch)
{
int num;
double y;
int u;

}




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
#include<iostream>

using namespace std;

int one;   //Global Variable
void hello(int&, double, char);   //Function Prototype and Formal parameters

int main ()  //Function Heading
{

int x;         //x,y and z are local vars
double y;
char z;

hello(x, y, z);   //Function call and actual parameters

hello(x, y -3.5, 'S');

}

void hello(int& first, double second, char ch) //function def
{
int num;                  //func body for the next 2 lines as well
double y;
int u;

}


this is my guess, do not take any of it too seriously
Need assistance (second opininon) to confirm this answer.. thanks for your input sript coder.
Num, y and u are local variables inside the function. Them cannot be accessed by any other function if not given as parameters inside that function.
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
#include<iostream>

using namespace std;

int one; // global variable
void hello(int&, double, char); // function prototype, formal parameters

int main () // function definition
{ // beginning of function body

int x; // local variable
double y; // local variable
char z; // local variable

hello(x, y, z); // function call, actual parameters

hello(x, y -3.5, 'S'); // function call, actual parameters

} // end of function body

void hello(int& first, double second, char ch) // function definition
// int& is a reference parameter, double second is a value parameter, char ch is a value parameter
{ // beginning of function body
int num;  // local variable
double y; // local variable
int u; // local variable

} // end of function body 


To be specific, when you pass by reference (int & first) if you say, do this:

first = 5;

You are changing the value of whatever argument you pass to that function in your int main(), where as if you pass by value, the compiler creates a copy of that variable, and you can do whatever you want to it, and it won't effect the variable in int main().

If you want to pass by reference without fear of accidentally changing the value of it, you'll want to use const

1
2
3
4
 void hello(const int & first, double second, char ch)
{
first = 5;  // error!
}


So why pass by reference? Two reasons:

1. Sometimes it's more memory/time efficient to take the address of whatever you're passing to the function rather than make a copy of it.
2. To change its value.
Last edited on
Thanks so much guys for your input.
Topic archived. No new replies allowed.