Function Arguments in C Programming with Examples

This C Tutorial Explains Function Arguments in C Programming with Example(s).

Let’s, first, try to understand why function arguments are required. Consider, for example, a C Program below:

#include <stdio.h>
void hahaha(void);	/* function hahaha() declared here */
 
int main(void)
{
    hahaha();	        /* hahaha() is called in main() */
    return 0;
}
 
/* hahaha() defined here */
void hahaha(void)
{
	printf("ha ha ha!\n");
}

Output as:

ha ha ha!

In the above program, ‘hahaha()’ just displayed a massage. It neither required to obtain any arguments from calling function in ‘main()’, nor did it need to return any value back to the calling function. In other words, ‘hahaha()’ didn’t need to communicate any information either ways with the calling function.

advertisement
advertisement

Basically, functions in a program are used for communicating information with calling function. For such communication, it requires arguments to be used both in function call and in function definition in correct number and order with their respective types. Function declaration, however, allows to omit argument names but their number and types in correct order, in a comma separated list, to be specified in the parenthesis following the function name. For example:

#include <stdio.h>
double mul_floats(float, float); /* declaration: variable names omitted */
 
int main(void)
{
    float x = 4.67, y = 3.55;
    double result;
 
    result = mul_floats(x, y);	
    /* function call with arguments in correct order with types */
 
    printf("multiplication of %f and %f is %lf\n", x, y, result);
    return 0;
}
 
double mul_floats(float u, float v) /* u and v two floats declared */
{
    return u * v;
}

Here’s output,

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
multiplication of 4.670000 and 3.550000 is 16.578501

In above program, two floats ‘x’ and ‘y’ in the calling function, mul_floats(x, y), are passed to function mul_floats() for multiplication. mul_floats() fun. received their copies in two other floats ‘u’ and ‘v’ respectively, which are private/local to this function. Recall here that floats ‘x’ and ‘y’ are actual arguments while floats ‘u’ and ‘v’ are formal arguments. mul_floats() then returned the product of two floats back to calling function via return statement. Notice that function mul_floats() is of type double, therefore return statement

    return u * v;

will return a double value i.e. product of two floats u and v is typecast implicitly to double before sending back to the calling function.

advertisement

Calling function received the product in a double variable named result & displayed the product to user.

Variables used in function call and function definition respectively to pass and receive the values are called function arguments. Note however that variables used in the calling function are called actual arguments while those used in the function definition are called as formal arguments or parameters.

advertisement

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.