Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,163 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,896 people online right now. Registration is fast and FREE... Join Now!




Help me prepare for my final please

 
Reply to this topicStart new topic

Help me prepare for my final please, 4 simple questions

ibaraku
16 Dec, 2007 - 02:42 AM
Post #1

D.I.C Head
Group Icon

Joined: 12 May, 2007
Posts: 164



Thanked: 1 times
My Contributions
As you know, finals are coming and I have a couple of questions, this are topics that I'm not to sure how to explain them if I saw them in an exam.

(a)Call by reference vs. value. What is the difference, why would you use one over another?
(b)Overloaded functions, what tells them apart and how do we use them.
©Automatic Variables and their scope. What happens with them as you enter and leave their scope.
(d)Linked Lists. What they are, why do we use them, how to place items correctly in a list.

Thanks again!!!
User is online!Profile CardPM
+Quote Post

Pontus
RE: Help Me Prepare For My Final Please
16 Dec, 2007 - 03:55 AM
Post #2

Dreaming Coder / Coding Dreamer
Group Icon

Joined: 28 Dec, 2006
Posts: 529



Thanked: 2 times
Dream Kudos: 275
My Contributions
Well, i'm going to give u the answer to b:

Overloaded functions are different functions that share the same name. Here is a good example:
CODE

int Sum(int a, int b);
double Sum(double a, double b);

Instead of making 2 functions with different names u can give it the same name. here u see how u use the functions
CODE

int main()
{
cout<<Sum(5,4)<<endl;
cout<<Sum(1.2,2.8)<<endl;
cin.get();
return 0;
}

The compiler automaticly chooses the right function. But, if they compiler cant tell the difference between 2 functions it will give an error.
Hope this helped...

This post has been edited by manhaeve5: 16 Dec, 2007 - 04:12 AM
User is offlineProfile CardPM
+Quote Post

kdbolt70
RE: Help Me Prepare For My Final Please
16 Dec, 2007 - 10:20 AM
Post #3

New D.I.C Head
*

Joined: 16 Dec, 2007
Posts: 19


My Contributions
for (a), passing by value actually creates a copy of the parameter in memory, and passes that copy to the function. This means that A) you are using more memory and time to copy, and B ) any modification to the passed in value will not reflect on the calling variable once the function goes out of scope. The argument passed by value is treated as a local variable, and will be destroyed when the function ends.

Passing by reference passes the actual argument, by means of its memory address, to the function. This means only an address is copied (usually the size of an integer) to the function. Any modification to this parameter will be reflected once the function ends, as it is the actual variable you are modifying.

value is beneficial when you are passing small data values (ints, bools, chars, etc), as it makes a copy of the parameter for modification, without any repercussions. Any sort of data structure should be passed by reference, because the cost of copying the entire thing makes it inefficient. Also, all arrays are passed by reference (for the same reason). You can also pass a const reference if you don't want the function to modify any of the values.


In terms of (d), google is probably a better help then I could in explaining everything about linked lists. Basically a linked list is a collection of nodes, each of which contain pointers to the next node in the list. Traversal is easy, as you just keep moving to the next node in the list. Things like binary search, reversals, etc, are a bit more complicated and often inefficient. Check this site or this DIC code snippet to enjoy all linked lists have to offer.

This post has been edited by kdbolt70: 16 Dec, 2007 - 10:30 AM
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 11:55PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month