What is function in C++?

Discussion in 'Programming' started by janiepetter, Aug 12, 2011.

  1. #1
    I want to know what is function in details ?please explain me with example.
     
    janiepetter, Aug 12, 2011 IP
  2. lumpy

    lumpy Well-Known Member

    Messages:
    942
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    160
    #2
    function sum(a,b)
    {
    sum=a+b;
    }

    When you type sum(2,3), the function will return 5. When you type sum(10,5) the function will return 50.
    Functions help you minimize the work that you do. For example if you want to read an array, you can read it as usual. But what if you have to read 100 arrays? You write a function and call it for every array.
     
    lumpy, Aug 12, 2011 IP
  3. Indianrockz

    Indianrockz Greenhorn

    Messages:
    79
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    20
    #3
    Basically function is not unique to C++. It is derived from C Language.

    Function: A piece of code (eg: to add 2 numbers, c = a + b;) which is defined as a block with a unique name for the block which can be used to call it multiple times for different values whenever required.

    Functions in C++ are of 2 types.

    1) Pre defined functions (Library functions)
    2) User defined Functions

    Pre Defined Functions:

    I hope you are aware of functions like clrscr(), getch(), getchar(), etc. These functions are already created and their definitions are stored into the headerfiles like conio.h, stdio.h, string.h. So you can re use it whenever you need it.

    User Defined Functions:

    These are functions that programmer writes for the usage in that particular program. While defining a new function, user has to declare it, define it and then call it whenever required.

    Function Declaration:

    It is just to inform the compiler that a function is written in the program with that function name which will receive the so and so arguments, will pass that type of data to the calling program.

    Returntype Function_name (datatype 1, datatype2,.......);

    Returntype ==> Valid C++ Data type
    Function_name ==> Name given by the user to that function
    datatype 1, datatype 2 ==> Argument list (A valid C++ data type)

    Eg: int sum (int, int);

    Note the semicolon at the end.

    Function Definition:

    In the function definition part, we will mention the actual process that has to be done in that function. (Compare with Clrscr, its function definition will contain code to clear the output screen)

    Returntype Function_name (datatype1 variable1, datatype2 variable2,.......)

    Returntype ==> Valid C++ Data type
    Function_name ==> Name given by the user to that function
    datatype 1, datatype 2 ==> Argument list (A valid C++ data type)
    variable1, variable2 ==> A valid C++ variable name (identifier)

    Eg: int sum (int x, int y)

    Note the semicolon is not there at the end.

    Function Call:

    This the part to use the function.

    Eg: C = sum(2,3);

    I hope you can understand the details mentioned. Incase you need any clarification, feel free to PM me.
     
    Indianrockz, Aug 13, 2011 IP
    p.caspian likes this.
  4. p.caspian

    p.caspian Peon

    Messages:
    964
    Likes Received:
    6
    Best Answers:
    1
    Trophy Points:
    0
    #4
    Thanks Bro, I was expecting this type of answers. Very Good Job. :eek:
     
    p.caspian, Aug 13, 2011 IP
  5. mmerlinn

    mmerlinn Prominent Member

    Messages:
    3,197
    Likes Received:
    819
    Best Answers:
    7
    Trophy Points:
    320
    #5
    A function (also called a procedure, subroutine, etc) is a place you go to do something then return to where you came from. Often, but not always, a function will either modify something or bring something back to where you came from.

    For example, a shopping trip is a function. You leave home, go to the store, and return home with something you bought.

    A second example is going to a cafe. You leave home, go to the cafe, eat, then return home with nothing, except maybe a stomach ache and an empty wallet.

    Functions in any computer language work the same way. You leave your current code, go to the function, do something, then maybe bring something back to the your original location in your current code.
     
    mmerlinn, Aug 13, 2011 IP
  6. honeybansal

    honeybansal Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    functions: functions are to be used in large programs where a block of statements are used again and again. function reduces the complexity of the program.
    main() is also a function from which execution begins.
    and other functions like
    1. no return value and no arguments
    void sum()
    {
    int a,b,sum;
    sum=a+b;
    cout<<sum;
    }
    2.return value but no arguments
    int sum()
    {
    int a,b,sum;
    sum=s+b;
    return(sum);
    }
    3.no return value but pass arguments
    void sum(int a,int b)
    {
    sum=a+b;
    cout<<sum;
    }
    4.return value and return arguments
    int sum(int a,int b)
    {
    sum=a+b;
    return(sum);
    }
     
    honeybansal, Aug 14, 2011 IP
  7. Vinil Mehta

    Vinil Mehta Member

    Messages:
    190
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    48
    #7
    functions are a part of code that perform some task or some job in order to fulfill the requirements.:cool:
     
    Vinil Mehta, Aug 16, 2011 IP
  8. janiepetter

    janiepetter Peon

    Messages:
    184
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Thank you for your wonderful help.Can you suggest me any good online tutorial for c++?
     
    janiepetter, Aug 16, 2011 IP
  9. Vinil Mehta

    Vinil Mehta Member

    Messages:
    190
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    48
    #9
    Vinil Mehta, Aug 16, 2011 IP