Solved

Procedures and Functions

Posted on 1998-11-11
10
226 Views
Last Modified: 2010-04-02
Hi,

I'm a pascal programmer trying to learn C++.
How do you do procedures and functions?!
0
Comment
Question by:yaelie
  • 4
  • 3
  • 3
10 Comments
 
LVL 3

Accepted Solution

by:
stefanr earned 20 total points
ID: 1177660
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
 
LVL 22

Expert Comment

by:nietod
ID: 1177661
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
 
LVL 1

Author Comment

by:yaelie
ID: 1177662
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
 
LVL 22

Expert Comment

by:nietod
ID: 1177663
>>   "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
 
LVL 3

Expert Comment

by:stefanr
ID: 1177664
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Author Comment

by:yaelie
ID: 1177665
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
 
LVL 1

Author Comment

by:yaelie
ID: 1177666
oki oki nm i got it...thanx for all your help :)
0
 
LVL 3

Expert Comment

by:stefanr
ID: 1177667
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
 
LVL 22

Expert Comment

by:nietod
ID: 1177668
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
 
LVL 1

Author Comment

by:yaelie
ID: 1177669
Thanking you :) you were right...i didnt declare them ahead of time
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now