Thread: Functions and Variables used by them

  1. #1

    Unhappy Functions and Variables used by them

    Hi there,

    I am just getting into this C++ programming, enjoying it as well. But from examples I have read on functions I am slightly confused

    Example code

    #include <iostream>

    addem(int a, int b, int c)
    c= a+b;

    int main()
    int a, b, c;
    addem(int a, b, c);
    std::cout<<"Lets add a and b, and we get"<<c;

    Question time now! In the function defination do I have to put int after each variable name, These variables are what is going to be passed to the function right? And do they have to be different or do they HAVE to be the same name as the name of the variables being passed to them. finally have I called the function correctly from the MAIN function.

    thanks for your time,


  2. #2
    Registered User
    Join Date
    Dec 2001

    Thumbs up Errors!!


    Let me list out the errors:

    * You never call a function in the main as
    adden(int a, b,c);
    I mean, you do not redeclare the variable a

    * You have passed the arguments correctly but may not get the expected result (see pass by values & pass by reference!)

  3. #3

    Unhappy update

    Right, that makes sense about declaring it twice. I called it wrong from the main as it did declare it twice! thanks for pointing that out. Its all a learning game!

    Am I now right in thinking that I HAVE to tell the function what each variable it is going to be passed is like this:

    addem(int a, int b, int c)

    Or can I do the function definition as:

    addem(int a, b, c)

    Also though my main question is does the name of the variables in the function definition HAVE to be the same as the variables being passed from the MAIN()

    What confuses me over this is if they are the same, arent you re initialising the varible in the function def, or does c++ not treat it like that.
    Sorry if I hav enot explained it correctly!


  4. #4
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001


    #include "iostream.h"

    void addem(int, int, int); //function prototype

    int main()
    int num1, num2, num3;
    cout<<"Enter first number: ";
    cout<<"\nEnter second number: ";
    cout<<"The sum is: "<<num3;
    return 0;

    void addem(int a, int b, int c) //function definition

    Ok, you put in only the type of variables for the prototype. For the definition you declare variables for the function, they only exist in that function. when you call the function, you need not to declare anything, they are already declared. the values of a,b,and c are, respectively, passed to num1, num2, and num3. Hope this helps.

  5. #5

    Talking Thanks

    Thanks for the posting,makes sense now.

    "Its a learning game!"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Variables and Functions
    By Hitetsu in forum C++ Programming
    Replies: 2
    Last Post: 03-29-2006, 08:01 AM
  2. Question about Static variables and functions.
    By RealityFusion in forum C++ Programming
    Replies: 2
    Last Post: 10-14-2005, 02:31 PM
  3. Static variables and functions problem in a class
    By earth_angel in forum C++ Programming
    Replies: 16
    Last Post: 09-15-2005, 12:08 PM
  4. Variables do not equal functions!!!
    By me@burk. in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 06:24 AM
  5. functions to return 2 variables?
    By tim in forum C Programming
    Replies: 5
    Last Post: 02-18-2002, 02:39 PM