Thread: Help with functions

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    6

    Help with functions

    Hello!

    This is my first post at the forums. And also my first problem with C++. I've programmed in a quite simple programming language called GML, short for GameMakerLanguage. It's a language for a program called GameMaker hence the name GML. (www.gamemaker.nl)

    In GM you were able to make scripts and objects. Objects could execute these scripts, you could make your own functions using these scripts. There were events, a draw event used for drawing text and images, create events, mouse click events and many more. At each event, a piece of code or script could be executed.

    In the draw event for example, I was able to draw some text using a for loop.

    Code:
    for(i=0; i < 10; i+=1){
    draw_text(0,0 + (i*20), i)
    }
    The first two arguments were the x, y position on the screen, and the last the text to be drawn at that position. It's really easy.

    However, GameMaker's speed is far from that of C++. GM is a tool for beginner game makers, learn a bit about programming and used for easy mainly 2D game developing. I decided it was time to move on to a much more advanced programming language.

    So I was reading all of the lessons from 'C++ made easy' and got to the functions part. I figured it'd be a bit similar to GM's scripts loops, variables, conditional statements were all similar to GM. But I'm having problems with C++'s functions.

    I learned that a function in C++ execute some code and return a function. Like
    Code:
    return a * b
    But I was hoping to be able to do more. Like using a function to print various text using a loop.

    A bit like this:
    Code:
    for(i=0;i < Times; i++){cout << Number << " Times " << i " = " << i*Number;}
    Also, GM had arguments. You could write:
    Code:
    MultiplyScript(5, 2)
    And in the script MultiplyScript:
    Code:
    return (argument0 * argument1)
    And it would return 10.

    I hope I'm making sence, not making too many mistakes or breaking any rules. Sorry, I'm really new to C++ and these boards, bear with me!

    Thanks in advanced! I'm also looking for somebody who could teach me a few basics, get me started. The tuts are great, but sometimes don't cover every single thing. I'd love to have a buddy on preferably MSN who could help me with some problems.

    - Tarik Abbara

  2. #2
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    Quote Originally Posted by Abbara
    Also, GM had arguments. You could write:
    Code:
    MultiplyScript(5, 2)
    And in the script MultiplyScript:
    Code:
    return (argument0 * argument1)
    And it would return 10.
    You mean something like this?

    Code:
    int multiply(int x, int y) {
        return x * y;
    }
    This function would be called by writing e.g. multiply(5, 2) The argument names are given with their type in the definition of the subroutine. The return value's type is given to the left of the function name.

    Quote Originally Posted by Abbara
    I learned that a function in C++ execute some code and return a function.
    You mean "return a value," probably.

    Quote Originally Posted by Abbara
    Like
    Code:
    return a * b
    But I was hoping to be able to do more. Like using a function to print various text using a loop.

    A bit like this:
    Code:
    for(i=0;i < Times; i++){cout << Number << " Times " << i " = " << i*Number;}
    Something like this?

    Code:
    void some_multiples(int Number, int Times) {
        for (int i = 0; i < Times; i++) {
           cout << Number << " * " << i << " = " << (i * Number) << endl;
        }
        return;
    }
    (The 'void' type means that the function does not return a value.)

    HTH

  3. #3
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    A (very VERY basic)C++ Function:
    Code:
    #include <iostream>
    using namespace std;
    
    int MultiplyScript(int a, int b)  //a function that intakes 2 integers
    {
    return a*b;  //returning the value of a*b
    }
    // Now to execute this
    int main()
    {
    cout<<MultiplyScript(5,2)<<endl; //This prints out what Multiply Script returns(which is 5 * 2)
    system("PAUSE");
    return 0;
    }
    You can put many different things in functions, not just returning values. For example:
    Code:
    #include <iostream>
    using namespace std;
    void PrintHi()
    {
    cout<<"Hi\n";
    }
    int main()
    {
    PrintHi();
    system("PAUSE");
    return 0;
    }
    Hope that helps you out a bit.

    EDIT: Beaten
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  4. #4
    Registered User
    Join Date
    Oct 2005
    Posts
    6
    void! That certainly sounds promising. I've tried a few things, but I'm still doing some stuff wrong.

    Code:
    #include <iostream.h>
    
    using namespace std;
    
    
    //char NumberMultipy(int Number, int Times);
    void NumberMultiply( int Number, int Times){
    for(int i=0;i < Times; i++){
              cout << Number << " Times " << i << " = " << i*Number << endl;;
              }
              return;
    }
    
    int main() {
        int Number;
        int Times;
        
      cout << "Please enter the number you want to multiply. \nAnd the times you want to multiply it." ;
      cin >> Number >> Times;
      cin.ignore;
      NumberMultiply( Number, Times);
      cin.get();
    }
    Compiler: Default compiler
    Building Makefile: "D:\Dev-Cpp\Makefile.win"
    Executing make...
    make.exe -f "D:\Dev-Cpp\Makefile.win" all
    g++.exe -c FunctionTest.cpp -o FunctionTest.o -I"lib/gcc/mingw32/3.4.2/include" -I"include/c++/3.4.2/backward" -I"include/c++/3.4.2/mingw32" -I"include/c++/3.4.2" -I"include"

    In file included from C:/Dev-Cpp/Bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/backward/iostream.h:31,
    from FunctionTest.cpp:1:

    C:/Dev-Cpp/Bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/backward/backward_warning.h:32:2: warning: #warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the <X> header for the <X.h> header for C++ includes, or <iostream> instead of the deprecated header <iostream.h>. To disable this warning use -Wno-deprecated.
    FunctionTest.cpp: In function `int main()':
    FunctionTest.cpp:20: error: statement cannot resolve address of overloaded function

    make.exe: *** [FunctionTest.o] Error 1

    Execution terminated
    Also, in GM the scripts were seperated. Right now I'm writing every code in one... Document. Would it be possible to say put a certain function like one to play a sound in a seperate document that can be called? Like if you're too lazy to type say
    cout << "Text"
    you'd be able to write
    message("Text")

    And have another document called message and have the code
    cout << argument0

    or something alone those lines...

    Either way, thanks alot! You guys rock

  5. #5
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    Code:
    #include <iostream>   //You had iostream.h, it's just iostream because it's a C++ standard header
    
    using namespace std;
    
    void NumberMultiply( int Number, int Times)
    {
              for(int i=0;i <= Times; i++)   //changed i<Times so it would include the number they put in, in the table
              {
              cout << Number << " Times " << i << " = " << i*Number << endl;
              }
    }
    
    int main() 
    {
      int Number;
      int Times;
        
      cout << "Please enter the number you want to multiply. \nAnd the times you want to multiply it." ;
      cin >> Number >> Times;            //Don't need a cin.ignore() unless you are using the cin.getline() function
      NumberMultiply( Number, Times);  //Run your function
      system("PAUSE");   //Pause to read, cin.get() works too
      return 0;  //End program
    }
    when a function is declared as a "void" function it means it will NOT return any value. If it is declared as an "int" function it means it will return an integer value, and so on.
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  6. #6
    Registered User
    Join Date
    Oct 2005
    Posts
    6
    That works! Thanks everybody.

    EDIT --------------------------------------
    Still have one more question before I move onto pointers. (Eep! Don't really get the purpose of a pointer.)

    Can we seperate pieces of code? Right now when I start a project > console application in Dev-C++ I get one long page. I'm okay with this for now. I'm just writing little super simple to you guys code to learn how to use C++. But in the future if I'd want to make a small application or game or whatever then it would be annoying to have 4 pages of functions, and then the main part where all those functions are called. Would it be possible to have say a seperate document. One for declaring functions, and one for the main part. Or even better, one for each function I want to declare...
    Last edited by Abbara; 10-16-2005 at 03:11 AM.

  7. #7
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    Yes, they are called header files. I believe there is a tutorial on this site about that. Then after you have made your header file you include it into your main file like this:
    Code:
    #include "MyHeader.h"
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  8. #8
    Registered User
    Join Date
    Oct 2005
    Posts
    6
    Hmm, can't get it right. I made a new source file, pasted a function in it and saved it as a .cpp file. Then I saved it as a .h file as well.
    Then in my main project I included it, but it says 'no such file or directory'.

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Quote Originally Posted by Abbara
    Hmm, can't get it right. I made a new source file, pasted a function in it and saved it as a .cpp file. Then I saved it as a .h file as well.
    Then in my main project I included it, but it says 'no such file or directory'.
    Look down the forum for the current thread titled "making header files", and see if that helps.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Void Functions Help
    By bethanne41 in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2005, 05:30 PM
  2. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  3. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 03:35 AM
  4. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM
  5. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 05:39 AM