Actual and Formal Arguments in C

This C Tutorial Explains Actual and Formal Arguments in a C Function with Example(s).

Consider, for example, a C program below,

/*
 * actual_formal_arg.c -- program hinges on relationship between actual and
 * formal arguments
 */
#include <stdio.h>
void even_odd(int);
 
int main(void)
{
    int num, what;
 
    printf("\nProgram determines a given integer is EVEN or ODD\n\n");
 
    printf("Enter an integer number: (q to quit)\n");
    while (what = scanf("%d", &num) == 1) {
        even_odd(num);
    /* integer num here is distinct from integer num in even_odd() */
        printf("Enter another integer: (q to quit)\n");
    }       
 
    printf("Thank you!\n");
    return 0;
}
 
/* num declared here is private to even_odd() */
void even_odd(int num)
{
    if (num % 2 == 0)
        printf("%d is EVEN Number!\n", num);
    else
        printf("%d is ODD Number!\n", num);
 
    return;
}

Below is output of the above program, for several test integers,

Program determines a given integer is EVEN or ODD
 
Enter an integer number: (q to quit)
1009
1009 is ODD Number!
Enter another integer: (q to quit)
34
34 is EVEN Number!
Enter another integer: (q to quit)
12
12 is EVEN Number!
Enter another integer: (q to quit)
1
1 is ODD Number!
Enter another integer: (q to quit)
2
2 is EVEN Number!
Enter another integer: (q to quit)
q
Thank you!

Now, we unravel difference between actual and formal arguments. Integer variable num in ‘main()’ is local to ‘main()’ and is distinct from integer variable num, being private to ‘even_odd()’ even they have same names. Therefore, there’s no conflict with same names when used in different functions. When function

advertisement
advertisement
    even_odd(num);

in ‘main()’ is called, actual value of num, here for ex. 1009, is placed in temporary storage area called stack, from where this value is copied to the argument num of called function.

void even_odd(int num) 
{
}

Function ‘even_odd()’ performs on the copied value of original value of num. Therefore, argument num private to even_odd() function is called formal argument or formal parameter and argument num local to main() function is called actual argument.

Sanfoundry Global Education & Learning Series – 1000 C Tutorials.

If you wish to look at all C Tutorials, go to C Tutorials.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.