Functions and Variables used by them

This is a discussion on Functions and Variables used by them within the C++ Programming forums, part of the General Programming Boards category; Hi there, I am just getting into this C++ programming, enjoying it as well. But from examples I have read ...

  1. #1
    Unregistered
    Guest

    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,

    simon

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    2

    Thumbs up Errors!!

    Hi,


    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
    Unregistered
    Guest

    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!


    Si

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

    here

    #include "iostream.h"

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

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

    void addem(int a, int b, int c) //function definition
    {
    c=a+b;
    }

    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
    Unregistered
    Guest

    Talking Thanks

    Thanks for the posting,makes sense now.

    Si
    "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, 07: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, 05:24 AM
  5. functions to return 2 variables?
    By tim in forum C Programming
    Replies: 5
    Last Post: 02-18-2002, 01:39 PM

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