0
0
CHow-ToBeginner · 3 min read

How to Call a Function in C: Simple Syntax and Example

In C, you call a function by writing its name followed by parentheses () and passing any required arguments inside. For example, functionName(argument1, argument2); calls the function with those values.
📐

Syntax

To call a function in C, use the function's name followed by parentheses. Inside the parentheses, include any arguments the function needs, separated by commas. End the call with a semicolon.

  • functionName: The name of the function you want to run.
  • ( ): Parentheses to hold any arguments.
  • arguments: Values passed to the function (optional if the function takes none).
  • ;: Marks the end of the statement.
c
functionName(argument1, argument2);
💻

Example

This example shows how to define a function that adds two numbers and how to call it from main. It prints the result to the screen.

c
#include <stdio.h>

// Function that adds two integers and returns the sum
int add(int a, int b) {
    return a + b;
}

int main() {
    int result = add(5, 3);  // Calling the function with 5 and 3
    printf("Sum is %d\n", result);
    return 0;
}
Output
Sum is 8
⚠️

Common Pitfalls

Common mistakes when calling functions in C include:

  • Forgetting parentheses after the function name, which means the function is not called.
  • Not passing the correct number or type of arguments the function expects.
  • Missing the semicolon at the end of the function call.
  • Calling a function before declaring or defining it, causing compiler errors.
c
// Wrong: missing parentheses, no function call
int result = add;  

// Correct: function call with parentheses and arguments
int result = add(2, 4);
📊

Quick Reference

Remember these tips when calling functions in C:

  • Always use parentheses () to call a function.
  • Pass the right number and type of arguments.
  • End the call with a semicolon ;.
  • Declare or define the function before calling it.

Key Takeaways

Call a function by writing its name followed by parentheses and arguments if needed.
Always include parentheses to actually call the function, not just refer to it.
Pass the correct number and type of arguments the function expects.
End the function call statement with a semicolon.
Declare or define functions before calling them to avoid errors.