function parameter

invisible.hippo

Arch-Supremacy Member
Joined
Nov 8, 2008
Messages
17,902
Reaction score
315
i understand that

int addition (int a, int b)
{
}

is the same as

int addition ()
{
int a;
int b;
}

However i am confused with this example
How to read data from text file. - C++ Forum


void getMyInfo(fstream&, char[], MyInfo&);

then in the void getMyInfo function the user declared as
void getMyInfo(fstream& afile, char filename[], MyInfo& a)


isnt fstream& the(&) ampersandm means ass by reference? so the afile will pass the value to where?
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,550
Reaction score
1,302
i understand that

int addition (int a, int b)
{
}

is the same as

int addition ()
{
int a;
int b;
}

Your understanding is INCORRECT. Who is the one whom taught you that they are the same?

While both functions do create local variables "a" and "b", the former function declaration is "int addition (int,int)" and the latter is "int addition(void)". The former takes in 2 parameters during invocation and the latter takes in none.

However i am confused with this example
How to read data from text file. - C++ Forum


void getMyInfo(fstream&, char[], MyInfo&);

then in the void getMyInfo function the user declared as
void getMyInfo(fstream& afile, char filename[], MyInfo& a)

Due to the way how the C/C++ parser works, any use of labels (function names, variables, ... ) must be declared BEFORE it is used.

"void getMyInfo(fstream&, char[], MyInfo&);" is known as the prototype, which declare the existent of such a function named getMyInfo that takes in 3 parameters of types "fstream&", "char[]", "MyInfo&" in the same following order and last but not least return a void type.

From now on, all source codes that follows or get included as follows will know there is such a function. It is for the compiler to put in the necessary placeholders so that it will know where to replace it with the proper offset address for invocation during execution.

"void getMyInfo(fstream& afile, char filename[], MyInfo& a)"
is the actual definition of the function with definition body.

Now you can always combine both declaration and definition together by just performing the definition before any first use of the function. However there is at least 1 case this will not be possible, which is a circular recursive function call as such

Code:
A() {
  B();
}

B() {
  A();
}

function B when parsed will know the existent of function A, but earlier when function A is parsed, the compiler doesn't know function B exist.

As such, the only way to solve this problem is declare a prototype for B first as such

Code:
void B(void);

void A(void) {
  B();
}

void B(void) {
  A();
}

isnt fstream& the(&) ampersandm means pass by reference? so the afile will pass the value to where?

You are asking a wrong question because of your wrong understanding about function parameters. You should be passing an argument into the function call getMyInfo and that argument will be referenced by afile. This is an example

Code:
fstream mystream;

getMyInfo(mystream, ...)

Hence "afile" is now referencing the object "mystream"
 

invisible.hippo

Arch-Supremacy Member
Joined
Nov 8, 2008
Messages
17,902
Reaction score
315
Hi, i understand the difference between prototype and actual parameter. However can you please explain on parameters during invocation. if i have parameters in the function, firstly must i use it? Secondly how to use it?

In this example, what is the difference or rather how does it differ that function a take in parameters during invocation and function b does not. Im kinda of confused as both still give me the same outcome (which will cout to the monitor)

function a (int a=1)
{
cout << a;
}

is the same as
function b ()
{
int b =1;
cout << b;
}
 

invisible.hippo

Arch-Supremacy Member
Joined
Nov 8, 2008
Messages
17,902
Reaction score
315
You are asking a wrong question because of your wrong understanding about function parameters. You should be passing an argument into the function call getMyInfo and that argument will be referenced by afile. This is an example

Code:
fstream mystream;

getMyInfo(mystream, ...)
Hence "afile" is now referencing the object "mystream"

does it mean, my afile will store and value and then i will use other object to access the value? sort of like pointer?

ps, can you recommend me a book that is easy to pick up c++ skill
currently im reading up c++ primer & The C++ Programming.Language.4th.Edition.Jun.2013[A4]. I find it too complex to understand.
 
Last edited:

KnightNiwrem

Senior Member
Joined
Jun 1, 2014
Messages
1,057
Reaction score
0
Hi, i understand the difference between prototype and actual parameter. However can you please explain on parameters during invocation. if i have parameters in the function, firstly must i use it? Secondly how to use it?

In this example, what is the difference or rather how does it differ that function a take in parameters during invocation and function b does not. Im kinda of confused as both still give me the same outcome (which will cout to the monitor)

function a (int a=1)
{
cout << a;
}

is the same as
function b ()
{
int b =1;
cout << b;
}

a(2) will return something else.
b(2) gives an error.

What do you mean by "use"? You have to pass in parameters without default values. If you are not going to use it in the function, why bother passing it, though?
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,550
Reaction score
1,302
does it mean, my afile will store and value and then i will use other object to access the value? sort of like pointer?

ps, can you recommend me a book that is easy to pick up c++ skill
currently im reading up c++ primer & The C++ Programming.Language.4th.Edition.Jun.2013[A4]. I find it too complex to understand.

I can't recommend you easy books because normally those bored me. I normally prefer o'reilly types of books and cut the chase and get to the core of the issue since at my level, I don't need the author to handhold me on how to go about doing each thing.

You are advice to go kinokuniya or any bookstore and read through the books few chapters and see if the author style suits you. It's not about books being easy, it's about how the author structure the topics to make it flow nicely in you.

Reference is pointer like, but in C++ it function like an alias rather than pointer.

My name is David, but maybe in another world, people call me John. That doesn't mean it's 2 different people, it's still the same me. Because in the world, there is only one ME. In actual fact, David is an alias just as John is.

When I wrote
Code:
Class david;

"david" is an alias for the object created.

It seems different from
Code:
Class *david = new Class();

But actually the idea is very similar. It's analogous to hardlinks and symbolic links in the Unix filesystem.
Pointers are symbolic links, while alias/references are hardlinks.

When I do this
Code:
Class &john = david;

What I did is on top on the earlier alias called "david" which is name for an object, I created another alais called "john" also referencing the same object. Memory addresses are at play here because ultimately your computer will not see "david" or "john". The computer is only interested in the object that you are referring to which is Object X at memory address YYYYY;

Pointer is different in the language, it is a symbolic link and hence it creates a memory location to store another memory location as value.
Code:
int i = 123;
int *m = &i;
int &n = i;

Pointer "m" has a part of the memory allocated maybe at memory location XYZ to store the memory location of integer "i". "m" is an alias for the memory location at XYZ. When I when to access the value at which the pointer "m" is pointing to, I will need to dereference it like this
Code:
printf("%d\n", *m);

On the other hand, reference "n" is an alias for integer "i", hence when you want to use the reference, you just have to code
Code:
printf("%d\n", n);

This reference concept is for C++, and it the naming convention is different from languages likes Java, where References in Java are indeed strict Pointers concept.

You see the confusion is what we called "devil in the details". You don't understand what goes under the hood, you forever need to assume based on the concept and can't fully understand why different programming languages called the same thing with different names and how they are actually implemented.
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,550
Reaction score
1,302
how come this example
reading and writing a file - C++ Forum

the parameter can declare like this
void displayRecord(fstream &);

You know, look carefully at the codes below after pretty printing

This is a wrongly written piece of source code. The intention as interpreted by the compiler can be lenient at times and totally looks fine but actually a very different piece of software is compiled.

This will compile in g++ but actually, C++ does not support nested functions, anyway, these are not even nested functions. I seriously don't know how the compiler actually decide it is correct. Perhaps the prototypes in the main functions are pushed out of the scope or just ignored since it is just a prototype after all.

Code:
#include <iostream>
#include <fstream>
using namespace std;
void addRecord(fstream &);
void displayRecord(fstream &);
void editRecord(fstream &);
void deleteRecord(fstream &);
// declare a structure for the record
// array sizes
const int NAME_SIZE =50, STUDENTID_SIZE = 50;
struct info
{
    char name[NAME_SIZE];
    char studentID[STUDENTID_SIZE];
    int age;
    double GPA;
};
int main()
{
    info person; // to hold info about person
    char again; // to hold y or n
    int choice;
    fstream people;
    cout <<"Student Database Management Optionsn";
    cout << "---------------------------------------"<< endl;
    cout << "(1)Add Student Recordn";
    cout << "(2)Display Student Recordn";
    cout << "(3)Edit Student Recordn";
    cout << "(4)Delete Student Record n";
    cout << "---------------------------------------"<<endl;
    cout << "Select Your Choice: "<< endl;;
    cin >> choice;
    while (choice < 1 || choice > 5)
    {
        cout << "Please Re-Enter your choice (1)(2)(3)(4)" << endl;
        cin >> choice;
    }
    void addRecord(fstream & ); [COLOR="Red"]// <-- notice the semicolon after the prototype ?
    [/COLOR]
    [COLOR="red"]// VVVVVVVVVVVVVVVVVVVVVV : Notice this is not a function body ? It is a scope created using the curly braces[/COLOR]
    {
        if ( choice ==1)
        {
            fstream people("people.txt", ios::out | ios::binary);
            //open file for binary output
            cout << "Enter the following information needed" << endl;
            cout << "Name:"<< endl;
            cin >> person.name;
            cout << endl;
            cout << "Age" << endl;
            cin >> person.age;
            cout << endl;
            cout << "GPA" << endl;
            cin >> person.GPA;
            cout << endl;
            cout << "Student ID" << endl;
            cin >> person.studentID;
            // write the contents to the file
            people.write(reinterpret_cast<char*>(&person),sizeof(person));
        }
    }
    [COLOR="red"]// ^^^^^^^^^^^^^^^^^^^^^^^^[/COLOR]
    void displayRecord(fstream &);
    {
        if (choice == 2)
        {
            people.open("peopel.txt", ios::in | ios::binary);
            cout << endl;
            cout << "Here is the file on Record:"<<endl;
            people.read(reinterpret_cast<char *>(&person),sizeof(person));
            if (!people.eof())
            {
                cout << "Name:"<< endl;
                cout << person.name << endl;
                cout << endl;
                cout << "Age" << endl;
                cout << person.age << endl;
                cout << endl;
                cout << "GPA" << endl;
                cout << person.GPA << endl;
                cout << endl;
                cout << "Student ID" << endl;
                cout << person.studentID << endl;
            }
            cout << endl;
            cout << "Thats all the data in the file!n";
            people.close();
        }
    }
    system("PAUSE");
    return 0;
}
 
Last edited:

invisible.hippo

Arch-Supremacy Member
Joined
Nov 8, 2008
Messages
17,902
Reaction score
315
a(2) will return something else.
b(2) gives an error.

What do you mean by "use"? You have to pass in parameters without default values. If you are not going to use it in the function, why bother passing it, though?

Hi, i hope im not irriating you guys off with my amature question :s13:
both way still end up with the same outcome, so i kinda of confuse what the difference with prototype with parameter and void prototype.

Is it both similar except the way the variable are declare? Just that one is declare such that one function have argument and the other does not?

I still feel that this statment has a deeper meaning

"Your understanding is INCORRECT. Who is the one whom taught you that they are the same?

While both functions do create local variables "a" and "b", the former function declaration is "int addition (int,int)" and the latter is "int addition(void)". The former takes in 2 parameters during invocation and the latter takes in none."

or is it if i were to declare a function with argument, this argument can be called up by other funnction using reference?

for example int b in function b can be reference like in the above example
Code:
 fstream mystream;

getMyInfo(mystream, ...)

Code:
#include <iostream>

using namespace std;


void functiona()
{
    int a =1;
    cout << a;
}


void functionb(int b=10)
{
    cout << b;
}


int main()
{
    cout << "function a    ";
    functiona();

    cout << endl << "function b   ";
    functionb();

    return 0;
}

Here's the executed program
Code:
function a 1
function b 10
 
Last edited:

KnightNiwrem

Senior Member
Joined
Jun 1, 2014
Messages
1,057
Reaction score
0
Hi, i hope im not irriating you guys off with my amature question :s13:
both way still end up with the same outcome, so i kinda of confuse what the difference with prototype with parameter and void prototype.

Is it both similar except the way the variable are declare? Just that one is declare such that one function have argument and the other does not?

I still feel that this statment has a deeper meaning

"Your understanding is INCORRECT. Who is the one whom taught you that they are the same?

While both functions do create local variables "a" and "b", the former function declaration is "int addition (int,int)" and the latter is "int addition(void)". The former takes in 2 parameters during invocation and the latter takes in none."

or is it if i were to declare a function with argument, this argument can be called up by other funnction using reference?

for example int b in function b can be reference like in the above example
Code:
 fstream mystream;

getMyInfo(mystream, ...)

Code:
#include <iostream>

using namespace std;


void functiona()
{
    int a =1;
    cout << a;
}


void functionb(int b=10)
{
    cout << b;
}


int main()
{
    cout << "function a    ";
    functiona();

    cout << endl << "function b   ";
    functionb();

    return 0;
}

Here's the executed program
Code:
function a 1
function b 10

I said to call the method with an integer parameter: 2.

Like this:
Code:
void functiona()
{
    int a =1;
    cout << a;
}


void functionb(int b=10)
{
    cout << b;
}

followed with:
Code:
functiona(2);
functionb(2);

Default parameters in C++
 

KnightNiwrem

Senior Member
Joined
Jun 1, 2014
Messages
1,057
Reaction score
0
ps, can you recommend me a book that is easy to pick up c++ skill
currently im reading up c++ primer & The C++ Programming.Language.4th.Edition.Jun.2013[A4]. I find it too complex to understand.

If you are at a stage where you don't understand, yet, the importance of function parameters, it would probably do you no harm to relearn the stuff.

Standford Intro course to C++

Why are you trying to learn C++ though? If you don't really care about the language to learn, Harvard provides free introductory courses in C, and MIT does it in Python.
 

*Pickle*

Arch-Supremacy Member
Joined
Aug 18, 2014
Messages
19,117
Reaction score
5
Hi, i understand the difference between prototype and actual parameter. However can you please explain on parameters during invocation. if i have parameters in the function, firstly must i use it? Secondly how to use it?

In this example, what is the difference or rather how does it differ that function a take in parameters during invocation and function b does not. Im kinda of confused as both still give me the same outcome (which will cout to the monitor)

function a (int a=1)
{
cout << a;
}

is the same as
function b ()
{
int b =1;
cout << b;
}

Hi there, glad you're picking up a programming language.

In your first function a, you declared a default parameter (int a=1) which means that when you use the function without passing in any arguments, the default parameter that you specified is used. And in this case, a(); is equivalent a(1);.

In your second function b, you declared a function which takes in no parameters. Thus, doing something like b(1) will give you an error.

If you had provided an argument to a (e.g a(2)), the compiler doesn't complain because it's valid and that the final results would have been different instead.

PS: Do take a look at this good book which dives deeply into pointers. Amazon.com: Pointers on C (9780673999863): Kenneth Reek: Books
 
Last edited:
Important Forum Advisory Note
This forum is moderated by volunteer moderators who will react only to members' feedback on posts. Moderators are not employees or representatives of HWZ Forums. Forum members and moderators are responsible for their own posts. Please refer to our Community Guidelines and Standards and Terms and Conditions for more information.
Top