# Problem with user defined function in C

Page 1 of 1

## 3 Replies - 676 Views - Last Post: 26 April 2015 - 07:48 PMRate 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=374996&amp;s=1a12778b206e090a82d5e7dd4d038f94&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 fallonrae

Reputation: 0
• Posts: 6
• Joined: 12-April 15

# Problem with user defined function in C

Posted 26 April 2015 - 07:27 PM

my user defined function does not seem to be working correctly here is the code:
```#include <stdio.h>

[// Function Declarations

float sumNP(int nickels, int pennies);

int main()
{
// Declarations
int quarters = 0, dimes = 0, nickels = 0, pennies = 0;
float sumofQD = 0.0;
float sumofNP = 0.0;
float dollarAmt = 0.0;

// Input
printf("Please enter the amount of nickels: ");
scanf("%d", &nickels);

printf("Please enter the amount of pennies: ");
scanf("%d", &pennies);

// Process
sumofNP = sumNP(nickels, pennies);
scanf("%f", &sumofNP);

// Output
printf("The total dollar amount is :\$%.2f", sumofNP);

return 0;

}

float sumNP(int nickels, int pennies)
{
float sumNP;
sumNP = .05 * nickels + .01 * pennies;
return sumNP;
}]

```

instead of printing out the actual sum this is what it does when I try to run it:
[Please enter the amount of nickels: 3
Please enter the amount of pennies: 3
3
The total dollar amount is :\$3.00]

after I press 3 for pennies and press enter it does to the next line and expects input, then that is the output.

Overall my question is what is wrong with my function? why isn't the program coming up with the correct output?

This post has been edited by jimblumberg: 26 April 2015 - 07:55 PM
Reason for edit:: Added missing code tags please learn to use them properly.

Is This A Good Question/Topic? 0

## Replies To: Problem with user defined function in C

### #2 infernorthor

• D.I.C Lover

Reputation: 362
• Posts: 1,718
• Joined: 07-February 14

## Re: Problem with user defined function in C

Posted 26 April 2015 - 07:36 PM

you requested a scanf the overwrote sumofNP , you typed 3 it printed \$3.00

• جوروترا

Reputation: 287
• Posts: 986
• Joined: 18-April 09

## Re: Problem with user defined function in C

Posted 26 April 2015 - 07:38 PM

Hi,

Your code produces a bunch warnings and an error. You should increase your compiler warning level and fix these warnings/error:

```||In function 'main':|
|10|warning: implicit declaration of function 'printf' [-Wimplicit-function-declaration]|
|10|warning: incompatible implicit declaration of built-in function 'printf' [enabled by default]|
|11|warning: implicit declaration of function 'scanf' [-Wimplicit-function-declaration]|
|11|warning: incompatible implicit declaration of built-in function 'scanf' [enabled by default]|
|17|warning: implicit declaration of function 'sumNP' [-Wimplicit-function-declaration]|
|7|warning: unused variable 'dollarAmt' [-Wunused-variable]|
|5|warning: unused variable 'sumofQD' [-Wunused-variable]|
|4|warning: unused variable 'dimes' [-Wunused-variable]|
|4|warning: unused variable 'quarters' [-Wunused-variable]|
|29|error: conflicting types for 'sumNP'|
|17|note: previous implicit declaration of 'sumNP' was here|
||=== Build failed: 1 error(s), 9 warning(s) (0 minute(s), 5 second(s)) ===|
```

Notice the error "conflicting types for 'sumNP'". You have a function sumNP() and a variable float sumNP in side that function ?

### #4 fallonrae

Reputation: 0
• Posts: 6
• Joined: 12-April 15

## Re: Problem with user defined function in C

Posted 26 April 2015 - 07:48 PM

infernorthor, on 26 April 2015 - 07:36 PM, said:

you requested a scanf the overwrote sumofNP , you typed 3 it printed \$3.00

Wow! Thank you so much! it works now!!