![]() |
|
#1
|
|||
|
|||
Need help understand function prototypes!hi, i take programming at school and i need some help understanding functions better becuase i have an assigntment due in 2 weeks...
so far my main code looks like this CPP / C++ / C Code:
my problem is i need to use functions to add and subtract values from one another and keep a running total of how many "deposits, withdrawals, and cheques" a user uses... thanks alot for any help... Brendan. Last edited by JdS : 20-Oct-2005 at 19:31.
Reason: Please insert your C code between [c] & [/c] tags
|
|
#2
|
||||
|
||||
Re: Need help understand function prototypes!Hi Blstretch
Welcome to the GID Forums. The function declaration should be of this type: CPP / C++ / C Code:
funtion_name(type varname1, type varname2, . . . , type varnameN); Here is the information about pass by value and pass by reference: C always passes arguments `by value' by default: a copy of the value of each argument is passed to the function; the function cannot modify the actual argument passed to it: CPP / C++ / C Code:
CPP / C++ / C Code:
You can learn more about functions in this link: Using Functions in C In your case you have to use the pass by reference as you want to update the values of balance, cheques etc. So for using this statement, CPP / C++ / C Code:
Use the function like this: CPP / C++ / C Code:
Similarly, you can define the other functions updateDeposit and updateWithdrawal. Please read the Guidelines before posting again. Regards, Paramesh. __________________
Don't walk in front of me, I may not follow. Don't walk behind me, I may not lead. Just walk beside me and be my friend. |
|
#3
|
|||
|
|||
Re: Need help understand function prototypes!thanks alot...that clears things up a bit
|
|
#4
|
|||
|
|||
Re: Need help understand function prototypes!umm...just a quick thing, when i tried to compile my program, the first part of it anyway, it had like 100 errors in it like
Code:
mostly those, i understood some of the other ones, and fixed a few, but i do not understand what the debugger means by this. and btw i know i didnt mention before but this is simple C prgramming, not C++ or anything...and i am using crimson editor to write my code as its got syntax hi-liting. any help would be great, thanks. |
|
#5
|
||||
|
||||
Re: Need help understand function prototypes!Since you didn't post the code that gives the errors, all we can do is guess. If you read the guidelines as requested you'll know what to tell us and how.
__________________
Cow: You're a lawyer too? Mooseblood (mosquito): Ma'am, I was already a bloodsucking parasite. All I needed was a briefcase! |
|
#6
|
|||
|
|||
Re: Need help understand function prototypes!ohh sorry, i thought i posted, my mistake i must have screwed something up....
CPP / C++ / C Code:
sorry about the previous post. thanks alot |
|
#7
|
||||
|
||||
Re: Need help understand function prototypes!Hi BlStretch,
Here are some tips: ----------------------------------------------------------------------------------------------------------------- Please put semicolon at the end of each statement. (your program misses some. Can you find where?) You can easily find errors by looking at the code itself. for example look at the line: CPP / C++ / C Code:
You missed out a double quote. ----------------------------------------------------------------------------------------------------------------- When you use a function, make sure that you use the function in the way it is defined. for example consider this line: You used no parameters in getTrans() function. CPP / C++ / C Code:
CPP / C++ / C Code:
----------------------------------------------------------------------------------------------------------------- Next, You can return a value only if you specify a return type. So if you want to return trans, define the function like this: CPP / C++ / C Code:
----------------------------------------------------------------------------------------------------------------- If you want to use the getBalance statement like this, CPP / C++ / C Code:
You should define the getBalance function like this: CPP / C++ / C Code:
----------------------------------------------------------------------------------------------------------------- In your function trans, you used the balance variable. Like this: CPP / C++ / C Code:
So if you still want to use balance variable, you can modify the function like this: CPP / C++ / C Code:
And dont pass every parameter as reference. If you want only to use the variable balance and you dont need to vary the variable in a function, use the simple pass by value as in the previous getTrans function definition. ----------------------------------------------------------------------------------------------------------------- Dont use the parameters of the updateCheque function to define the other functions updateDeposit and updateWithdrawal. Declare the function the way you want the function to use. If you want to use updateDeposit like this: CPP / C++ / C Code:
CPP / C++ / C Code:
Similarly you modify the other function updateWithdrawal according to the way you want to use them in the main function. ----------------------------------------------------------------------------------------------------------------- You can return only one value from any function using the return statement. How would you return many values? That is why we use pass by reference. So if you modify the variables passed by reference, they are modified in the main function itself. ----------------------------------------------------------------------------------------------------------------- Rememer this again: Quote:
Note how the values passed are used inside the program. Can you find the errors in your functions based on this? ----------------------------------------------------------------------------------------------------------------- I hope you understand what i have typed here. If you have any doubts or clarifications, please post again. Regards, Paramesh. __________________
Don't walk in front of me, I may not follow. Don't walk behind me, I may not lead. Just walk beside me and be my friend. Last edited by Paramesh : 21-Oct-2005 at 07:35.
|
|
#8
|
||||
|
||||
Re: Need help understand function prototypes!In addition to what Paramesh mentioned, let's look at your getTrans() function in detail. The prototype:
CPP / C++ / C Code:
CPP / C++ / C Code:
CPP / C++ / C Code:
The prototype states it's a void function with a parameter that is passed back. This means the function does not return a value that can immediately be used as you did in main(). The function must be defined as CPP / C++ / C Code:
In the function, as Paramesh mentioned you cannot use balance because it's not in the function. This is a bad place to display it anyway because it's not necessary in this function. Move the print lines into main(). Also, if you want a specific format for the number, specify it. Instead of having two printf's that differ by a space, define the printf statement as: CPP / C++ / C Code:
The menu is defined as CPP / C++ / C Code:
CPP / C++ / C Code:
1) read 2) adjust for display 3) add new commands 4) remove old commands You defined the variable trans as a pointer but use it as a character. Since it's a pointer, you need to reference it as *trans. CPP / C++ / C Code:
2) trans is a pointer, not a character 3) you have no ';' 4) read this. 'Nuff said. CPP / C++ / C Code:
So, rewrite your function as: The prototype: CPP / C++ / C Code:
CPP / C++ / C Code:
CPP / C++ / C Code:
__________________
Cow: You're a lawyer too? Mooseblood (mosquito): Ma'am, I was already a bloodsucking parasite. All I needed was a briefcase! |
|
#9
|
|||
|
|||
Re: Need help understand function prototypes!thanks alot guys, i appreciate the time you spent helping me with the coding...
i tried to wrap my head around the whole prototype function thing really quickly, and i think it didnt work very well. as well, i am terrible at not placing my ; after statements, its just one of those things i forget often, im trying to find a program to work with my crimson editor that will underline bad coding so i can correct it better. Also thanks alot for explaining functions as i am trying to stay ahead of my class by reading a little ahead in the book and trying to simplfy my code by using these new ways of programming. thanks for yout time, brendan |
|
#10
|
|||
|
|||
Re: Need help understand function prototypes!I know replying to myself in my own trhead mite not be correct, but i just need to ask, is that
CPP / C++ / C Code:
i read the part 5 and 6 of the tutorials you made Walt, and they make sense, i just dont quite get the functioning of a while statement without braces. |
Recent GIDBlog
Last Week of IA Training by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The