Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors

C Functions

Function Definition and Declaration:

  • A function in C consists of a function declaration (prototype) and a function definition.
  • The function declaration specifies the function name, return type, and parameter types (if any). It informs the compiler about the existence and signature of the function.
  • The function definition contains the actual implementation of the function, including the function body (code).
  • Here’s an example of a function declaration and definition:
C
// Function declaration
int addNumbers(int a, int b);

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

Calling Functions:

  • To use a function, you need to call it from another part of your program.
  • Function calls typically include the function name followed by parentheses, which may contain arguments (if any).
  • The return value of a function can be assigned to a variable or used directly in expressions.
  • Here’s an example of calling the addNumbers() function:
C
int result = addNumbers(3, 4);
printf("The sum is: %d\n", result);

Function Parameters:

  • Functions can have parameters, which allow you to pass values to the function.
  • Parameters are specified in the function declaration and definition within parentheses.
  • The values passed to the function are called arguments.
  • Here’s an example of a function with parameters:
C
int multiply(int a, int b) {
    int product = a * b;
    return product;
}

Function Return Type:

  • The return type of a function specifies the type of value the function returns.
  • Functions can have various return types, such as int, float, char, void, etc.
  • If a function doesn’t return a value, the return type is void.
  • Here’s an example of a function with a return type of float:
C
float calculateAverage(float a, float b, float c) {
    float average = (a + b + c) / 3;
    return average;
}

Function Call by Value:

  • By default, C uses call by value, which means that function arguments are passed as copies of their values.
  • Any changes made to the parameter within the function do not affect the original argument.
  • Here’s an example illustrating call by value:
C
void increment(int value) {
    value++;
}

int main() {
    int num = 5;
    increment(num);
    printf("The value is: %d\n", num);  // Output: The value is: 5
    return 0;
}

Function Prototypes:

  • Function prototypes allow you to declare a function before its actual definition.
  • Prototypes specify the function’s name, return type, and parameter types.
  • Prototypes are typically placed in a header file that can be included in multiple source files.
  • This enables functions to be defined later in the code but still be usable in other parts of the program.
  • Here’s an example of a function prototype:
C
// Function prototype
int multiply(int a, int b);

int main() {
    int result = multiply(3, 4);
    printf("The product is: %d\n", result);
    return 0;
}

// Function definition
int multiply(int a, int b) {
    int product = a * b;
    return product;
}