?
Solved

Procedures and Functions

Posted on 1998-11-11
10
Medium Priority
?
231 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
10 Comments
 
LVL 3

Accepted Solution

by:
stefanr earned 80 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
Suggested Courses

752 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