WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

C · Basic · question 7 of 100

What is the role of functions in C, and how do you define and call a function?

📕 Buy this interview preparation book: 100 C questions & answers — PDF + EPUB for $5

In C programming language, functions are used to encapsulate a block of code that performs a specific task. Functions are essential for breaking down complex programs into smaller, more manageable parts, and for reusing code across multiple parts of a program. Here’s how you can define and call a function in C:

Defining a function: The syntax for defining a function in C is:

    return_type function_name(parameter_list) {
        // code to be executed
        return return_value;
    }

In this example, return_type is the data type that the function returns, function_name is the name of the function, parameter_list is a list of parameters that the function accepts, and return_value is the value that the function returns.

    #include <stdio.h>
    
    int square(int x) {
        int result = x * x;
        return result;
    }
    
    int main() {
        int value = 5;
        int result = square(value);
        printf("The square of %d is %dn", value, result);
        return 0;
    }

In this example, a function named square is defined that takes one parameter, x. The function returns the square of x, which is calculated and stored in the variable result. The main() function calls the square() function with an argument of 5, and the result is stored in the variable result. The program then prints the message "The square of 5 is 25" to the console.

Calling a function: To call a function in C, you simply use the function name followed by a list of arguments enclosed in parentheses. The syntax for calling a function is:

    function_name(argument_list);

In this example, the square() function is called with an argument of value, which is set to 5.

Understanding how to define and call functions in C is essential for writing modular, reusable code. Functions allow developers to break down complex programs into smaller, more manageable parts, and to reuse code across multiple parts of a program. They also help make programs more readable and easier to maintain.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic C interview — then scores it.
📞 Practice C — free 15 min
📕 Buy this interview preparation book: 100 C questions & answers — PDF + EPUB for $5

All 100 C questions · All topics