gonzo
gonzo

Reputation: 41

Beginner C - trying out functions

I tried to write my first function in ANSI C.

The purpose of the function is to get 2 user inputs ( capital , interest_rate ) and to return the result of 'interest_rate * capital' to the main function in which I then try to print out the final result.

My code so far:

#include <stdio.h>

 /* k= Kapital ( capital )
  * i= Zinssatz ( interestrate )
  * s= aufruf der compute funktion
  * 
  */

long compute_interest (long k,long i) {
  printf("Bitte geben Sie Ihr Startkapital ein\n"); /*user input capital*/
  scanf("%ld\n", &k);

  printf("Bitte geben Sie den Zinssatz ein\n"); /*user input intrstrte*/
  scanf("%ld\n", &i);

  return k * i;
}



 long main(void) {
   long s;
   s = compute_interest;
   printf("geld = %ld\n", s);



   return 0;
 }  

Compiling gives me this error message:

    warning: assignment makes integer from pointer without a cast    
    [enabled by default]
    s = compute_interest;
      ^

What is my mistake? What should I change?

Upvotes: 3

Views: 141

Answers (4)

haccks
haccks

Reputation: 106012

In C, function names are treated as pointer to the function itself. compute_interest is a function name and its type is long (*)(long, int). s is of type long int and the assignment s = compute_interest; is assigning a pointer to long data type and that's the reason why you are getting the warning.
You need to place () after compute_interest to let the compiler know that its a function call. You also need to remove functions parameters long k,long i and place it inside the function body.

Upvotes: 1

Damien Black
Damien Black

Reputation: 5647

You need () to make a function call:

compute_interest()

Additionally, the function takes two arguments, so you need to send them in... for example:

compute_interest(2000, 2) 

Upvotes: 4

james jelo4kul
james jelo4kul

Reputation: 829

Since you are not passing any value to the function, i suggest you leave the function empty. i.e your function should be like this

long compute_interest () {

  long k, i;
  printf("Bitte geben Sie Ihr Startkapital ein\n"); /*user input capital*/
  scanf("%ld\n", &k);

  printf("Bitte geben Sie den Zinssatz ein\n"); /*user input intrstrte*/
  scanf("%ld\n", &i);

  return k * i;
}

Then to call up the function you have to put the parentheses. i.e

long main(void) {
   long s;
   s = compute_interest();
   printf("geld = %ld\n", s);



   return 0;
 }  

This must give you the desired results. Hope this helps

Upvotes: 4

dbush
dbush

Reputation: 223972

Two issues:

s = compute_interest;

This doesn't call the function. Because you omitted the (), this actually tries to assign a pointer to the function to s. This is why you're getting the warning. Do this to call the function:

s = compute_interest();

Which brings us to the second issue. You define compute_interest to take two parameters, however the value of those parameters is overwritten by the scanf calls.

What you really want in this situation is for k and i to be local variables rather than parameters:

long compute_interest () {
  long k, i;

  printf("Bitte geben Sie Ihr Startkapital ein\n"); /*user input capital*/
  scanf("%ld\n", &k);

  printf("Bitte geben Sie den Zinssatz ein\n"); /*user input intrstrte*/
  scanf("%ld\n", &i);

  return k * i;
}

Upvotes: 3

Related Questions