Avocado
Avocado

Reputation: 97

Call a function from withing a function

I am trying to build a CARDIAC Computer simulator in C, and I got to a stage where I need to call a function from within a function.


In the code below I am trying to call the function JAZ(), in the function CARDIAC(), and I get the obvious error saying that I can't call it since it was not pre-defined.
What do I need to do in order for the compiled program to printf "Point reached!"

This is my code:

#include <stdio.h>
#include <stdlib.h>

int input;

void CARDIAC(int* ptr)
{
    int input = *ptr;

    while(input<900)
    {
        if(input<100 && input>=0)
        {
            // INP();
        }
        else if(input<200 && input>99)
        {
            printf("Point reached!\n");
            break;
        }
        else if(input<300 && input>199)
        {
            // LDA();
        }
        else if(input<400 && input>299)
        {
            // LDI();
        }
        else if(input<500 && input>399)
        {
            // STA();
        }
        else if(input<600 && input>499)
        {
            // STI();
        }
        else if(input<700 && input>599)
        {
            // ADD();
        }
        else if(input<800 && input>699)
        {
            // SUB();
        }
        else if(input<900 && input>799)
        {
            JAZ();
        }
        else
        {
            // HRS();
        }
    } 
    printf("Done\n");
}

void JAZ()
{   
    input = 180;
    CARDIAC(&input);
}

int main()
{
    input=820;
    CARDIAC(&input);

    return 0;
}


This is the output I get:

test.c: In function ‘CARDIAC’:
test.c:47:4: warning: implicit declaration of function ‘JAZ’ [-Wimplicit-function-declaration]
    JAZ();
    ^~~
test.c: At top level:
test.c:57:6: warning: conflicting types for ‘JAZ’
 void JAZ()
      ^~~
test.c:47:4: note: previous implicit declaration of ‘JAZ’ was here
    JAZ();
    ^~~

Upvotes: 1

Views: 61

Answers (3)

Holy semicolon
Holy semicolon

Reputation: 1389

What do I need to do in order for the compiled program to printf "Point reached!"

you have in your function Jaz input = 280; so it will not printed because 280 > 200 as you write in your else if statement else if(input<200 && input>99).

 void JAZ()
 {   
     input = 280;       //you must change this value
     CARDIAC(&input);
 }  

and another problem that you have is your loop it will not end.

edit

I don't really know what are you trying to do with while in that function, but If you want to end it input must reach 900 or higher to stop the while loop as you declare in your CARDIAC function while(input<900).

here is a code that print "Point reached" :

 #include <stdio.h>
 #include <stdlib.h>

int input;
void JAZ();

void CARDIAC(int* ptr)
{
    int input = *ptr;

    //while(input<900)
    //{
        if(input<100 && input>=0)
        {
            // INP();
        }
        else if(input<200 && input>99)
        {
            printf("Point reached!\n");
         //   break;
        }
        else if(input<300 && input>199)
        {
            // LDA();
        }
        else if(input<400 && input>299)
        {
            // LDI();
        }
        else if(input<500 && input>399)
        {
            // STA();
        }
        else if(input<600 && input>499)
        {
            // STI();
        }
        else if(input<700 && input>599)
        {
            // ADD();
        }
        else if(input<800 && input>699)
        {
            // SUB();
        }
        else if(input<900 && input>799)
        {
            JAZ();
        }
        else
        {
            // HRS();
        }
    //} 
    //printf("Done\n");
}

void JAZ()
{   
    input = 180;
    CARDIAC(&input);
}


int main()
{
    input=820;
    CARDIAC(&input);

    return 0;
}

Upvotes: 0

Tom Kuschel
Tom Kuschel

Reputation: 775

You have to put function prototypes before the both functions, like:

#include <stdio.h>
#include <stdlib.h>

int input;

/* function prototypes */
void CARDIAC(int *ptr);
void JAZ(void);

void CARDIAC(int* ptr)
{
    int input = *ptr;
 ....

Upvotes: 1

SalvadorViramontes
SalvadorViramontes

Reputation: 580

Try to move the JAZ() function before CARDIAC()

Upvotes: 1

Related Questions