What are Actual and Formal Arguments in a C Function

«
»
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,

advertisement
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
    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.

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

advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn