What is function in C++?

11 replies
Please explain me with example.Thanks in advance.
#function
  • {{ DiscussionBoard.errors[4452085].message }}
  • Profile picture of the author taspocahon
    The return value is actually very simple, you have some sort of function as a function of the black box, call the function, it passed into the function equivalent to the required data, and then start the function by its function runs, the final output the results of a run, the output result is the return value. But different functions for different functions.
    {{ DiscussionBoard.errors[4452109].message }}
    • Profile picture of the author jambuzz
      A function is a group of statements that is executed when it is called from some point of the program. The following is its format:

      type name ( parameter1, parameter2, ...) { statements }

      where:
      type is the data type specifier of the data returned by the function.
      name is the identifier by which it will be possible to call the function.
      parameters (as many as needed): Each parameter consists of a data type specifier followed by an identifier, like any regular variable declaration (for example: int x) and which acts within the function as a regular local variable. They allow to pass arguments to the function when it is called. The different parameters are separated by commas.
      statements is the function's body. It is a block of statements surrounded by braces { }.
      {{ DiscussionBoard.errors[4452556].message }}
  • Profile picture of the author leppozdrav
    Ha ha... pretty theoretical tough. But, there are lot more books which can explain functions to you in a better way!
    I have a good knowledge on object oriented programming. I would suggest you to look things in a object oriented way rather than knowing "function in C++".
    Figure out what a function in a OO world. corolate it with C++. You will get to know the taste of the language!
    {{ DiscussionBoard.errors[4453104].message }}
    • Profile picture of the author Thrasher66099
      C++ is an object orient programming language. That means there are items known as objects. This can be anything from a character in a video game to a combo box in a business application. Just like an object in real life these objects can perform actions and have actions performed on them. These actions are known as functions. All a function really is, is a grouping of code that makes sense to together.

      Example
      My object is a combo box.

      Now if I want I can create a line of code that will listen for the user to send input (i.e. press a button on the keyboard). Then I can write a line of code that takes this input stores it in a variable in memory. I then take that variable in memory and do a calculation on that variable. The new calculation I place in the combo box.

      Ok as you can see that is a lot of code so rather than recoding that every time I place all of that in a function called TranslateInput(). Now whenever I want my program to translate the user input I just place TranslateInput() in my code rather than all of those separate lines of code.


      If you have any other C++, C#, VB.NET, OpenGL, or XNA questions feel free to pm me or post them here. (Can't pm you back quite yet. I need 4 more posts.)
      {{ DiscussionBoard.errors[4463358].message }}
    • Profile picture of the author janiepetter
      Originally Posted by leppozdrav View Post

      Ha ha... pretty theoretical tough. But, there are lot more books which can explain functions to you in a better way!
      I have a good knowledge on object oriented programming. I would suggest you to look things in a object oriented way rather than knowing "function in C++".
      Figure out what a function in a OO world. corolate it with C++. You will get to know the taste of the language!

      If you have a good knowledge then can you suggest me any good online tutorial for c++?
      {{ DiscussionBoard.errors[4476267].message }}
      • Profile picture of the author manimoor
        Hi..Using functions we can structure our programs in a more modular way, accessing all the potential that structured programming can offer to us in C++.

        A function is a group of statements that is executed when it is called from some point of the program. The following is its format:

        type name ( parameter1, parameter2, ...) { statements }

        where:
        • type is the data type specifier of the data returned by the function.
        • name is the identifier by which it will be possible to call the function.
        • parameters (as many as needed): Each parameter consists of a data type specifier followed by an identifier, like any regular variable declaration (for example: int x) and which acts within the function as a regular local variable. They allow to pass arguments to the function when it is called. The different parameters are separated by commas.
        • statements is the function's body. It is a block of statements surrounded by braces { }.

        Here you have the first function example:

        // function example #include <iostream> using namespace std; int addition (int a, int b) { int r; r=a+b; return (r); } int main () { int z; z = addition (5,3); cout << "The result is " << z; return 0; }

        output is 8.
        {{ DiscussionBoard.errors[4478286].message }}
      • Profile picture of the author Thrasher66099
        Originally Posted by janiepetter View Post

        If you have a good knowledge then can you suggest me any good online tutorial for c++?
        Teach Yourself C++ in 21 Days

        Read that entire ebook. After you've finished it you will understand all the key components of C++. From there you can start to learn more advanced ideas such as ai or graphics.
        {{ DiscussionBoard.errors[4478510].message }}
        • Profile picture of the author vvslaura
          Functions are building blocks of the programs. They make the programs more modular and easy to read and manage. All C++ programs must contain the function main( ). The execution of the program starts from the function

          main( ). A C++ program can contain any number of functions according to the needs.

          The general form of the function is:

          return_type function_name(parameter list)
          {
          body of the function

          }
          {{ DiscussionBoard.errors[4483938].message }}
  • Profile picture of the author odehfehr
    Features are the building blocks of the programmes. They make the programs more modular and easy to read and manage. All C++-programs must contain main () function. The implementation of the programme begins with the main () function. A C++ program can have any number of functions in accordance with the needs. The General form of the function is:-


    return_type function_name(parameter list)
    {
    body of the function

    }
    {{ DiscussionBoard.errors[4505820].message }}
  • Profile picture of the author Daret
    A function is a group of statements which perform some operation on data being passed to it and can also return the result of operation. A function can be called at any step in a program. Functions provide reusability and modularity.

    Function definition:
    return_type_of_function function_name(function parameters)
    {
    //function body
    return statement, if any
    }
    Signature
    {{ DiscussionBoard.errors[4580070].message }}

Trending Topics