Home / Free Tutorials

Practical Learning Guide

Function Parameters and Arguments in C

Trace value copies, return values and pointer arguments with a complete C program

This self-study guide explains the topic and provides a practical starting point. For trainer-led learning, review the related course and request the current syllabus and batch details.

Read the Study Plan

Distinguish a parameter from an argument

In add(int left, int right), left and right are parameter variables declared by the function. In add(2, 3), the expressions 2 and 3 are arguments supplied by the caller. Trace which argument initializes which parameter before reading the function body. Keep the function declaration visible before the call so the compiler can check the declared interface.

Predict the effect on the caller

C passes argument values into the function. Assigning 99 to a local integer parameter changes that parameter, not the caller's integer. A return statement can communicate a computed result, which the caller must use or store. Before running the example, predict the value of score after change_copy and write down why it should remain 7.

Use a pointer when the object must change

Passing &score supplies a pointer value identifying score. The function still receives a value copy, but writing through that valid pointer changes the referenced object. This is different from giving C a separate pass-by-reference calling mode. The example checks for NULL; that check does not make a dangling pointer or a pointer to an invalid object safe.

Compile, run and compare your prediction

Save the complete example as parameters.c. Compile with gcc -std=c11 -Wall -Wextra -Werror parameters.c -o parameters, then run ./parameters. Compare each printed line with your prediction. Treat a compiler warning as a question about the interface or implementation rather than suppressing it. The small fixed integers keep this exercise focused on parameter behaviour.

Practice a return-value version

Replace change_original with a function that returns the new integer, then write score = updated_score(score). Explain how this differs from modifying score through a pointer. Next, call add with a negative value and add a test where both inputs are zero. Keep inputs small: signed integer overflow is a separate issue and is not handled by this introductory add function.

Review checklist

You should be able to label the arguments at a call site, label the parameters in a definition, explain why change_copy leaves score unchanged and explain why *value can update score in the pointer version. If the program behaves differently, reproduce the exact compiler command and compare the copied source before changing several lines at once.

Complete C example

#include <stdio.h>

static void change_copy(int value)
{
    value = 99;
    printf("inside copy: %d\n", value);
}

static void change_original(int *value)
{
    if (value != NULL) {
        *value = 99;
    }
}

static int add(int left, int right)
{
    return left + right;
}

int main(void)
{
    int score = 7;
    change_copy(score);
    printf("after copy: %d\n", score);
    change_original(&score);
    printf("after pointer: %d\n", score);
    printf("sum: %d\n", add(2, 3));
    return 0;
}

Expected result

inside copy: 99
after copy: 7
after pointer: 99
sum: 5

Further reading

Check your progress

Keep your working files, test cases and notes together. Explain one design decision, demonstrate a failure case and list what you would improve next. These concrete outputs make it easier to identify the next skill to practice.