im brushing over some of my notes for a midterm tommorow.

questions i have when i was reading:

what exactly does a parameter do?
i know it passes a value to a function-but how is it always like that? or is it more complicated than that? is it kind of like passing a power in math?

for the functions should the declarations go in the very top-or in its respective function?
like:

double funct1(); //function protoype, is double ok to use? since i cant use void? (need //return)
#include <iostream>
using namespace std;
int main()
{

double funct1();

return 0;
}

//function
double funct1()
{
int x = 1, y = 2; //declare here or at top in main? note: no GLOBAL const allowed!
cout << "Answer: " << x + y << endl;
return ? //<---dont know what to return here. 
}

what does it mean to pass by value or pass by ref? wats the diff?

wat are the basics of an array?
2d array?

and wat is a structure?

guys-im reading my book but i feel like im reading something foreign! :(
it doesnt make sense for me! help me in simple english please!!

ur help will be GREATLY appreciated!!
(ok-back to studying :D

function prototypes normally are placed after all includes and before the first function.

There are two ways (in C) and three ways in c++ to pass parameters.

  • pass by value -- the object is just a copy of the value in the calling function.
  • pass by reference -- in c++ this is the actual object declared in the calling function
  • pass by pointer -- this is a pointer to an object declared in the calling function

Pass By Value -- only a copy of the variable is passed to the function. Any changes made to that variable by the function are not visible to the calling function.

Pass By Reference/Pointer -- The function receives a pointer or reference to the object and any changes made by the function are also made in the calling function.

To clarify these concepts for yourself you should write small programs that demonstrate the use of those parameter types. Testing on your computer for yourself is worth thousands of words in a textbook or online at a website.

--what exactly does a parameter do?
i know it passes a value to a function-but how is it always like that? or is it more complicated than that? is it kind of like passing a power in math?

Im not sure what you mean by 'is it always like that'. A parameter (properly called an argument I believe) is the interface between your code and your function. That, and the return code, are used to pass data from your program to your functions (via a copy or a reference to an address).

for the functions should the declarations go in the very top-or in its respective function?
like:

No. Keep your #includes at the top. It might compile if you declare them at the top, but it's good practice not to.

double funct1(); //function protoype, is double ok to use? since i cant use void? (need //return)
#include <iostream>
using namespace std;
int main()
{

double funct1();

return 0;
}

//function
double funct1()
{
int x = 1, y = 2; //declare here or at top in main? note: no GLOBAL const allowed!
cout << "Answer: " << x + y << endl;
return ? //<---dont know what to return here. 
}

Void is allowed for a function return type. In your main() you are incorrectly using the function. It should be more like double dValue = funct1() . You function should be returning something - most probably the result of x + y (although it should really just be returning an int). Unless there's a good reason, you should usually keep your output to main() or a designated outpt function. So that addition function (made more useful) should just be:

//prototype:
int funct1(int, int)
//actual function:
int funct1(int x, int y)
{
     return x + y;
}
//called in main()
int main()
{
    int iSum = funct1(10, 15);
    cout << iSum;
    //outputs 25
    return 0;
}

what does it mean to pass by value or pass by ref? wats the diff?
Passing by reference passes the address of the variable to the function. This can be pretty beneficial at a basic level in that it can alter the values since it's directly altering their memory location, hence returning more than 1 value (the return code). Passing by value creates a temporary copy of that variable in memory, and even if modified will not affect the variable outside the scope of the function. Typically it is best to pass everything by reference unless you have a good reason not to. (It's faster since no copy needs to be made. If you don't want to accidentally modify the reference, you can always make it const)

wat are the basics of an array?
2d array?

An array is a indexed list of any data type that is accessed by an index in between the [] operator. A 2d array is basically a matrix of values, accessed by 2 indexes between [] operators.

ie...

int iArray[10];
int i2dArray[10][10];
for (int i(0); i < 10; ++i)
   iArray[i] = 5;     //this will iterate every value in the array and assign it to 5
for (int i(0); i < 10; ++i)
   for(int ii(0); ii < 10; ++ii)
      i2dArray[ii][i] = 10; //this will iterate every value in the 2d array and assign it to 10

and wat is a structure?
A structure is a user defined data type that can contain other data types and functions. Here's an example:

#include <iostream>
#include <string.h>
using namespace std;

struct sStudents
{
   string sName;
   int      iAverage;
};

int main()
{
    sStudents A[5];   //make an array of 5 students
    for (int i(0); i < 5; ++i)
    {
        cout << "Enter student name: ";
        cin >> A[i].sName;
        cout << "Enter students average: ";
        cin >> A[i].iAverage;
    }
    cout << "\n\nName\t\tAverage\n--------------------";
    for (int i(0); i < 5; ++i)
        cout << endl << A[i].sName << "\t\t" << A[i].iAverage;
    return 0;
}

This will create a struct calles sStudents that contains a students name and their average. It will then prompt you to input each students name and average, then will display them.

Someone correct me if I'm wrong please.

As of array, when you first declare array, you are allocating a block of memory and what you get is a pointer( address of a memory) that indicates where that block of memory starts. And from then on, evertime you access the array, you start from the address pointed by the pointer (called base address) and as you go up the index of array you calculate address + index*numberofbytes. (char = 1 byte, int = 4 bytes, so forth).

When you are creating 2d array, you are first creating a regular array. But inside the array, the contents aren't ints or chars, but rather addresses that points to other blocks of memory that contains the actual values.

Does that make sense to you? I hope that helped..

thanks guys!
this was really helpful!
esp skatamatic! thank you so much! :)
i actually understood it!!

now im gonna go and practice some of this stuff!! :)

When you are creating 2d array, you are first creating a regular array. But inside the array, the contents aren't ints or chars, but rather addresses that points to other blocks of memory that contains the actual values.

If you declare a 2d array like this: int array[2][4]; the compiler creates one continuous block of memory that consists of 2 * 4 integers. The indices of the array are not addresses but, like 1d array, are actual integers. When you reference a single element of the array, say array[i][j] the compiler does the math for you to determine the exact element. Most likely it will calculate (array + (i * 4) + j );

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.