Jump to content

How to call function? C

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
13 replies to this topic

#1
naspek

naspek

    Newbie

  • Members
  • PipPip
  • 10 posts
how to call function? (line 1)
how am i going to print ELIGIBLE or NOT ELIGIBLE(line 3)


 loan_calculate(); //how to call function?
 printf("Monthly payment <RM>:%f", monthly); //i cant print the value
 printf("Status: ", status); //how am i going to print ELIGIBLE or NOT ELIGIBLE
 
 printf("Status: ");

return 0;
}

void loan_calculate (float L, float DP, int Y, float S, float *monthlyPtr, char *statusPtr, float I){

*monthlyPtr = ((L - DP)+((L-DP)*(I/100)*Y))/(12*Y);

if (*monthlyPtr <= ((25/100)*(*monthlyPtr)))
*statusPtr = 'E';

else
*statusPtr = 'N';

return;
}

Edited by WingedPanther, 01 September 2009 - 05:51 AM.
fix code tags (the # button)


#2
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
Okay, what's the error you get? There seems to be quite a few problems with this, could you enclose the code in [noparse][code][/noparse] tags next time, by pressing the # symbol on the reply?

First off, the loan_calculate function in the main function isn't being passed any arguments, when you clearly need to send it 7 arguments (WOW). You'll need to give that function all the numbers it needs to work with! Also, it should probably return a value, to make it easier and you don't have to pass the "*monthlyPtr" float pointer, just have the function return a float value.

EDIT: Some more, on aesthetics. The names you have for the loan_calculate function variables are extremely hard to understand. I have no idea what "E" is supposed to mean, nor what "DP" is, or any of these one letter variables. The math is not terribly descriptive either. You should name them things that correspond to what they do, even if that means the names are longer. That way, they're self documenting, which is EXTREMELY IMPORTANT. You have no idea how nice it is when you unbury code from six months ago and find that you carefully commented everything, so you know what it does. :)
Wow I changed my sig!

#3
naspek

naspek

    Newbie

  • Members
  • PipPip
  • 10 posts
ok..
this program will calculate whether the user eligible to have a loan or not eligible..

#include <stdio.h>
void loan_calculate (float, float, int, float, float *, char *, float);
int main ()
{
 float L, DP, S, I, monthly;
 int Y;

 printf("LOAN ADVISOR\n");
 printf("Car purchase price/loan <RM>: ");
 scanf("%f", &L);

 if (L < 50000)
  I == 4.0;
 else if (L < 100000 && L >= 50000)
  I == 3.5;
 else if (L >= 100000)
  I == 3.0;

 printf("Down payment <RM>: ");
 scanf("%f", &DP);
 printf("Payment period <in year>: ");
 scanf("%d", &Y);
 printf("Monthly salary: ");
 scanf("%f", &S);
 
 loan_calculate();
 printf("Monthly payment <RM>:%f", monthly);

 
 printf("Status: ");

return 0;
}

void loan_calculate (float L, float DP, int Y, float S, float *monthlyPtr, char *statusPtr, float I){

*monthlyPtr = ((L - DP)+((L-DP)*(I/100)*Y))/(12*Y);

if (*monthlyPtr <= ((25/100)*(*monthlyPtr)))
*statusPtr = 'E';

else
*statusPtr = 'N';

return;
}
above, is my cource code..
should be clearer to consult me..

the errors are:
In function `int main()':
2) too few arguments to function `void loan_calculate(float, float, int, float, float*, char*, float)'
26) at this point in file

#4
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
Alright, so what it's saying is your problem. You're giving loan_calculate too few arguments. Named variables don't carry over from one function to another, just because you named a variable in main "E" or "DP" doesn't mean that when you call it in another function that it will still be there, they are not in the same scope. Variables declared in a function only have what's called "function scope", which means they don't go anywhere outside of the function, so you can use that same name again in another function to signify a different number.

What you need to do is this:
 char* status;
 // -- Your other code, go back to initialization
 loan_calculate(L, DP, Y, S, &monthly, status, I);
 printf("Monthly payment <RM>:%f", monthly);
 printf("Status: %s", status);
Though to be honest I'd cut down on your passed arguments, seriously. There really should be an easier way to do it, like first off you can probably pass the "monthly" value back as a return value, like this:
monthly = loan_calculate(L, DP, Y, S, status, I);
You can also think about putting all of those floats in a structure, that you'd instantiate in main and pass the address of as an argument to loan_calculate. If you did that you could probably bring the argument count down to 2 or 3 at worst.
Wow I changed my sig!

#5
naspek

naspek

    Newbie

  • Members
  • PipPip
  • 10 posts
geez.. thanks..
i understand to call the function now..
however.. i kinda confuse bout the function definition..
i declare 'status' as a character..
how am i going to print it as E=Eligible and N=NOT ELIGIBLE?
should i use string instead..?

my program should run like below..

LOAN ADVISOR
Car purchase price/loan <RM>: 15000
Down payment <RM>:0
Payment period <in year>:7
Monthly salary:1500

Monthly payment <RM>:228.57
Status:ELIGIBLE


#6
veda87

veda87

    Programmer

  • Members
  • PipPipPipPip
  • 126 posts
Zekedragon already explained you abt the status problem also...
char* status;

 // -- Your other code, go back to initialization

 loan_calculate(L, DP, Y, S, &monthly, status, I);

 printf("Monthly payment <RM>:%f", monthly);

 printf("Status: %s", status);

all you have to do is

void loan_calculate (float L, float DP, int Y, float S, float *monthlyPtr, char *statusPtr, float I){


*monthlyPtr = ((L - DP)+((L-DP)*(I/100)*Y))/(12*Y);


if (*monthlyPtr <= ((25/100)*(*monthlyPtr)))

statusptr = (char *) malloc(sizeof("ELIGIBLE");

strcpy(statusptr, "ELIGIBLE");


else

statusptr = (char *) malloc(sizeof("NOTELIGIBLE");

strcpy(statusptr, "NOTELIGIBLE");


return;

}

And one thing...
*statusPtr = 'E';

this will show an error.. one cant directly assign a character to an pointer.. one have to allocate memory and then assign the memory to the pointer..then store character in the memory...

#7
kiddies

kiddies

    Programmer

  • Members
  • PipPipPipPip
  • 130 posts
i just share my code how to call function in c

#include<stdio.h>

void tuker(int *px,int *py) {
	int temp = *px ;
	
	*px = *py;
	*py = temp ;
	}
	
	
	int main(void) {
	
	int a=10, b=20;
	
	printf("before:\n");
	printf("nilai a = %d\n",a);
	printf("nilai b = %d\n",b);
	
	
	tuker(&a,&b); /* call the function*/
	
	printf("after:\n");
	printf("nilai a = %d\n",a);
	printf("nilai b = %d\n",b); 
	
	return 0;
	}


#8
naspek

naspek

    Newbie

  • Members
  • PipPip
  • 10 posts
wow!
i haven't learn 'strcpy', 'malloc' and 'sizeof'..
what i'm trying to do is, i want to store E = ELIGIBLE and N = NOT ELIGIBLE.
then.. i want to call them by referrence in main function..

 char *statusPtr;


 loan_calculate(L, DP, Y, S, &monthly, status, I);

 printf("Monthly payment <RM>:%.2f", monthly);

 printf("Status:%s", status);


return 0;

}


void loan_calculate (float L, float DP, int Y, float S, float *monthlyPtr, char *statusPtr, float I){


*monthlyPtr = ((L - DP)+((L-DP)*(I/100)*Y))/(12*Y);


if (*monthlyPtr <= ((25/100)*(*monthlyPtr)))

statusPtr = 'E';

E = ELIGIBLE;


else

statusPtr = 'N';

N = NOT ELIGIBLE;


return;

}

however.. these errors occur..
a.c: In function `main':
a.c:28: error: incompatible type for argument 6 of `loan_calculate'
a.c: In function `loan_calculate':
a.c:40: warning: assignment makes pointer from integer without a cast
a.c:41: error: `E' undeclared (first use in this function)
a.c:41: error: (Each undeclared identifier is reported only once
a.c:41: error: for each function it appears in.)
a.c:41: error: `ELIGIBLE' undeclared (first use in this function)
a.c:43: error: syntax error before "else"
a.c:45: error: `N' undeclared (first use in this function)
a.c:45: error: `NOT' undeclared (first use in this function)
a.c:45: error: syntax error before "ELIGIBLE"

#9
veda87

veda87

    Programmer

  • Members
  • PipPipPipPip
  • 126 posts
instead of
char *statusPtr
use
char statusPtr

use shouldn't use
statusPtr = 'E';

E = ELIGIBLE;


else

statusPtr = 'N';

N = NOT ELIGIBLE;

just use
statusPtr = 'E';

else

statusPtr = 'N';


in main() function use this to check and print the status
if(statusptr == 'E') {

printf("status: ELIGIBLE\n");

} else if(statusptr == 'N') {

printf("status: NOTELIGIBLE\n")

}

ok.. this will help u....

#10
naspek

naspek

    Newbie

  • Members
  • PipPip
  • 10 posts
this is my latest code
#include <stdio.h>

void loan_calculate (float, float, int, float, float *, char* , float);

int main ()

{

 float L, DP, S, I, monthly;

 int Y;

 char statusPtr;

 printf("LOAN ADVISOR\n");

 printf("Car purchase price/loan <RM>: ");

 scanf("%f", &L);


 if (L < 50000)

  I == 4.0;

 else if (L < 100000 && L >= 50000)

  I == 3.5;

 else if (L >= 100000)

  I == 3.0;


 printf("Down payment <RM>: ");

 scanf("%f", &DP);

 printf("Payment period <in year>: ");

 scanf("%d", &Y);

 printf("Monthly salary: ");

 scanf("%f", &S);


 loan_calculate(L, DP, Y, S, &monthly, statusPtr, I);

 printf("Monthly payment <RM>:%.2f\n", monthly);

 printf("Status: %s", statusPtr);

 return 0;

}


void loan_calculate (float L, float DP, int Y, float S, float *monthlyPtr, char *statusPtr, float I){


*monthlyPtr = ((L - DP)+((L-DP)*(I/100)*Y))/(12*Y);


if (*monthlyPtr <= ((25/100)*(*monthlyPtr))){

statusPtr = 'E';

char *E = "ELIGIBLE";

}

else{

statusPtr = 'N';

char *N = "NOT ELIGIBLE";

}

return;

}
Error occur:
a.c: In function `main':
a.c:26: warning: passing arg 6 of `loan_calculate' makes pointer from integer without a cast
a.c: In function `loan_calculate':
a.c:37: warning: assignment makes pointer from integer without a cast
a.c:41: warning: assignment makes pointer from integer without a cast

ok.. i really apreciate u guys help..
its really help me to understand it step by step..
however..
for this code
printf("Status: %s", statusPtr);
and this one..
if (*monthlyPtr <= ((25/100)*(*monthlyPtr))){

statusPtr = 'E';

char *E = "ELIGIBLE";

}

else{

statusPtr = 'N';

char *N = "NOT ELIGIBLE";
i still can't figure out..
thanks veda87, kiddies and ZekeDragon for your help.. :thumbup:
but, i need to do the E=ELIGIBLE & N=NOT ELIGIBLE in function definition..
in main function, i just have to print it.. so it will give the output:
Status: ELIGIBLE or..
Status: Not Eligible

The problem is, i cant print it out.. :crying:

#11
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
Here's the new (and somewhat improved) version of your code:
#include <stdio.h>
#include <string.h>

float loan_calculate (float, float, int, float, char *, float);

int main ()
{
 float loanAmount, downPayment, applicantSalary, interest, monthlyPayment;
 int paymentPeriod;
 char eligibility[13]; /* Make sure their is enough room either way. */

 printf("LOAN ADVISOR\n");
 printf("Car purchase price/loan <RM>: ");
 scanf("%f", &loanAmount);

 if (loanAmount < 50000)
  interest = 4.0;
 else if (loanAmount < 100000 && loanAmount >= 50000)
  interest = 3.5;
 else if (loanAmount >= 100000)
  interest = 3.0;

 printf("Down payment <RM>: ");
 scanf("%f", &downPayment);
 printf("Payment period <in year>: ");
 scanf("%d", &paymentPeriod);
 printf("Monthly salary: ");
 scanf("%f", &applicantSalary);
 /* I have to wonder, what if you get trash values from
    all of these? You need to test user input! */

 monthlyPayment = loan_calculate(loanAmount, downPayment, paymentPeriod,
                                 applicantSalary, eligibility, interest);

 printf("Monthly payment <RM>: %.2f\n", monthlyPayment);

 printf("Status: %s", eligibility);

return 0;
}

float loan_calculate (float L, float DP, int Y, float S, char *statusPtr, float I){

float monthlyPay = ((L - DP)+((L-DP)*(I/100)*Y))/(12*Y);

if (monthlyPay <= (.25 * S))
strcpy(statusPtr, "ELIGIBLE");
else
strcpy(statusPtr, "NOT ELIGIBLE");

return monthlyPay;
}
All I did was fix the char returning ELIGIBLE or NOT ELIGIBLE. I also changed your if statement since the code "*monthlyPtr <= ((25/100)*(*monthlyPtr))" will ALWAYS return false no matter what value *monthlyPtr is (wait, scratch that, it might return true if *monthlyPtr is negative, but I don't think that's what you want. :P). I also took the liberty to rename the variables in main, these are the kinds of names you should use when naming variables, they're much easier to read and to understand their purpose in the code. :)

Take a look at it, and try to understand exactly what is going on. It shouldn't be that hard.
Wow I changed my sig!

#12
veda87

veda87

    Programmer

  • Members
  • PipPipPipPip
  • 126 posts
This code is exactly the same as ZekeDragon's code but it is without using strcpy command....

#include <stdio.h>


float loan_calculate (float, float, int, float, char *, float);



int main ()

{

        float loanAmount, downPayment, applicantSalary, interest, monthlyPayment;

        int paymentPeriod;

        char eligibility; /* Make sure their is enough room either way. */


        printf("LOAN ADVISOR\n");

        printf("Car purchase price/loan <RM>: ");

        scanf("%f", &loanAmount);


        if (loanAmount < 50000)

                interest = 4.0;

        else if (loanAmount < 100000 && loanAmount >= 50000)

                interest = 3.5;

        else if (loanAmount >= 100000)

                interest = 3.0;


        printf("Down payment <RM>: ");

        scanf("%f", &downPayment);

        printf("Payment period <in year>: ");

        scanf("%d", &paymentPeriod);

        printf("Monthly salary: ");

        scanf("%f", &applicantSalary);

        /* I have to wonder, what if you get trash values from

        all of these? You need to test user input! */


        monthlyPayment = loan_calculate(loanAmount, downPayment, paymentPeriod,applicantSalary, &eligibility, interest);


        printf("Monthly payment <RM>: %.2f\n", monthlyPayment);


        if(eligibility == 'E') {

                printf("Status: ELIGIBLE");

        } else if(eligibility == 'N') {

                printf("Status: NOT ELIGIBLE");

        }


        return 0;

}


float loan_calculate (float L, float DP, int Y, float S, char *statusPtr, float I)

{


        float monthlyPay = ((L - DP)+((L-DP)*(I/100)*Y))/(12*Y);


        if (monthlyPay <= (.25 * S))

                *statusPtr = 'E';

        else

                *statusPtr = 'N';


        return monthlyPay;

}