-
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
:confused:
LAST NOTE: THE ACTUAL CODE (NOT FINISHED) IS IN THE LAST POST OF THE SECOND PAGE
-
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.
-
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.
-
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
-
The ones who have seen the code, do you have any suggestion or comment?
thanks
-
> 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.
-
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.
;)
-
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.
-
I couldn't attach both files at the same time.
-
>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.
-
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
-
:) 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
-
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
-
Yes, I think I do understand this time, finally. Now I'd better keep on working on paper, as Salem said.
-
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? :confused:
-
header file
About including the calculations in a header file, is it correct?
Or should I do something like a library?
What I want to have, is a shorter code, so I thought about dividing it in parts; like menus and calculations.
Am I trying to do the correct thing?
-
> About including the calculations in a header file, is it correct?
No - header files are for storing definitions only - like #defines, structures, typedefs, and function prototypes.
> Or should I do something like a library?
Probably not - unless you intend to re-use parts of it in other projects.
> What I want to have, is a shorter code, so I thought about dividing it in parts; like menus and calculations.
This is a good idea - what you would end up with is several source files containing the code, and perhaps a single .h file describing the interfaces.
Have you done a design yet?
I think the menu function would be better like this, with the actual switch statement being in main
Code:
int menu ( ) {
char buff[100];
int choice;
do {
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");
fgets( buff, sizeof(buff), stdin );
choice = buff[0];
}
while ( choice < '1' || choice > '5' );
return choice;
}
That way, when you come to pass parameters to
old_project();
caracteristics();
you wont have to pass them all to menu, just to pass them onto your other functions.
-
> Have you done a design yet?
Yes I have, but I don't post it because I've done it by hand on paper. More or less, I've divided the whole thing in:
1) new and existing project
you can create, load and modify projects
2) results
the computer shows the results and offer options like modify,
print on paper, print to file, and I might think of something else
3) materials
list all the existing materials, create, modify
4) calculations
I intend to insert all the calculations that go before >2) results,
maybe all in a huge function (doesn't sound good), I'll see later
I'm leaving the calculations for the end. I think that if I'm able to have an idea of how to do it, and I undersand it, it won't be that difficult.
My next step right now is to apply what you've told me now.
Thanks a lot for your help
-
The examples for parameter passing I've done are not similar to what you're saying.
I've modified the MENU() function as you said, and included the switch in the main(). My problem is how to make the switch to get the value of choice. Should I declare choice as a global variable?
Or what am I supposed to do?
int
main(void)
{
MENU();
switch (choice) {
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;
}
return (0);
}
int
MENU( )
{
char buff[100];
int choice;
clrscr();
do {
printf("\n\nPlease choose one of the following options: \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");
fgets( buff, sizeof(buff), stdin );
choice = buff [0];
}
while ( choice < '1' || choice > '5' );
return (choice);
}
I don't really understand how you use buff, but it doesn't really matters right now.
-
I've declared choice as global and it works.
But I thought I had to try to keep the number of global variables to a minimun, trying to use more local variables.
Is there another way to do it?
-
> My problem is how to make the switch to get the value of choice. Should I declare choice as a global variable?
No!
In main, you have this
Code:
int main ( ) {
int choice;
while ( 1 ) {
choice = menu();
if ( choice == '5' ) break; // from the infinite while loop
switch ( choice ) {
case 1: ....
}
}
return 0;
}
-
Sorry, I had in mind that I could declare local variables in any function but "main".
So I have declared choice as a local at main, but then I have to declare it in MENU() as well.
I've done this so far, and it compiles OK and it runs OK (except that the menu is displayed twice when I return from the function caracteristics, I'll solve this later).
Is it OK then if I just declare the variable twice as local? I mean, I know it works, but is it correct?
double LOA, I, J, P, E, D, GM, H1, H2, H3, S1, S2, S3;
int main( )
{
int choice;
while ( 1) {
choice = MENU();
if ( choice == '5' ) break; // from the infinite while loop
switch (choice) {
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;
}
}
return (0);
}
int
MENU( )
{
char buff[100];
int choice;
clrscr();
do {
printf("\n\nPlease choose one of the following options and press enter: \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");
fgets( buff, sizeof(buff), stdin );
choice = buff [0];
}
while ( choice < '1' || choice > '5' );
return (choice);
}
void
new_project(void)
{
char file_name[PATH_MAX];
FILE *output;
printf("Write the name of the new project \n");
scanf("%s", file_name);
strcat( file_name, ".pro" );
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();
}
void
old_project(void)
{
// Open an existing project
char file_name[PATH_MAX];
FILE *input;
struct ffblk ffblk;
int done;
clrscr();
printf("Directory listing of existing projects \n");
done = findfirst("*.pro",&ffblk,0);
while (!done)
{
printf(" %s\n", ffblk.ff_name);
done = findnext(&ffblk);
}
printf("\nName of the project you want to load: ");
scanf("%s", file_name);
input = fopen(file_name, "r");
if (( input = fopen(file_name, "r+")) == NULL ) // check it for errors
{
printf("There was an error reading from the file!! \n");
printf("Press any key to continue ");
getch();
old_project();
}
fscanf(input, "%lf\n", &LOA);
fscanf(input, "%lf\n", &I);
fscanf(input, "%lf\n", &J);
fscanf(input, "%lf\n", &P);
fscanf(input, "%lf\n", &E);
fscanf(input, "%lf\n", &D);
fscanf(input, "%lf\n", &GM);
fscanf(input, "%lf\n", &H1);
fscanf(input, "%lf\n", &H2);
fscanf(input, "%lf\n", &H3);
fscanf(input, "%lf\n", &S1);
fscanf(input, "%lf\n", &S2);
fscanf(input, "%lf\n", &S3);
fclose(input);
}
void
caracteristics(void)
{
clrscr();
printf("The caracteristics of the actual project are: \n");
printf("1) LOA: %.2f\n", LOA);
printf("2) I: %.2f\n", I);
printf("3) J: %.2f\n", J);
printf("4) P: %.2f\n", P);
printf("5) E: %.2f\n", E);
printf("6) Displacement: %.0f\n", D);
printf("7) GM: %.3f\n", GM);
printf("8) H1: %.2f\n", H1);
printf("9) H2: %.2f\n", H2);
printf("10) H3: %.2f\n", H3);
printf("11) S1: %.2f\n", S1);
printf("12) S2: %.2f\n", S2);
printf("13) S3: %.2f\n", S3);
printf("\n\n Press any key to continue");
getch();
MENU();
}
-
> Sorry, I had in mind that I could declare local variables in any function but "main".
Thats a new one - most newbies just declare it wrong by saying it returns void.
The only 'special' rule for main is that you can't call it recursively - apart from that, its just like any other function.
> Is it OK then if I just declare the variable twice as local? I mean, I know it works, but is it correct?
Very correct.
> double LOA, I, J, P, E, D, GM, H1, H2, H3, S1, S2, S3;
Now to get rid of all these global variables, by putting the declaration inside new_project.
Code:
printf("\nLOA: ");
scanf("%lf", &LOA);
fprintf(output, "%.2f\n", LOA);
printf("\nI: ");
scanf("%lf", &I);
fprintf(output, "%.2f\n", I);
You only need one local variable here - since all you are doing is reading input, and writing it out to a file
Code:
double inputval;
printf("\nLOA: ");
scanf("%lf", &inputval);
fprintf(output, "%.2f\n", inputval);
printf("\nI: ");
scanf("%lf", &inputval);
fprintf(output, "%.2f\n", inputval);
> (except that the menu is displayed twice...
Because it still calls MENU();
It should just return to main, where it will fall out of the switch statement, then back round the while(1) loop into a call to MENU().
-
>> Sorry, I had in mind that I could declare local variables in any >function but "main".
>Thats a new one - most newbies just declare it wrong by saying >it returns void.
>The only 'special' rule for main is that you can't call it recursively -> apart from that, its just like any other function.
I'm glad you've found something new because of me, even it's not something positive. :)
>You only need one local variable here - since all you are doing is >reading input, and writing it out to a file
I've done it. It's a great idea. I only have a problem with it. The function old_project. What I used to do before, was to read the data from an existing .pro file, and copy all the values into the global variables (LOA, B...). No I can't as these variables don't exist anymore.
So I thought:
1) that maybe I should create a file called actual (for example); this file would take the values of the project I want to open. This way, the functions that I'm going to create for the calculations, would take the values reading from this file.
or
2) might be a better idea, just to have a global variable, that keeps the name of the actual project, and when I'm going to use its values (in the calculations functions), I'd just read strait away from the file.
I think that 2) is a better option because to handle a variable will be better than a whole file.
Am I right this time?
-
OK, now I've included a char called actual. The name of the project the user is using, is saved into this variable.
I've compiled it and it works. It runs as well.
I still have to fix the menu appearing twice when I come back from a function (I'll do it later).
I attach the code I have till now, in case anyone wants to have a look.
-
>> (except that the menu is displayed twice...
>Because it still calls MENU();
>It should just return to main, where it will fall out of the switch >statement, then back round the while(1) loop into a call to MENU().
I've changed it to main(), and I still have the same problem.
I've posted this specific question at:
http://www.cprogramming.com/cboard/s...&threadid=5560
-
I think I got it, it the function MENU, you have fgets on stdin, it's supposed to work, but for some strange reason it doesn't.
use scanf instead!
try
I tried it here and it works!
Oskilian
-
Your teacher means to say that the parameters that u are passing to your functions are not ok...i mean to say that, if u have a function say name,
name(param1,param2,......paramn)
here u may not be using the correct params
-
>Your teacher means to say that the parameters that u are >passing to your functions are not ok...i mean to say that, if u >have a function say name,
In fact my problem was that I didn't pass parameters at all.
Now the whole code is changed, with much help from Big Salem and Osk.
I'm still working on it, though. The output is about the same, but the code is a lot more efficient (I think).
Well, I just wanted to say thanks again to Salem and Osk, because you've been quite patient with me.
Salem: actually you've helped as well with my last question. I needed to count the number of lines in a text file. So I used the "search" in the C board, and ended up with some code from you. That's the function I've done:
Code:
int count_lines ( ) {
char buff[BUFSIZ];
int contador = 0;
char file_name[PATH_MAX];
FILE *input;
strcpy (file_name, material);
input = fopen(file_name, "r");
while ( fgets( buff, BUFSIZ, input ) != NULL ) {
contador++;
}
fclose( input );
return (contador);
}