Course Content
C Tutorial
About Lesson

Function Arguments and Return Values

In C programming, functions are powerful tools used to perform specific tasks. They can take arguments, which are values passed to the function, and return values, which are the results produced by the function. Understanding function arguments is crucial for writing efficient and effective C programs.

What are Function Arguments?

Function arguments are the values passed to a function when it is called. They allow functions to work with specific data or perform operations based on that data. In C, function arguments are specified within the parentheses following the function name. For example:

void calculateArea(int length, int width) {
int area = length * width;
printf("The area is: %dn", area);
}

In this example, int length and int width are the function arguments.

Types of Function Arguments

In C, there are different types of function arguments:

1. Call by Value

By default, C uses call by value to pass arguments to functions. Call by value means that a copy of the argument’s value is passed to the function. Any changes made to the parameters inside the function do not affect the original values.

void changeValue(int x) {
x = 10; // Changes made here are local to this function
}

int main() {
int num = 5;
changeValue(num);
// The value of num remains unchanged (still 5)
return 0;
}

2. Call by Reference

In some cases, it might be necessary to modify the original value of an argument within a function. Call by reference allows the function to directly access and modify the original value using pointers.

void changeValue(int *x) {
*x = 10; // Modifies the original value
}

int main() {
int num = 5;
changeValue(&num);
// The value of num is modified to 10
return 0;
}

Return Values in Functions

Return values are the values that a function sends back to the calling code. In C, a function can return a single value using the return statement. For example:

int addNumbers(int a, int b) {
int sum = a + b;
return sum;
}

int main() {
int result = addNumbers(3, 5);
// The result will be 8
return 0;
}

Conclusion