20centuryboy
20centuryboy

Reputation: 81

When you define a prototype of a function how does compiler know to which variable in function to assign argument value?

Im new to programming and this really confuses me.Sorry if question is stupid to people more advanced here.

When i study i see programmers assign random variable name to the parameter when creating prototype of it like here in cs 50:

#include <cs50.h>
#include <stdio.h>

float discount(float price);

int main(void) {
    float regular = get_float("Regular Price: ");
    int percent_off = get_int("Percent Off: ");
    float sale = discount(regular, percent_off);
    printf("Sale Price: %.2f\n", sale); }

// Discount price float discount(float price, int percentage) {
    return price * (100 - percentage) / 100; }

I dont know if its expected of me to understand this.Because this is introduction to programming and they go in detail about 0 and 1s but no one clarify this so i guess it should be something you figure out on the fly.So i might just not be for this.But so many things confuse me.

1.In this code and other ones on this introduction to programming course they use these functions like get_int.With argument that you write in to be printed i guess they somehow turn argument into string that is printed.But why after writing initial question execution stops and waits for you to type something in? In all previous cases when you type something in its printed and thats it but now after this print statement computer waits for you to type something why? And after you type in it prints what you typed in it to the screen this time normally and moves on to new line of code but now print statement works as expected only that this time you type in console instead of in code like normally how is that possible ?

2.In this program he (professor) defines prototype of function on top,there argument is called in parenthesis (float price).But later on it says (float price,int percentage)in function definition so i guess prototype and definition are not connected but why bother write anything in prototype other than type of argument?

And more importantly what really confuses me is that in program then it becomes something completely different arguments are now all of a sudden regular, and percent off why its so confusing to me?

3.And after you input these arguments in function how computer knows which one corresponds to what inside of the function if you change names constantly? Like does it look in which orders you type in so first empty variable in function becomes first argument value typed in and second second value ect.?

Upvotes: 1

Views: 85

Answers (1)

Eric Postpischil
Eric Postpischil

Reputation: 223747

1.In this code and other ones on this introduction to programming course they use these functions like get_int.With argument that you write in to be printed i guess they somehow turn argument into string that is printed.

get_float and get_int are functions. Somebody has written code for them. That code prints the parameter it is passed.

But why after writing initial question execution stops and waits for you to type something in?

After printing the string, get_float and get_int ask for input, by calling getchar or some other routine. Sometimes input that the user typed previously may already be in a buffer, in which case the input routine takes that input and returns it. If there is no input already in the buffer, then the getchar or other input routine waits for the user to type more input. This is done in conjunction with operating system services and the software that implements the terminal interface.

2.In this program he (professor) defines prototype of function on top,there argument is called in parenthesis (float price).But later on it says (float price,int percentage)in function definition so i guess prototype and definition are not connected but why bother write anything in prototype other than type of argument?

This is a mistake in the code you show. When discount(regular, percent_off) appears after float discount(float price); (and with that declaration still “visible”), the compiler will complain. The prototype, calls, and definition are connected and cannot work correctly with errors such as this.

3.And after you input these arguments in function how computer knows which one corresponds to what inside of the function if you change names constantly? Like does it look in which orders you type in so first empty variable in function becomes first argument value typed in and second second value ect.?

The arguments and parameters are positional. The value of the first argument is assigned to the first parameter. The value of the second argument is assigned to the second parameter, and so on. The names of the parameters in a function definition are used inside the function body to refer to the parameters. The names of the parameters in a function declaration that is not a definition are irrelevant to the compiler. It is customary to use names in parameters that are informative to human readers, but it is not necessary. The parameter names in declarations that are not definitions may be omitted.

Upvotes: 1

Related Questions