# User-Defined Function

Page 1 of 1

## 9 Replies - 6550 Views - Last Post: 12 May 2009 - 01:06 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=104206&amp;s=8eafa7dae91f00c6c4443219031d0183&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 iCode

Reputation: 0
• Posts: 52
• Joined: 15-January 09

# User-Defined Function

Posted 09 May 2009 - 06:03 AM

I am creating a program consisting of four user-defined function each doing different task but showing results in the end. However, my code says that elements are undeclared. How can I link the functions?

```#include<stdio.h>

float i=0;

printf("Please enter students marks (0000 to end): ");
while(1){
if (i==0000)
break;
scanf("%f", &marks[i]);
i++;
}
}

float Average(float marks[]){
int sum=0, total=0;

for(i=0;i<'\0'; i++){
sum+=marks[i]
total++}

return sum/total;
}

void Results(float marks[]){
int M=0, P=0, F=0;

for(i=0;i<'\0'; i++){
if (marks[i]<35){
F++;}
else if(35<=temp[i]<=60){
P++;}
else{
M++;}
}

printf("Merit Students: %d\nPass Students: %d\nFailed Students: %d", M, P, F);
}

int main()
{
float marks[100] = {0};
printf("Average = %f C\n", Average(marks));
Results(marks);
return 0;
}
```

This post has been edited by iCode: 09 May 2009 - 06:08 AM

Is This A Good Question/Topic? 0

### #3 AndyH1963

Reputation: 22
• Posts: 201
• Joined: 22-June 08

## Re: User-Defined Function

Posted 09 May 2009 - 06:33 AM

I would suggest that you tidy your code up and:

(1) Drop your K&R style, i.e. placing opening brace on same line as methiod name, condition etc, and adopt the ANSI C++ style where opening brace goes on a new line and braces are vertically aligned. You will then see that you are missing some semi-colons.

(2) Only use UPPER CASE variable names for constants.

You are using a method that you have not even declared i.e. ReadTemp

See what you end up with then.

### #5 janotte

• code > sword

Reputation: 991
• Posts: 5,141
• Joined: 28-September 06

## Re: User-Defined Function

Posted 09 May 2009 - 07:00 AM

I'm not sure how the location of your opening brace would help you here but you absolutely need to stop ending lines with closing braces.
The closing brace belongs directly under the opening character of the initiating statement.
Whether you place the opening brace in that same column (as AndyH1963 recommends) or on the same line as the initiating statement (as I prefer) does not change the fact of where the closing brace belongs.
(the "else if" is an exception which proves the rule).
You need to get that right to have code that is correctly formatted and therefore readable for the reasons AndyH1963 suggests.

Also use indentation consistently and correctly.
Your code was not correctly indented and that made it effectively unreadable for both you and us.
If you can't easily read your code you can't debug it.

So here is your code 'scrubbed' but uncorrected.
I have maintained the OTBS (One True Brace Style - or K&R style as AndyH1963 calls it).
Notice that I have used bracing even on your one line scope at "break;".
Do this. It's not required but it will pay you back many time over in increased readability and debugging ease.
I have left your errors in place.
I have added some line numbers for reasons that will become apparent.
```/* 1*/  #include <stdio.h>
/* 2*/
/* 3*/  void ReadMarks(float marks[]) {
/* 4*/  	float i=0;
/* 5*/  	printf("Please enter students marks (0000 to end): ");
/* 6*/  	while(1) {
/* 7*/  		if (i==0000) {
/* 8*/  			break;
/* 9*/			    }
/*10*/  		scanf("%f", &marks[i]);
/*11*/  		i++;
/*12*/  	}
/*13*/  }
/*14*/
/*15*/  float Average(float marks[]) {
/*16*/  	int sum=0, total=0;
/*18*/  	for(i=0;i<'\0'; i++) {
/*19*/  		sum+=marks[i]
/*20*/  		total++
/*21*/  	}
/*22*/  	return sum/total;
/*23*/  }
/*24*/
/*25*/  void Results(float marks[]) {
/*26*/  	int M=0, P=0, F=0;
/*28*/
/*29*/  	for(i=0;i<'\0'; i++) {
/*30*/  		if (marks[i]<35) {
/*31*/  			F++;
/*32*/  		} else if(35<=temp[i]<=60){
/*33*/  			P++;
/*34*/ 				} else {
/*35*/  				M++;
/*36*/  			}
/*37*/  		}
/*38*/
/*39*/  		printf("Merit Students: %d\nPass Students: %d\nFailed Students: %d", M, P, F);
/*40*/  }
/*41*/
/*42*/
/*43*/
/*44*/
/*45*/  int main() {
/*46*/  	float marks[100] = {0};
/*48*/  	printf("Average = %f C\n", Average(marks));
/*49*/  	Results(marks);
/*50*/  	return 0;
/*51*/  }

```

Here is what the compiler reports:
dic.c: In function ‘ReadMarks’:
dic.c:10: error: array subscript is not an integer
dic.c: In function ‘Average’:
dic.c:18: error: ‘i’ undeclared (first use in this function)
dic.c:18: error: (Each undeclared identifier is reported only once
dic.c:18: error: for each function it appears in.)
dic.c:20: error: syntax error before ‘total’
dic.c: In function ‘Results’:
dic.c:27: warning: implicit declaration of function ‘ReadTemp’
dic.c:27: error: ‘temp’ undeclared (first use in this function)
dic.c:29: error: ‘i’ undeclared (first use in this function)

What did your compiler report?
You did try to compile this before posting it didn't you?

Fix the compiler reported errors and warnings.
Scan your code for missing ";"
Write the function ReadTemp().

If you are still having trouble get back to us.

This post has been edited by janotte: 09 May 2009 - 02:31 PM

### #6 iCode

Reputation: 0
• Posts: 52
• Joined: 15-January 09

## Re: User-Defined Function

Posted 11 May 2009 - 02:33 AM

```/* 1*/  #include <stdio.h>
/* 2*/  float Average(float); void Results(float);
/* 3*/  void ReadMarks(float marks[]) {
/* 4*/		  float i=0;
/* 5*/		  printf("Please enter students marks (0000 to end): ");
/* 6*/		  while(1) {
/* 7*/				  if (i==0000) {
/* 8*/						  break;
/* 9*/					  }
/*10*/				  scanf("%f", &marks[i]);
/*11*/				  i++;
/*12*/		  }
/*13*/  }
/*14*/
/*15*/  float Average(float marks[]) {
/*16*/		  int sum=0, total=0;
/*18*/		  for(i=0;i<'\0'; i++) {
/*19*/				  sum+=marks[i];
/*20*/				  total++;
/*21*/		  }
/*22*/		  return sum/total;
/*23*/  }
/*24*/
/*25*/  void Results(float marks[]) {
/*26*/		  int M=0, P=0, F=0;
/*28*/
/*29*/		  for(i=0;i<'\0'; i++) {
/*30*/				  if (marks[i]<35) {
/*31*/						  F++;
/*32*/				  } else if(35<=marks[i]<=60){
/*33*/						  P++;
/*34*/						  } else {
/*35*/								  M++;
/*36*/						  }
/*37*/				  }
/*38*/
/*39*/				  printf("Merit Students: %d\nPass Students: %d\nFailed Students: %d", M, P, F);
/*40*/  }
/*41*/
/*42*/
/*43*/
/*44*/
/*45*/  int main() {
/*46*/		  float marks[100] = {0};
/*48*/		  printf("Average = %f C\n", Average(marks));
/*49*/		  Results(marks);
/*50*/		  return 0;
/*51*/  }
```

10: array subscript is not an integer <--- I've defined 'i' already, what does it mean by this?
15: conflicting types for 'Average' <--- I only wrote the function prototype at the top so why does it intefere?
18: `i' undeclared (first use in this function) <--- I've cross referenced from the 'ReadMarks' so 'i' should be declared?
25: conflicting types for 'Results' <--- I don't understand what this mean.
29: `i' undeclared (first use in this function) <--- I've cross referenced.

• Saucy!

Reputation: 6259
• Posts: 24,028
• Joined: 23-August 08

## Re: User-Defined Function

Posted 11 May 2009 - 05:15 AM

Your reading skills need help, apparently, as well as your attention to detail.

iCode, on 11 May, 2009 - 04:33 AM, said:

10: array subscript is not an integer <--- I've defined 'i' already, what does it mean by this?

Read the error...NOT AN INTEGER...is your declaration of i here
```float i = 0;
```

an INTEGER???

---

iCode, on 11 May, 2009 - 04:33 AM, said:

15: conflicting types for 'Average' <--- I only wrote the function prototype at the top so why does it intefere?

```float Average(float);
```

```float Average(float marks[])
```

---

iCode, on 11 May, 2009 - 04:33 AM, said:

18: `i' undeclared (first use in this function) <--- I've cross referenced from the 'ReadMarks' so 'i' should be declared?

Reread your book/notes on scope. A variable declared in one function is not visible inside another function.

---

iCode, on 11 May, 2009 - 04:33 AM, said:

25: conflicting types for 'Results' <--- I don't understand what this mean.

Just like the other one.

---

iCode, on 11 May, 2009 - 04:33 AM, said:

29: `i' undeclared (first use in this function) <--- I've cross referenced.

And again, just like the other.

### #8 iCode

Reputation: 0
• Posts: 52
• Joined: 15-January 09

## Re: User-Defined Function

Posted 11 May 2009 - 08:39 AM

I've sorted the other problems but this one I don't understand.

JackOfAllTrades, on 11 May, 2009 - 04:15 AM, said:

```float Average(float);
```

```float Average(float marks[])
```

The prototype is for all values where as the definition is for the specific case. Within the {}, I have considered for the specific case of marks[]. What need to be altered?

• Saucy!

Reputation: 6259
• Posts: 24,028
• Joined: 23-August 08

## Re: User-Defined Function

Posted 11 May 2009 - 10:11 AM

With that prototype, you've told the compiler you're going to be defining a function called Average which takes a float as an argument and returns a float.

When you define the function, you've defined a function called Average which takes an array of floats as an argument and returns a float.

Can you see the difference now?

### #10 iCode

Reputation: 0
• Posts: 52
• Joined: 15-January 09

## Re: User-Defined Function

Posted 11 May 2009 - 10:44 AM

JackOfAllTrades, on 11 May, 2009 - 09:11 AM, said:

With that prototype, you've told the compiler you're going to be defining a function called Average which takes a float as an argument and returns a float.

When you define the function, you've defined a function called Average which takes an array of floats as an argument and returns a float.

Can you see the difference now?

I see. It differs for an array. I should thus change the prototype? Is the prototype supposed to be:
```float Average(float [])
```

Or do I need to change the defined function?

• Saucy!

Reputation: 6259
• Posts: 24,028
• Joined: 23-August 08

## Re: User-Defined Function

Posted 11 May 2009 - 12:13 PM

Well, seeing as you're calling ReadMarks inside Average, and ReadMarks requires an array to be passed, what do you think needs to be changed?

### #12 iCode

Reputation: 0
• Posts: 52
• Joined: 15-January 09

## Re: User-Defined Function

Posted 12 May 2009 - 01:06 AM

JackOfAllTrades, on 11 May, 2009 - 11:13 AM, said:

Well, seeing as you're calling ReadMarks inside Average, and ReadMarks requires an array to be passed, what do you think needs to be changed?

The Average needs to be changed.

```float Average(float [])
```

The code above says average containing a floating point array. I'm not seeing why that is wrong.

This post has been edited by iCode: 12 May 2009 - 01:07 AM