Solved

Procedures and Functions

Posted on 1998-11-11
10
225 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
>>   "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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 1

Author Comment

by:yaelie
Comment Utility
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
Comment Utility
oki oki nm i got it...thanx for all your help :)
0
 
LVL 3

Expert Comment

by:stefanr
Comment Utility
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
Comment Utility
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
Comment Utility
Thanking you :) you were right...i didnt declare them ahead of time
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

771 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

12 Experts available now in Live!

Get 1:1 Help Now