Im learning ++ by myself and have decided to follow this tutorial and very interesting
http://www.cprogramming.com/tutorial/lesson4.html

However as you can see the int mult ( int x, int y ); is defined twice: at the top and the bottom. Whay it is that. Also I have found many programs uses to put their main blocks in header files, is it possible to that with this function.

I have python b-ground and I learned C long ago in class and all I remember is few of printf and scanf and fading memories of other stuffs

Thanks all

Recommended Answers

All 7 Replies

int myfunc(int, int);  //function prototype

int main()
{
   myfunc(6, 6);
}

int myfunc(int x, int y)  //function definition
{
   if(x == y)
     cout << "Yeah";
   else 
     cout << "Boo";
}

The function prototype is almost the same as the first line of the function definition. The differences are the prototype only needs the parameter types, not the parameter names and there is a semicolon at the end of the prototype, but not at the end of the first line of the definition. You don't need a prototype if the function is defined (usually before main()) before it is called.

In terms of the main() block, I can't help much.

Thanks for that great help. I understand now better.
What about header and source code relation? (those .cpp and .h files in projects)

I mean suppose I want to shift functio to a header file and just use it in cpp file, what are procedures. Sorry for jumping in far topic, but I'm interested to have a clue

When doing this, the method is normally to have aheader file containing the function prototype. A Cpp file containing the function declaration and the a main cpp file that contains your int main(int argc, char *argv[]){} function, etc etc.

//example.h
#ifndef EXAMPLE_H_
#define EXAMPLE_H_

int myfunc(int, int);

#endif
//example.cpp

#include "example.h"

int myfunc(int x, int y){
  return (x*y)-(x+y);
}
//main.cpp

#include <iostream>
#include "example.h"

int main(int argc, char *argv[]){
  std::cout << myfunc(12, 2);
  return 0;
}

Chris

Thanks Chris,
Last question on this. What is difference between #include <example.h> and #include "example.h"
Thanks alot

<> state the file is located in the include directories.

"" states it may be located in the same location as the main source file.

(something like that) You need all the files in the same Project / Tell your compiler to compiler all 3 together :)

Chris

Thanks Chris,
Have good time :)

Thanks all who said anything too :)
The case is closed

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.