Nicolas Nino
Nicolas Nino

Reputation: 7

Can I put a function inside of a function?

Im currently taking a COP 3223 class and im learning about function atm. I was just wondering if its possible to put a function inside of a function. Example:

void StudentId(int *);
int CourseLoad();
void invoiceCRN0();
//-------------------------------------------------------------
int main()
{
    int x,y;
    int load;
    StudentId(&x);
    CourseLoad();

    if (CourseLoad()==0)printf("Hello"); 
    return 0;
}
//-----------------------------------------------------------
void StudentId(int *id)
{
    printf("Enter your student id: ");
    scanf("%d", id);
}
//-----------------------------------------------------------
int CourseLoad()
{
    int crnload;
    printf("How many courses (up to 3)?: ");
    scanf("%d", crnload);
    if(crnload==0) return 0;
    if(crnload==1) return 1;
    if(crnload==2) return 2;
    if(crnload==3) return 3;

}
//-------------------------------------------------------------
void invoiceCRN0()
{
    int crm1;
    printf("Enter your class code: ");
    scanf("%d", crm1);
    switch (crm1)
    {
        case 1:
            printf("something");
            break;
        case 2:
            printf("something1");
            break;
        case 3:
            printf("something2");
            break;

    }
}
//-------------------------------------------------------------

Heres an example of a piece of code that will run a function inside of another function. Im basically trying to have it like this but instead of the main function printing out "Invalid CRN" if the return value is 0, I want it to print out an entire function like invoiceCRN0

#include <stdio.h> 
//hint for Project 4 
 
//---------------------------------------- 
 
int checkCrn ( int crn);// returns 1 if crn is OK, 0 otherwise 
int getCreditHours ( int crn ); // returns the credit hours of crn 
char* printPrefix ( int crn); //prints the prefix of crn 
 
//--------------------------- 
void  main( ) 
{ 
 
    int crn1, crn2, crn3; 
 
    printf ("Enter your three course numbers :"); 
    scanf ("%d%d%d", &crn1, &crn2, &crn3); 
 
    if(checkCrn(crn1)*checkCrn(crn2)*checkCrn(crn3) == 0) printf ("Invalid crn!\n"); 
    else { 
 
        printf ("%d\t%s\t%d\n", crn1, printPrefix (crn1), getCreditHours(crn1)); 
        printf ("%d\t%s\t%d\n", crn2, printPrefix (crn2), getCreditHours(crn2)); 
        printf ("%d\t%s\t%d\n", crn3, printPrefix (crn3), getCreditHours(crn3)); 
 
    } 
 
} 
 
//--------------------------- 
int checkCrn ( int crn) 
{ 
    if ( crn != 9696 && crn != 4587 && crn != 4599 ) return 0; 
    return 1; 
} 
//--------------------------- 
 
int getCreditHours ( int crn ) 
{ 
    switch ( crn ) 
    { 
        case 9696: return 5; 
        case 4587: return 3; 
        case 4599: return 1; 
    } 
 
} 
//--------------------------- 
char* printPrefix ( int crn ) 
{ 
    switch ( crn ) 
    { 
        case 9696: return "MAT 111"; 
        case 4587: return "COP 222"; 
        case 4599: return "STA 200"; 
    } 
 
} 

Upvotes: 0

Views: 78

Answers (1)

John Bollinger
John Bollinger

Reputation: 180058

I was just wondering if its possible to put a function inside of a function.

As far as I can tell from your code, you are asking about putting a function call inside a function. Not only can you do so, but in C that is the only place where you can put function calls.

The opposite is true of function definitions: standard C does not provide for defining a function (by providing the function body) inside the body of another function. All function definitions must be at file scope, outside of any other definition.

Upvotes: 2

Related Questions