13 Replies - 1471 Views - Last Post: 17 August 2006 - 09:36 AM Rate Topic: -----

#1 brainy_creature  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 177
  • Joined: 07-August 06

Function Prototypes

Post icon  Posted 14 August 2006 - 11:09 AM

here are few lines of ahuge progarm coded in complete reference by herbert
#include<stdio.h>
#include<ctype.h>
#include<stdlib.h>

#define CLASSES 3 
#define GRADES 30

int grade[CLASSES][GRADES]; /*line 1

void enter_grade (void); /* line2

int get_grade (int num);/*line 3

void disp_grades (int g[][GRADES]); /*line 4

int main(void)
{


now my doubt is what does the syntax of line1,2,3,4 mean? what do they signify?
are we creating functions outside the main() ?
please advice?

Is This A Good Question/Topic? 0
  • +

Replies To: Function Prototypes

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Function Prototypes

Posted 14 August 2006 - 11:18 AM

Those functions are not being created - those are function prototypes. The prototype tells the compiler that a function matching those paramters will be called during program execution, and that the defintion for that function follows the main program.
Was This Post Helpful? 0
  • +
  • -

#3 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Function Prototypes

Posted 14 August 2006 - 11:19 AM

Those are called prototypes. This is a way to alert the compiler that it will come across calls in main that are defined later in your code.

It used to be that you had to write the functions first then you would write all the code for you main. Now you can just declare a prototype of the function you will create and put the actual function with its code after main.

**edit: looks like Amadeus beat me to the punch by 1 minute

This post has been edited by jayman9: 14 August 2006 - 11:20 AM

Was This Post Helpful? 0
  • +
  • -

#4 brainy_creature  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 177
  • Joined: 07-August 06

Re: Function Prototypes

Post icon  Posted 14 August 2006 - 11:29 AM

why is there a need of a prototype anyways...?

This post has been edited by brainy_creature: 14 August 2006 - 11:34 AM

Was This Post Helpful? 0
  • +
  • -

#5 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Function Prototypes

Posted 14 August 2006 - 11:35 AM

As noted in the previous posts, the prototype is used to tell the compiler that a function which it has never seen before will be used in the program. Without that knowledge, the compiler will not know how to process the function, as it does not exist in C or C++.
Was This Post Helpful? 0
  • +
  • -

#6 William_Wilson  Icon User is offline

  • lost in compilation
  • member icon

Reputation: 204
  • View blog
  • Posts: 4,807
  • Joined: 23-December 05

Re: Function Prototypes

Posted 14 August 2006 - 11:47 AM

They are needed when method calls occur before the actual method in question. Thus the compiler must be pre-warned of such a method existing as to correctly connect the method call and the method.
Was This Post Helpful? 0
  • +
  • -

#7 1lacca  Icon User is offline

  • code.rascal
  • member icon

Reputation: 44
  • View blog
  • Posts: 3,822
  • Joined: 11-August 05

Re: Function Prototypes

Posted 14 August 2006 - 12:30 PM

It is a legacy of C programming, because when the C (and C++) language was created, there were no 2-pass compilers available, so they couldn't resolve forward references (to functions, variables, etc.) You don't have this kind of problem in Java and most younger languages, because compilers are smarter nowadays, and they can workaround this.
Was This Post Helpful? 0
  • +
  • -

#8 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 180
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

Re: Function Prototypes

Posted 14 August 2006 - 10:57 PM

I would like to elaborate more on what everyone said.

Consider this example.
main()
{
	   int x=3;
	   display(x);
}

display(x)
{
	  printf("%d",x);
}



Now when the compiler compiles this, when it encounters the display() fn, it is confused because it has not encountered the display function before it's function call.
If a prototype is mentioned, then the compiler knows that the particular function of matching parameters will be encountered after main.

You can avoid function prototypes by declaring functions before main().

Function Prototypes are used to reduce the number of passes taken by the compiler to compile the code
Was This Post Helpful? 0
  • +
  • -

#9 brainy_creature  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 177
  • Joined: 07-August 06

Re: Function Prototypes

Post icon  Posted 14 August 2006 - 11:54 PM

so would the prototype look like this....?
void display(x);


This post has been edited by brainy_creature: 14 August 2006 - 11:58 PM

Was This Post Helpful? 0
  • +
  • -

#10 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Function Prototypes

Posted 15 August 2006 - 05:06 AM

Well, now that we've all said the same thing a few times, the answer is yes, the prototype for that function would be what you've provided. Sorry if all the similar answers confused you! :)
Was This Post Helpful? 0
  • +
  • -

#11 vivek_nagathan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 10-August 06

Re: Function Prototypes

Post icon  Posted 17 August 2006 - 03:34 AM

View Postbrainy_creature, on 14 Aug, 2006 - 11:09 AM, said:

here are few lines of ahuge progarm coded in complete reference by herbert
#include<stdio.h>
#include<ctype.h>
#include<stdlib.h>

#define CLASSES 3 
#define GRADES 30

int grade[CLASSES][GRADES]; /*line 1

void enter_grade (void); /* line2

int get_grade (int num);/*line 3

void disp_grades (int g[][GRADES]); /*line 4

int main(void)
{


now my doubt is what does the syntax of line1,2,3,4 mean? what do they signify?
are we creating functions outside the main() ?
please advice?




Hi,

1)The Line1 indicates you are declaring a global variable,a two dimensional array.

2)The line 2 indicates you are declaring a function which you are going to declare it later in your program or in some other file.The function doesn't accept any arguments.

3)line 4 indicates you are declaring a function which you are going to declare it later in your program or in some other file.It accepts one input parameter of type int and return type is also of type int.

4) line 4 is also a declaration.It accepts two dimensional array as input parameter.


Hope.This has helped you.

Regards, :)
Vivekanand
Was This Post Helpful? 0
  • +
  • -

#12 brainy_creature  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 177
  • Joined: 07-August 06

Re: Function Prototypes

Posted 17 August 2006 - 07:17 AM

oh yes ......... it was very usefull, coz am a newcomer any kind of information is very usefull...
i have a question regarding the line 2---

what the fact that the function in line 2 does not accept any argument mean??
what does passing an argument to a function really mean..
what happens there ... can you please explain the logic?
Was This Post Helpful? 0
  • +
  • -

#13 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Function Prototypes

Posted 17 August 2006 - 07:39 AM

http://www.geocities...3/Clesson11.htm

Arguments are information that is passed to the function that may be needed to perform whatever task the function performs. If the function does not need any local information, then it does not require parameters, or arguments.

Think of it like this - if I am a function whose job it is to invite you to a party, but I do not know where the party is, that information needs to be provided to me as an argument. Then, as a function, I take that argument that has been provided, and invite you that location.

If I am a function whose job it is to tell you the time, then no argument is needed (in a simple case). When called, I simply tell you the time.
Was This Post Helpful? 0
  • +
  • -

#14 brainy_creature  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 177
  • Joined: 07-August 06

Re: Function Prototypes

Posted 17 August 2006 - 09:36 AM

View PostAmadeus, on 17 Aug, 2006 - 07:39 AM, said:

http://www.geocities...3/Clesson11.htm

Arguments are information that is passed to the function that may be needed to perform whatever task the function performs. If the function does not need any local information, then it does not require parameters, or arguments.

Think of it like this - if I am a function whose job it is to invite you to a party, but I do not know where the party is, that information needs to be provided to me as an argument. Then, as a function, I take that argument that has been provided, and invite you that location.

If I am a function whose job it is to tell you the time, then no argument is needed (in a simple case). When called, I simply tell you the time.


Awesome... i understood it, it was a great explanation..
thanks a lot again...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1