Procedures and Functions

Hi,

I'm a pascal programmer trying to learn C++.
How do you do procedures and functions?!
LVL 1
yaelieAsked:
Who is Participating?
 
stefanrConnect With a Mentor Commented:
As far as I remember Pascal, the difference between procedures and functions is that a procedure doesn't have a return value, but functions have. In C++ there is no such concept as a "procedure", but a function can have no return value, a 'void'.

So, a "procedure" in C++ would look like this:

void Procedure(/*optional arguments*/)
{
   return; // Optional.
}

and a function returning an int:

int IntFunc(/*optional arguments*/
{
   int n = 0;

   . . .
   return n; // _NOT_ optional.
}

In Pascal there is also the concept of parameter 'passing by value' and 'passing by reference', the VAR keyword. In C++, VAR corresponds to the '&' character following the argument type, like this:

void Procedure(int& nByReference, int nByValue)
{
}

Remember that unlike Pascal, case is important. The names PROCEDURE and Procedure is _NOT_ the same identifier.
0
 
nietodCommented:
Another important differences is that in C++ a function returns its value using the return statement, like

int AddTwo(int i)
{
    return i + 2;
}

in Pascal, a value is returned, but assigning that value to a "variable" that has the same name as the function.   Also note that in C++ a return can occur anywhere (even multiple places) in a function.  In pascal, a function had to "end" at the end, but in C++ you can end it in the middle, like

char * LookForA(char *StrPtr)
{
    while (true)
   {
       if (*StrPtr == 'A')
         return StrPtr;
      ++StrPtr;
   }
}
0
 
yaelieAuthor Commented:
hi...I'm sorry its taken me so long to respond to this I didnt have access to the computer for a couple days...

I tried what you said but i keep on getting a msg saying

"function has no prototype" ...what does this mean?
whats missing?
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
nietodCommented:
>>   "function has no prototype" ...what does this mean?
It probably means you tried to use a function before it was declared.  In C, just like in Pascal, you must declare a function before you can use it.  So the following will give an error.

void A()
{
   B(); // ERROR, B() is not yet declared.
}

void B()
{
}

but either of the following will be fine

void B();  // Declare a forward prototype of B.
void A()
{
   B(); // Fine B() is declared
}

void B()
{
}


OR

void B()
{
}

void A()
{
   B(); // Fine B() is declared
}



0
 
stefanrCommented:
Normally you place the forward declarations such as:

void B();  // Declare a forward prototype of B.

above in a special header file (extension .h). Then any .cpp file that calls the function uses the statement

#include "filename.h" // Replace with the name of your header file.

at the beginning of the file. Even the .h files themselves can #include other header files like this. Normally you include some standard files using the

#include <iostream.h>

syntax. That means that the compiler looks for the header file in only those directories declared in the INCLUDE environment variable (that is a somewhat simplified description).

If you use the

#include "iostream.h"

syntax, the compiler will search for the include file in thecurrent directory, and if not found, in the INCLUDE path as well.
0
 
yaelieAuthor Commented:
let me show you what ive done and maybe that will help...

#include <iostream.h>


void Hi()
{            <============ GET A NO FUNCTION PROTOTYPE MSG
cout<<"Hello"<<endl;
}


void Ask()
{       <============ GET A NO FUNCTION PROTOTYPE MSG
char name;
cout<<"What is the first letter in your name?"<<endl;
cin>>name;
cout<<"Hello"<< name<<endl;
}


main()
{
Hi();
Ask();
return 0;
}

I marked the lines that its pointing to when saying no functoin prototype..
0
 
yaelieAuthor Commented:
oki oki nm i got it...thanx for all your help :)
0
 
stefanrCommented:
You have to declare the function prototypes like this:

#include <iostream.h>

// These two lines below is normally in a header file that you includes here.
void Hi(); // This is a function prototype.
void Ask(); // This is also a function prototype.

void Hi()
{
   // This is your function definition.
   . . .
}


void Ask()
{
   // This is your function definition.
   . . .
}
 . .
0
 
nietodCommented:
yaelie, in the last example you posted, the code should have been fine.  You shouldn't have gotten any error messages.  You do not need forward function prototypes for that code.
0
 
yaelieAuthor Commented:
Thanking you :) you were right...i didnt declare them ahead of time
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.