Function Calling & Arguments

How to call a function with arguments?

Function & Arguments

Functions are blocks of code that perform a specific task, and they can accept input values (arguments), process them, and optionally return a result. Here's the basic syntax for calling a function and passing arguments (parameters) in C:

return_type function_name(arg1 name1, arg2 name2, ...);

Here's an example of a simple C function and how to call it:

c Copy Code
#include <stdio.h>

 int max(int a, int b, int c) {
 return (a > b) ? ((a > c) ? a : c) : ((b > c) ? b : c);
 }
 
 int main() {
 int a = 10, b = 15, c = 5;
 int result = max(a,b,c);
 printf("The max number is = %d", result);
 return 0;
 }
Output:
The max number is = 15

* In this program, we find the maximum number using the conditional (ternary) operator.

In C programming, functions are defined with a certain number of parameters (also known as formal parameters or formal arguments) that specify the data types and names of the values that the function expects to receive when it is called. These formal parameters act as placeholders for the actual values (actual arguments or arguments) that will be passed to the function when it is called.

Actual Arguments:

The arguments listed in the function calling statement are referred to as actual arguments. They are the actual values passed to a function to compute a value or to perform a task. Consider a function call statement with actual arguments as given below:

int result = add(10, 5);

Formal Arguments:

The arguments used in the function declaration are referred as formal arguments. They are simply formal variables that accept or receive the values supplied by the calling program. Consider a function call statement with formal arguments as given below:

int add(int a, int b) {
    // a and b are formal arguments (parameters)
    return a + b;
}

Note: That the number of actual and formal arguments and their data types should match.

Recursive Function

A recursive function in C is a function that calls itself to solve a problem. Recursive functions are used in programming to break down complex problems into simpler subproblems that are easier to solve.

Here's a basic example of a recursive function in C that calculates the factorial of a non-negative integer:

c Copy Code
#include <stdio.h>

// Recursive function to calculate factorial
int factorial(int n) {
    if (n <= 1) {
        return 1;
    }
    else {
        return n * factorial(n - 1);
    }
}

int main() {
    int num = 5;
    int result = factorial(num);
    printf("Factorial of %d is = %d\n", num, result);
    return 0;
}
Output:
Factorial of 5 is = 120

In this example, the 'factorial' function calculates the factorial of a given integer 'n'. It does so by recursively calling itself with a smaller value of 'n' until it reaches the base case (where 'n' is 0 or 1), at which point it returns 1. The recursive case calculates 'n!' as 'n' multiplied by the factorial of '(n-1)'.

Note: Recursive functions can be powerful and elegant, but they should be used with care, and the termination conditions should be well-defined to avoid infinite recursion.

What's Next?

We actively create content for our YouTube channel and consistently upload or share knowledge on the web platform.