• Home
  • 6.2 Function Calls

6.2 Function Calls

View Categories

6.2 Function Calls

A function call transfers control to a function so that its statements can execute. Arguments supplied in the call provide values for the function’s parameters.

#include <stdio.h>

void displayMessage(void)
{
    printf("Function called\n");
}

int square(int number)
{
    return number * number;
}

int main(void)
{
    displayMessage();

    int result = square(5);

    printf("Square = %d\n", result);

    return 0;
}

Example Output #

Function called
Square = 25

Explanation #

The statement:

displayMessage();

calls displayMessage(), which prints a message and returns no value.

The expression:

square(5)

calls square() with the argument 5. The function calculates 5 * 5 and returns 25.

The returned value is assigned to result:

int result = square(5);

Function Call Components #

Component Example Purpose
Function name square Identifies the function
Argument 5 Supplies a value to the function
Parentheses (5) Enclose the argument list
Return value 25 Value returned by the function
Function call statement displayMessage(); Calls a function without using a return value

A function call can appear in an expression, an assignment, or a standalone statement.

int a = square(4);
printf("%d\n", square(3));
displayMessage();

The first call assigns the returned value to a. The second passes the returned value to printf(). The third calls a function whose return type is void.

A function must be declared before it is called when the compiler needs that declaration to determine the function’s interface.

Powered by BetterDocs

Leave a Reply

Your email address will not be published. Required fields are marked *