Functions

Functions

A function is a self-contained block of code that performs a specific task. Functions are used to break down a program into smaller, more manageable pieces, and also to reuse code throughout the program. A function can take input arguments and can also return a value.

A function declaration is a statement that informs the compiler about the function name, return type, and the types of its parameters. It is used to declare the function signature without defining the actual function body. This is useful when a function is defined later in the program, or when the function is defined in a separate source file.

A function call, on the other hand, is a statement that invokes a function and passes arguments to it. It is used to execute the code inside the function and return a value (if the function has a return type).

Here is an example of a function declaration and function call in C:

				
					#include <stdio.h>
// Function declaration
int add(int a, int b);
int main()
 {
   int x = 5, y = 10, sum;
   // Function call
   sum = add(x, y);
   printf("The sum of %d and %d is %d\n", x, y, sum);
   return 0;
}
// Function definition
int add(int a, int b) {
   return a + b;
}

				
			

In this example, the function add() is declared at the top of the program with its return type int, name add, and its two integer arguments a and b. The function is then defined below the main() function with its code inside a set of curly braces.

Inside the main() function, the add() function is called with the values of x and y as arguments. The return value of the function (which is the sum of x and y) is then assigned to the sum variable. Finally, the program prints out the result using the printf() function.

In summary, a function declaration is used to declare the function signature without defining the actual function body, while a function call is used to execute the code inside the function and return a value (if the function has a return type).

Function  Arguments:

In C programming, there are two ways to pass arguments to a function: call by value and call by reference.

Call by value means that the function receives a copy of the argument value in its parameter, and any changes made to the parameter inside the function do not affect the original argument value. Here is an example of call by value:

				
					#include <stdio.h>
void increment(int x)
 {	
   x++;
   printf("Inside increment function, x = %d\n", x);
}
int main()
 {
   int x = 5;
   increment(x); // call by value
   printf("After function call, x = %d\n", x);
   return 0;
}

				
			

In this example, the increment() function takes an integer argument x by value. Inside the function, x is incremented by 1, but this does not affect the value of x in the main() function because the function only receives a copy of the argument value.

Call by reference means that the function receives a reference to the original argument variable in its parameter, and any changes made to the parameter inside the function affect the original argument value. Here is an example of call by reference:

				
					#include <stdio.h>
void increment(int *x) 
{
   (*x)++;
   printf("Inside increment function, x = %d\n", *x);
}	
int main()
 {
   int x = 5;
   increment(&x);                                              // call by reference
   printf("After function call, x = %d\n", x);
   return 0;
}

				
			

In this example, the increment() function takes a pointer to an integer argument x by reference. Inside the function, the value of x is incremented by 1 using the * operator to dereference the pointer. Since the pointer references the original x variable in the main() function, the increment affects the original value of x.

In summary, call by value means that the function receives a copy of the argument value in its parameter, while call by reference means that the function receives a reference to the original argument variable in its parameter. Any changes made to the parameter inside a call by value function do not affect the original argument value, while changes made inside a call by reference function affect the original argument value. To pass an argument by reference in C, you use a pointer to the original variable as the argument.

Pass  Arrays  as  Function  Parameters

In C, you can pass arrays as function parameters by declaring the parameter as a pointer to the first element of the array. This allows the function to access and manipulate the elements of the array directly.

Here’s an example of a function that takes an array of integers as a parameter and computes the sum of its elements:

				
					int sum(int *arr, int size)
 {
    int result = 0;
    for (int i = 0; i < size; i++)
 {
        result += arr[i];	
    }
    return result;
}

				
			

This function takes two parameters: a pointer to the first element of the array, and the size of the array. The function then iterates over the elements of the array using pointer arithmetic and computes the sum of its elements.

To call this function, you can pass an array as the first argument:

				
					int arr[5] = {1, 2, 3, 4, 5};
int total = sum(arr, 5);
printf("The sum of the array elements is %d\n", total);                             // Outputs: The sum of the array elements is 15

				
			

This calls the sum function with the arr array as the first argument, and the size of the array as the second argument. The function computes the sum of the array elements and returns the result.

Join To Get Our Newsletter
Spread the love