简体   繁体   中英

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?

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) 

Since you are not passing any value to the function, i suggest you leave the function empty. ie 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. ie

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

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;
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM