Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,928 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,852 people online right now. Registration is fast and FREE... Join Now!




Function Design Question

2 Pages V  1 2 >  
Reply to this topicStart new topic

Function Design Question, Using a custom function

Delta_Echo
4 Jan, 2008 - 05:04 AM
Post #1

D.I.C Regular
***

Joined: 24 Oct, 2007
Posts: 480


My Contributions
Ok, i read a tutorial on creating a function, but how would i use it in another program?
Do i have to specify a directory where the functions source is?

Thanks
User is offlineProfile CardPM
+Quote Post

Jingle
RE: Function Design Question
4 Jan, 2008 - 05:14 AM
Post #2

D.I.C Regular
***

Joined: 20 Oct, 2007
Posts: 250


My Contributions
this is just to get you started.
CODE

#include <stdio.h>
void Hi()
{
  printf("this is a function\n");
}

int main()
{
    Hi();
// you can call Hi() when ever you want in main
    Hi();
    getchar();
    return 0;
}


also there is some good info on google you could look at

This post has been edited by Jingle: 4 Jan, 2008 - 05:16 AM
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Function Design Question
4 Jan, 2008 - 05:17 AM
Post #3

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,351



Thanked: 51 times
Dream Kudos: 25
My Contributions
Any function you create will be either in your main source file, or more properly, in another source file that is included within your project, much like the standard headers are used.
User is online!Profile CardPM
+Quote Post

Delta_Echo
RE: Function Design Question
4 Jan, 2008 - 05:22 AM
Post #4

D.I.C Regular
***

Joined: 24 Oct, 2007
Posts: 480


My Contributions
So, i make a function and name the source file "echo" and then type #include <echo>? Do i have to put it in a specific Directory?


User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Function Design Question
4 Jan, 2008 - 05:31 AM
Post #5

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,351



Thanked: 51 times
Dream Kudos: 25
My Contributions
Not really.

http://www.cplusplus.com/doc/tutorial/functions.html

http://www.cplusplus.com/doc/tutorial/functions2.html

your functions can be declared and defined in your main source file - or they can be defined in another header file, something like "functions.h", which would then be included in your source file. And yes, the compiler will need to be able to locate the file in which functions reside, so it will either have to be included directly in the project directory, or the path specified to the compiler manually.
User is online!Profile CardPM
+Quote Post

Jingle
RE: Function Design Question
4 Jan, 2008 - 05:36 AM
Post #6

D.I.C Regular
***

Joined: 20 Oct, 2007
Posts: 250


My Contributions
sorry i totoly misunderstood your question.


This post has been edited by Jingle: 4 Jan, 2008 - 05:39 AM
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Function Design Question
4 Jan, 2008 - 05:39 AM
Post #7

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,351



Thanked: 51 times
Dream Kudos: 25
My Contributions
QUOTE(Jingle @ 4 Jan, 2008 - 08:36 AM) *

sorry i totoly misunderstood your question.
you create header files like Hi.h
then in your .cpp file you #include "Hi.h"
then you can use the functions from Hi.h


CODE

// this is the sorce file
// HI.cpp
#include <stdio.h>
#include "Hi.h"

int main()
{
  Hi();
  getchar();
  return 0;
}

CODE

//this is Hi.h
void Hi()
}
  printf("HI");
}


Although it may be better to use one opening brace and one closing brace for the function itself. wink2.gif
User is online!Profile CardPM
+Quote Post

Delta_Echo
RE: Function Design Question
4 Jan, 2008 - 05:51 AM
Post #8

D.I.C Regular
***

Joined: 24 Oct, 2007
Posts: 480


My Contributions
is printf like cout ?
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Function Design Question
4 Jan, 2008 - 06:02 AM
Post #9

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,351



Thanked: 51 times
Dream Kudos: 25
My Contributions
printf() is a C function for outputting formatted text. It can be roughly equated to cout, although cout is actually an object of the ostream class.
User is online!Profile CardPM
+Quote Post

Delta_Echo
RE: Function Design Question
4 Jan, 2008 - 06:56 AM
Post #10

D.I.C Regular
***

Joined: 24 Oct, 2007
Posts: 480


My Contributions
Ok, i made a project in Dev-C++ to confirm i understand,
i made two source files in the project:
main.cpp
funH

main.cpp:

CODE

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

using namespace std;

int main()
{
    funH();
    cin.get();
}


funH:
CODE

void funH()
{
     cout << "Hello World 2.0\n";
}


and when i try to compile the source i get these errors:
CODE


2 C:\Documents and Settings\D3174\Desktop\main.cpp funH.h: No such file or directory.
   C:\Documents and Settings\D3174\Desktop\main.cpp In function `int main()':
8 C:\Documents and Settings\D3174\Desktop\main.cpp `funH' undeclared (first use this function)
  (Each undeclared identifier is reported only once for each function it appears in.)
C:\Documents and Settings\D3174\Desktop\Makefile.win [Build Error]  [main.o] Error 1


any Ideas?
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Function Design Question
4 Jan, 2008 - 07:03 AM
Post #11

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,351



Thanked: 51 times
Dream Kudos: 25
My Contributions
When using function in C or C++, you need to have the prototypes available so that the compiler is aware of their existence.

CODE

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

using namespace std;
void funH();

int main()
{
    funH();
    cin.get();
}

The links i provided above speak to the proper use of prototypes and definitions.

and what is the exact name of the funH file? It needs to be funh.h, because you are including a header file, not a cpp file.

As a final FYI, if one is splitting functions into files, the traditional method is to have the prototypes in a header file, and the function definitions themselves in a separate cpp file.
User is online!Profile CardPM
+Quote Post

Tom9729
RE: Function Design Question
4 Jan, 2008 - 11:02 AM
Post #12

Debian guru
Group Icon

Joined: 30 Dec, 2007
Posts: 1,589



Thanked: 12 times
Dream Kudos: 325
My Contributions
A proper example (in C, for C++ you would want to use ".cpp" files instead of ".c", and you would use "<iostream>" and "std::cout" instead of "puts".

foo.h, declares some functions, which will be defined in another file.
CODE

#ifndef _FOO_H // These make sure this header file is only included once.
#define _FOO_H

void foo(void);

#endif


foo.c, includes the above header file and defines the functions in it.
CODE

#include "foo.h"
#include <stdio.h>
#include <stdlib.h>

void foo(void)
{
    puts("Hello world!");
}



main.c
CODE

#include "foo.h"
#include <stdio.h>
#include <stdlib.h>

int main()
{
    foo(); // Call foo.

    return EXIT_SUCCESS;
}


If you already declared your functions/variables in a header file, and you've included that header file, then you don't have to declare them again in your source file(s).
User is online!Profile CardPM
+Quote Post

2 Pages V  1 2 >
Reply to this topicStart new topic
Time is now: 1/8/09 03:03PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month