Back to C Programming
2026-07-125 min read

Syntax of function call

Learn Syntax of function call step by step with clear examples and exercises.

Why This Matters

Function calls are a fundamental aspect of programming in C, allowing you to structure your code and reuse functionalities effectively. This guide will delve into the syntax of function call, providing practical examples, common mistakes, and interview-ready insights.

Why This Matters

Understanding function calls is crucial for writing efficient and maintainable C programs. By organizing your code into functions, you can:

  1. Reuse functionalities across multiple parts of your program.
  2. Break down complex tasks into smaller, manageable pieces.
  3. Improve readability by separating related code snippets.
  4. Debug and test individual functions more easily.
  5. Write modular programs that can be easily extended or modified.

Prerequisites

To follow this guide, you should have a basic understanding of C programming concepts:

  1. Variables, data types, and operators
  2. Control structures (if-else statements, loops)
  3. Basic input/output using printf() and scanf() functions
  4. Arrays and pointers (optional but recommended for advanced topics)

Core Concept

Defining a Function

A function in C is a self-contained block of code that performs a specific task. To define a function, you use the following syntax:

return_type function_name(parameters) {
// function body
}
  1. return_type: The data type of the value returned by the function (optional for functions that don't return any value).
  2. function_name: A unique identifier for the function.
  3. parameters: Zero or more variables representing input values passed to the function.
  4. // function body: The code block containing the instructions executed when the function is called.

Calling a Function

To call a function, you use its name followed by parentheses containing any required arguments. The syntax for calling a function is as follows:

function_name(arguments);
  1. function_name: The identifier of the function you want to call.
  2. arguments: Zero or more values passed to the function (if any).

Example: A Simple Function for Addition

Let's create a simple function that adds two integers and returns the result:

#include <stdio.h>

int add(int num1, int num2) {
int sum = num1 + num2;
return sum;
}

int main() {
int num1 = 5;
int num2 = 3;
int result = add(num1, num2);
printf("The sum is: %d\n", result);
return 0;
}

In this example, we have defined a function called add() that takes two integer arguments and returns their sum. In the main() function, we call add() with the values 5 and 3, store the result in the variable result, and print it to the console.

Worked Example

Let's create a more complex example that demonstrates multiple functions and their interactions:

#include <stdio.h>
#include <math.h>

// Function to calculate the square root of a number
double sqrt(double num) {
return sqrt(num);
}

// Function to check if a number is even or odd
int is_even(int num) {
if (num % 2 == 0) {
return 1; // Even
} else {
return 0; // Odd
}
}

// Function to find the factorial of a number
unsigned long long factorial(unsigned int n) {
if (n <= 1) {
return 1;
} else {
return n * factorial(n - 1);
}
}

int main() {
double num = 9.0;
int check_even = 6;
unsigned long long fact = 5;

printf("The square root of %.2f is: %.2f\n", num, sqrt(num));
printf("Number %d is %s even.\n", check_even, is_even(check_even) ? "It is" : "It isn't");
printf("Factorial of %u is: %llu\n", fact, factorial(fact));

return 0;
}

In this example, we have defined three functions: sqrt(), is_even(), and factorial(). In the main() function, we call these functions with different arguments and print the results to the console.

Common Mistakes

  1. Forgetting to include required header files: Make sure you include all necessary header files for your functions (e.g., `, `).
  2. Incorrect parameter types or number of parameters: Ensure that the data types and number of arguments match those declared in the function definition.
  3. Not returning a value from a function that should return one: If your function is supposed to return a value, make sure you include a return statement with the appropriate value.
  4. Calling a function before its definition: In C, functions must be defined before they can be called within the same file.
  5. Not initializing local variables: Local variables in a function are automatically initialized to zero or NULL by default, but it's good practice to initialize them explicitly when necessary.

Practice Questions

  1. Write a function that takes two floating-point numbers as arguments and returns their average.
  2. Implement a function that swaps the values of two integer variables passed by reference.
  3. Create a function that finds the maximum of three integers.
  4. Write a function that calculates the area of a circle given its radius.
  5. Implement a function that checks if a string is a palindrome.

FAQ

  1. What happens when a function is called but not defined in the current file?
  • In C, functions must be defined before they can be called within the same file. If you try to call an undeclared function, you will receive a compiler error.
  1. Can I pass arrays or pointers as arguments to my functions?
  • Yes, you can pass arrays and pointers as arguments to your functions in C. When passing an array, the array name is implicitly converted to a pointer to its first element.
  1. How do I handle errors or exceptional conditions within a function?
  • You can use conditional statements (e.g., if, else if) to check for error conditions and return appropriate error codes or messages. Alternatively, you can use exception handling mechanisms provided by modern C compilers.
  1. Can I overload functions in C?
  • No, C does not support function overloading like some other programming languages (e.g., C++). Each function must have a unique signature (name and parameter list) to avoid ambiguity during compilation.
  1. What is the difference between a function prototype and a function definition?
  • A function prototype declares the name, return type, and parameters of a function, while a function definition provides the actual implementation of the function. Function prototypes are used to inform the compiler about upcoming functions in your code.