Argument in C
Introduction
Arguments are essential in the world of programming because they allow information to be sent between various program components. Programmers can provide function arguments to enable them to accept input values and return output values. Arguments play a crucial role in the C programming language's ability to produce modular and flexible code. The definition, types, and application of parameters in C function calls will all be covered in this tutorial.
Defining Arguments
The value provided to a function when it is called is referred to in C as an argument. The function can carry out particular actions or computations due to it as input. The use of parenthesis and commas to separate arguments is standard. When a function is defined, its function prototype or declaration declares the parameters it anticipates.
Types of Arguments
Formal arguments, usually referred to as parameters, and real arguments are both supported in C.
1.Formal Arguments/Parameters:
Formal arguments, which stand in for the variables that will receive the values given during a function call, are stated in the function prototype. They serve as stand-ins for the arguments themselves. Formal arguments let functions work with various data types without having to rewrite the program completely. Formal arguments in C are identified by their identifier and data type.
2.Actual Arguments:
Actual arguments are the values or expressions supplied to a function during a function call. They are also known as arguments or parameters. They deliver the data required for the function to carry out its actions. The types of actual arguments include constants, variables, and expressions.
Function with and Without argument
1.Function with Argument:
Functions in the C programming language that accept input values or data from the caller code are referred to as functions with arguments or functions with parameters. Data can be handed into a function through arguments, allowing it to conduct actions or calculations using the supplied values.
Syntax
return_type function_name(parameter1_type parameter1_name,parameter2_type parameter2_name,...) { // Function body // Perform operations using the parameters // Return a value (if applicable) }
Example program
#include<stdio.h> int sum(int num1,int num2){ int result =num1 + num2; return result; } int main() { int a =5, b =3; int sum_result =sum(a, b); printf("The sum of %d and %d is: %d ", a, b, sum_result); return 0; }
Output
The sum of 5 and 3 is: 8
2. Function without Argument:
Functions in the C programming language that don't need input values from the caller code are referred to as functions without arguments or parameters. It uses the code contained within the function body as the only basis for carrying out its calculations or actions.
Syntax
return_type function_name() { // Function body // Perform operations // Return a value (if applicable) }
Example program
#include<stdio.h> void greet() { printf("Hello! Welcome to the world of C programming. "); } int main() { greet(); return 0; }
Output
Hello! Welcome to the world of C programming.
Passing Arguments to Function
In C, you must stick to a certain syntax to send parameters to a function. The function name is followed by brackets carrying the actual arguments when invoking a function. The formal arguments specified in the function prototype must be followed in terms of number, order, and type.
Methods for Passing Arguments to Function
1.Call by Value:
When supplying arguments, C, by default, uses the call-by-value mechanism. Making a duplicate of the value of the real arguments and transferring it to the formal arguments is known as a "call by value." The original values of the real parameters outside the function are unaffected by any changes made to the formal arguments inside the function.
2.Call by Reference:
It may occasionally be necessary to change the parameters within a function. Calling a function by reference enables the function to get the address of the real parameters, allowing modifications to be performed directly to the original variables. Pointers must be used as formal arguments using this method.
Argument Modifier
To increase the flexibility and utility of arguments, C offers various modifiers:
1. Default Arguments:
In contrast to certain other programming languages, C does not provide default arguments. But by overloading functions or using C++'s function overloading features, you may obtain a comparable functionality.
2.Variable Arguments:
The 'stdarg.h' header file is used in C to allow variable arguments. A function that accepts variable parameters can take any number of them. The 'stdarg.h' library offers macros and methods like 'va_start, va_arg,' and 'va_end' to handle variable arguments.
Conclusion
The C programming language extensively uses arguments to construct dynamic and adaptive functions. Knowing the various types of arguments, how they are provided, and potential modifiers can help one build dependable and reusable code. Programmers can use arguments to increase their programs' functionality and code readability, whether they use call-by-value or call-by-reference. Now that you are aware of this, you may use arguments in C to your advantage to build modular programs that are more effective.