Function Call in C

In the C programming language, a function call is a statement that invokes a function and executes the code within it. Functions are blocks of code that perform a specific task and can be called multiple times throughout a program, allowing for code reuse and modular programming.

To make a function call in C, you need to follow a specific syntax. The syntax for a function call is as follows:

function_name(argument1, argument2, ...);

Let’s break down the different components of a function call:

  • function_name: This is the name of the function you want to call. It should match the name of the function you defined earlier in your program.
  • arguments: These are the values or variables that you pass to the function. They are enclosed in parentheses and separated by commas. The number and type of arguments should match the function’s definition.

Here’s an example of a function call in C:

#include <stdio.h>

// Function declaration
void greet(char name[]);

int main() {
  char myName[] = "John";
  
  // Function call
  greet(myName);
  
  return 0;
}

// Function definition
void greet(char name[]) {
  printf("Hello, %s!n", name);
}

In this example, we have a function called “greet” that takes a character array as an argument. Inside the main function, we declare a character array called “myName” and assign it the value “John”. We then call the “greet” function and pass the “myName” array as an argument.

When the “greet” function is called, it receives the “myName” array as its argument and prints out the greeting “Hello, John!” using the printf function.

It’s important to note that function calls in C are executed sequentially. This means that the program will execute each function call in the order they appear in the code. In the example above, the “greet” function call is executed after the declaration and assignment of the “myName” array.

Additionally, C functions can have a return type, which specifies the type of value the function returns. If a function has a return type other than “void”, you can assign the returned value to a variable or use it in an expression.

Here’s an example of a function call that returns a value:

#include <stdio.h>

// Function declaration
int add(int a, int b);

int main() {
  int result;
  
  // Function call and assignment
  result = add(3, 5);
  
  printf("The result is: %dn", result);
  
  return 0;
}

// Function definition
int add(int a, int b) {
  return a + b;
}

In this example, we have a function called “add” that takes two integer arguments and returns their sum. Inside the main function, we declare an integer variable called “result” and assign it the value returned by the “add” function when called with the arguments 3 and 5.

The value of “result” is then printed out using the printf function, resulting in the output “The result is: 8”.

Function calls are an essential part of C programming, allowing you to break down complex tasks into smaller, manageable functions. By understanding how to make function calls and pass arguments, you can write more organized and modular code.

Scroll to Top