Function calls

This is a discussion on Function calls within the C++ Programming forums, part of the General Programming Boards category; The book I use to learn C++, practically explains nothing about it! I don't want to make sections in my ...

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    367

    Function calls

    The book I use to learn C++, practically explains nothing about it!

    I don't want to make sections in my codes, which I have to call by using the "goto"-statement.

    Please explain for me, once and for all, about function calls.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    A function is a module in a program that structures the program and breaks it up into logical sections. Take this long main function for an example. (I know it's not long, bear with me).
    Code:
    #include <iostream>
    #include <cmath>
    
    int main ( void )
    {
      int n = 1, input;
      std::cout<<"The number is "<< n <<"\n";
      // Ask for the user to enter a number
      // and add that number to n
      std::cout<<"Enter a number to add\n";
      std::cin>>input;
      n += input;
      std::cout<<"The number is "<< n <<"\n";
      // Calculate n to the user selected power
      std::cout<<"Enter a power to raise n to\n";
      std::cin>>input;
      n = pow ( n, input );
      std::cout<<"The number is "<< n <<"\n";
      return 0;
    }
    That's kind of ugly and we can do better by taking the operations and breaking them up into logical parts.
    Code:
    #include <iostream>
    #include <cmath>
    
    void printResult ( int x )
    {
      std::cout<<"The number is "<< x <<"\n";
    }
    
    int getInput ( void )
    {
      int input;
      std::cout<<"Enter a number\n";
      std::cin>>input;
      return input;
    }
    
    void add ( int& x )
    {
      x += getInput();
    }
    
    void raise ( int& x )
    {
      int i = getInput();
      x = pow ( x, i );
    }
    
    int main ( void )
    {
      int n = 1;
      printResult ( n );
      add ( n );
      printResult ( n );
      raise ( n );
      printResult ( n );
      return 0;
    }
    That's much easier to follow and we don't need the comments anymore because the function names perform that duty. Anytime you feel you need to use goto, you can easily add a function because they both do something similar, go there and execute that code. The difference between functions and goto is that goto doesn't come back. A function will return to the same line that you called it, thus allowing you to execute code after the call without setting another label and calling goto again.

    I could go on and on about functions, so you might want to be more specific in what you don't understand.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    first of all I suggest getting a different book,

    second: calling a function is simply: execution of your program jumps to a block of code, enclosed in braces {}, that begins with a desired function Name
    and thus when such function finishes it returnes to the next line that followes after the original call to that function was made...

    for example:

    void SomeFunction()
    {

    // blah blah

    }


    int main()
    {

    SomeFunction();

    return 0;
    }


    did i make it any clearer???


    if you ever get into dynamic memory management and stuff like that you will learn that function calling in reality is executed differently.......however for now this idealogy is fine

    Regards,
    matheo917

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Accurately detecting function calls
    By Mole42 in forum C Programming
    Replies: 5
    Last Post: 05-17-2009, 05:01 AM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 09:18 PM
  3. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  5. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 06:45 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21