Exforsys
+ Reply to Thread
Results 1 to 4 of 4

Variable Declaration

This is a discussion on Variable Declaration within the C and C++ forums, part of the Programming Talk category; I am confused with the declaration of variable passed as argument to a function.I tried a sample C program given ...

  1. #1
    ashlee is offline Senior Member Array
    Join Date
    Apr 2006
    Answers
    108

    Variable Declaration

    I am confused with the declaration of variable passed as argument to a function.I tried a sample C program given below:
    #include <stdio.h>
    main()
    {
    int a=500,c;
    c=sampel(a);
    printf("c=&#37;d",c);
    }
    sample(t)
    {
    int t;
    t=t+10;
    return(t);
    }

    The above program gave me error. Is it that I have to declare t before the opening braces of defining the function sample? What is the reason for this?


  2. #2
    Mukhtar Ahmad is offline Junior Member Array
    Join Date
    May 2007
    Answers
    12
    int sample(int t)
    {
    int t;
    t=t+10;
    return(t);
    }


    look at the "int sample(int t)" as compared to sample(t)


  3. #3
    sammy is offline Senior Member Array
    Join Date
    Apr 2006
    Answers
    145
    The error you would have got is Re-declaration of t in function sample. Am I right? The reason for this is you have not declared the variable t before the opening brace of function sample().So by default it is assumed as integer variable. But again after the opening brace you have declared again variable t which causes the compiler to throw the error message as re-declaration of
    variable t. Remove the declaration and try out and always it is best to declare variables before the opening braces of function calling.


  4. #4
    Manyamanas is offline Member Array
    Join Date
    Dec 2010
    Answers
    32
    the argument part in the function is the error...
    a function got 3 parts return type,name and arguments.....


    here the return type will be int,name is sample and argument is int t within brackets
    int sample(int t)
    {
    t=t+10;
    return(t);
    }


    •    Sponsored Ads



Latest Article

Network Security Risk Assessment and Measurement

Read More...