Thread: Parameter pass

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    207

    Question Parameter passing

    Hi, I guess this question is quite simple for you guys, but please answer it

    My teacher says that the program I wrote is quite simple, that I'm missing a few things, like "parameter pass". But I don't even know what this is?

    Any simple definition is welcome

    Muchas gracias




    LAST NOTE: THE ACTUAL CODE (NOT FINISHED) IS IN THE LAST POST OF THE SECOND PAGE
    Last edited by Gades; 11-18-2001 at 08:06 AM.

  2. #2
    Sayeh
    Guest

    Parameter Pass

    "Parameter pass"-- perhaps they mean "parameter passing"?

    If os, there are only 2 ways--

    -- pass by address, or
    -- pass by value.

    If you pass a parameter by address, this would be like passing the address of a variable (or something else), or a pointer as an argument to a function. This is also called 'passing by reference".

    prototype
    -------------
    void foo(char *);

    call
    ------------------
    foo(&mychar); /* pass address of mychar */
    foo(myCharP); /* pass a pointer */

    For example, if I pass someone the location of my house, I don't pass them my house, I pass them the street address to my housel.


    prototype
    -------------
    void foo(char);

    call
    -------------
    foo(mychar); /* pass the character */

    For example, if I pass someone the location of my house, I'm passing them the whole house, not the address where it resides. Another way to look at it is that I'm passing the "contents", rather than the "address" of item 'x'. Whatever 'x' is.

    enjoy.

  3. #3
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    You should learn to use functions. A function is called from main and it can be passed arguements. The function requires a definition and should have a prototype.

    Code:
    //return type -  function name  -  parameters
    void MyFunction(int,double);  //prototype
    
    
    int main()
    {
          int num;
          double price;
        
          scanf("%d %lf", &num, &price);
    
          //num and price are arguements to the function call
          MyFunction(num,price); //function call
          return 0;
    }
    
    //function definition
    void MyFunction(int num, double price)
    {
        //num and price are the function parameters
          //calculate charge
          double charge = num * price;
          printf("Charge is: ",charge);
    }
    Although just a useless function, this is a demonstration of the syntax. This function passes the arguements num and price by value, therefore copies of these arguements are created an placed in the function parameters of the same name.

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    207
    OK, I'll try to use my functions the way you guys say.

    I'll post the code just the way it is now, I don't know if it's working because I modify too many things all the time, but I guess you'll have an idea of what's going on.






    Note: The updated code is down the page
    Last edited by Gades; 11-06-2001 at 03:56 PM.

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    207

    Question

    The ones who have seen the code, do you have any suggestion or comment?

    thanks

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > that I'm missing a few things, like "parameter pass"
    Yeah - all YOUR functions are of the form
    void foo ( void );
    ie, no parameters, no result returned.

    The only way for this code to work is if you have a whole mess of global variables (which you have).

    In fact, only a couple of your functions have any local variables at all.

    For instance, target can be declared local to
    diametros_cont_DY
    diametros_cont_CONV
    diametros_link_CONV

    And 'i' is a loop variable, which should also be local to wherever it is used.

    As for passing parameters, that would require more study.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Aug 2001
    Posts
    207
    Sir Salem,

    you've done more than my teacher in the whole course.

    Thanks a lot.

    Well, today now I'm going to change the code as much as I can and I'll post it again.


    And thanks to you guys as well.

  8. #8
    Registered User
    Join Date
    Aug 2001
    Posts
    207
    I have made just a couple of changes, enough to be able to compile the code. In case any one wants to compile it.

    The file Annika1.txt can be used as an existing project, but it has to be rename to Annika1 with no extension.

  9. #9
    Registered User
    Join Date
    Aug 2001
    Posts
    207
    I couldn't attach both files at the same time.

  10. #10
    Registered User
    Join Date
    Aug 2001
    Posts
    207
    >Yeah - all YOUR functions are of the form
    >void foo ( void );
    >ie, no parameters, no result returned.

    I don't really understand this. For example, the function "project_1" ask you for the new values for a new project. You enter all the values that are asked, and then the program calls again the function "project", where we came from.

    And I know that all the variables have now new values, the ones the user just entered.

    Or are you saying something else?

    And again, thanks for your help.

  11. #11
    Former Member
    Join Date
    Oct 2001
    Posts
    955
    well, first, let me ask you this:

    you have

    for (i = 0; DYload[i] < target; ++i);
    V1_load_lk_load_DY = DYload[i];
    V1_load_lk_diameter_DY = DYdiameter[i];

    is this what you want? or do you want

    for (i = 0; DYload[i] < target; ++i)
    {
    V1_load_lk_load_DY = DYload[i];
    V1_load_lk_diameter_DY = DYdiameter[i];
    }

    or don't you know what the second one means?

    what I do to explain functions is say that a function is some kind on "little black box" which, when you give it some values, it does something and returns another value which is the result of processing these values.
    an example of a function you can make would be

    int suma(int a, int b)
    {
    int c;
    c=a+b;
    return c;
    }

    now, let's take a look at the code:

    the first thing (int), defines the type of value you want to return as a process of the entrance values

    the next thing (suma), defines what you want to name the function, in this case "suma" ("add" for those of you who don't know spanish)

    the other two things are the entrance values for the function, in this case: a and b (the two numbers you're going to add)

    the code inside the function are the operations you are doing with the entrance values, note that I'm using a and b, the same values defined at the beggining of the function. What you must know is that these a and b values only exist inside the function, if you make any reference to these values, you will get an error.
    The other thing that can happen is that you also have a global (defined at the beggining of the code, such as

    "double viento, RMactual, pressure;"

    ) variable called "a", in this case, the two a's are completely independent of each other, and if you make a reference to a outside of the function, you're calling the global variable, and if you're doing it inside the function, you're calling the a inside the suma function, and, as soon as the function ends, the variable "a" is destroyed, but this doesn't mean that as soon as you call the function, like

    int x,y,c;
    x=3;
    y=5;
    c=suma(x,y);

    the values x and y would be reset, no!, as soon as you call the function, a takes the value x has, and b takes the value y has, they just copy the values the first variables have.

    you're currently using functions, such as MENU, but you're having some global variables which contain the values you're going to use, so you change the values before calling the function, and check them inside the function. Instead of doing this, you can make your functions like "suma", but with all the values you need to use in your function. This would make your job a lot easier and would make confusion leave.

    Your functions have the word "void" at the beggining and at the end of the function, this means that you are telling the compiler that these functions do not take any parameters (entrance values), and that they don't return any value.

    If you really want to change one or more values that a call to the function passes, such as strcat (defined in string.h and adds up one string to another), there is a programming method called "passing arguments by reference", but that's a very complicated topic and I'm not going to go into it right now

    Just as a side note: almost everything which goes with parenthesis (such as printf, scanf, or fopen) are functions!

    did you understand?
    if not, feel free to tell me so, I really like explaining things (I think I may be cut out to be a programming teacher (I have taught programming to some friends and they are currently good!, considering that they are studying non-programming-and-non-computer majors))

    Oskilian

  12. #12
    Registered User
    Join Date
    Aug 2001
    Posts
    207
    GRACIAS OSK!!

    I think I do understand the theory, but it get messy when I try to apply to some of my functions. I'll post later one of the examples I try if it works.

    But there is something I still don't understand. You and Salem tell me that my functions "don't return any value", you specify that when I call the functions I just use void, with no parameters. But when I run my program, and my functions are used, the variables DO change, so I thought that my functions DO return some values. I really don't get this "returning/non-returning" thing.

    Anyway, I'll see how bad I can do it,

    Thanks

  13. #13
    Former Member
    Join Date
    Oct 2001
    Posts
    955
    Ok, so the function changes the values, but this doesn't mean that it actually returns something.

    The actual idea of a function is to make a black box which solves little problems, there are a lot of functions in math.h, such as double pow(double a, double b), which returns a^b, the idea of a function is that you can call it a lot of times, and for each time, the function does different stuff to different values.

    Take for instancce our function Suma:

    int a,b,c,d,e,f,g;

    a=1;
    b=2;
    c=5;
    d=-2;
    e=suma(a,b);
    f=suma(c,d);
    g=suma(e,f);

    so, what you're doing is reusing the same code to do the same thing, but with different values.

    The kind of functions you are using are what in Basic are called Sub's, or Subroutines, which don't return anything because they don't have to, they just help you to make your code more understandable, and they are mosly called only once, such as in your code.

    for example, the printf function takes the parameters and outputs text to stdout (usually the screen), and what makes it a good function is that you call it hundreds of times in your code, so they are solving small parts of problems to help you solve the big problem (making your program)

    Do you understand?

    Oskilian

  14. #14
    Registered User
    Join Date
    Aug 2001
    Posts
    207
    Yes, I think I do understand this time, finally. Now I'd better keep on working on paper, as Salem said.

  15. #15
    Registered User
    Join Date
    Aug 2001
    Posts
    207

    still working on it

    Hi again.

    I've done some examples with parameter passing, and I understand them.

    I've been trying to follow Salem's advice and now I'm writing again my code.

    First I'm doing all the menus, trying to tidy up the program. I was thinking about including all the calculations in a header file, and keep just the menus on the main code (I don't mean the actual "main").

    The problem is that I think I could get the calculations to use parameter passing, but what about all the menus? Am I supposed to do it as well?

    Because my menus look like this:

    void
    MENU(void)
    {

    char option;
    clrscr();

    printf("\n\nPlease choose one of the following options: \n\n\n");
    printf(" 1) New project\n");
    printf(" 2) Existing project \n");
    printf(" 3) Materials library \n");
    printf(" 4) Instructions \n");
    printf(" 5) Exit \n");

    option = getch();

    while (option>='1'||option<'6')

    switch (option) {
    case '1':
    clrscr();
    new_project();
    caracteristics();
    break;

    case '2':
    clrscr();
    old_project();
    caracteristics();
    break;

    case '3':
    clrscr();
    materials();
    break;

    case '4':
    clrscr();
    instructions ();
    break;

    case '5':
    clrscr();
    EXIT ();

    break;

    default:

    MENU();
    break;

    }
    }



    void
    new_project(void)
    {

    char file_name[8];

    FILE *output;

    printf("Write the name of the new project \n");
    scanf("%s", file_name);
    output = fopen(file_name, "w");
    printf("Please enter the characteristics of the new project \n");
    printf("All the values are to be entered in meters, except the weight");
    printf("\nwhich is in kilograms");

    printf("\nLOA: ");
    scanf("%lf", &LOA);
    fprintf(output, "%.2f\n", LOA);
    printf("\nI: ");
    scanf("%lf", &I);
    fprintf(output, "%.2f\n", I);
    printf("\nJ: ");
    scanf("%lf", &J);
    fprintf(output, "%.2f\n", J);
    printf("\nP: ");
    scanf("%lf", &P);
    fprintf(output, "%.2f\n", P);
    printf("\nE: ");
    scanf("%lf", &E);
    fprintf(output, "%.2f\n", E);
    printf("\nDisplacement (in kilograms): ");
    scanf("%lf", &D);
    fprintf(output, "%.2f\n", D);
    printf("\nGM: ");
    scanf("%lf", &GM);
    fprintf(output, "%.2f\n", GM);
    printf("\nH1: ");
    scanf("%lf", &H1);
    fprintf(output, "%.2f\n", H1);
    printf("\nH2: ");
    scanf("%lf", &H2);
    fprintf(output, "%.2f\n", H2);
    printf("\nH3: ");
    scanf("%lf", &H3);
    fprintf(output, "%.2f\n", H3);
    printf("\nS1: ");
    scanf("%lf", &S1);
    fprintf(output, "%.2f\n", S1);
    printf("\nS2: ");
    scanf("%lf", &S2);
    fprintf(output, "%.2f\n", S2);
    printf("\nS3: ");
    scanf("%lf", &S3);
    fprintf(output, "%.2f\n", S3);

    fclose(output);

    printf("\n\n Press any key to continue");
    getch();
    MENU();

    }



    Am I doing it right this time?
    Is it going to be a mess again?

    Any suggestion?


    Salem: are you bored of me?
    Last edited by Gades; 11-17-2001 at 08:01 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 11-22-2007, 12:58 AM
  2. Passing "this" as function parameter
    By pgavigan in forum C++ Programming
    Replies: 15
    Last Post: 07-13-2007, 10:06 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Problems generating terrain with OpenGL
    By OnionKnight in forum Game Programming
    Replies: 8
    Last Post: 04-26-2007, 05:05 AM
  5. Parameter passing with pointer to pointer
    By notsure in forum C++ Programming
    Replies: 15
    Last Post: 08-12-2006, 07:12 AM