Unit 2 · Lesson 420 minAcademic review pending

Functions that take input and give an answer

Arguments are copied into parameters, which is why changing a parameter inside a function never changes the caller's variable.

Choose explanation

After this lesson

You should be able to

  • Write a function with parameters and a non-void return type.
  • Explain pass by value using a traced example.
  • Predict the output of a program that modifies a parameter.
01

Parameters are the function's own variables

When you call area_of(5.0), the value 5.0 is copied into the parameter radius. radius belongs to the function; it is created when the call starts and destroyed when the call ends. The caller's variable and the parameter are two different boxes that briefly hold the same number.

The return type says what kind of answer comes back. return hands one value to the caller and ends the function immediately — any code after it in that function never runs.

One input, one answer
#include <stdio.h>

double area_of(double radius);

int main(void)
{
    double r = 5.0;
    printf("Area = %.2f\n", area_of(r));   /* 78.54 */
    return 0;
}

double area_of(double radius)
{
    return 3.14159 * radius * radius;
}
02

Pass by value, demonstrated

This is the idea students most often get wrong, so trace it rather than trusting it. The function doubles its parameter and prints 20. Back in main, n is still 10 — the function doubled its own copy, and that copy no longer exists.

This is a protection, not a limitation. A function cannot accidentally corrupt the caller's data, so you can call it without reading its body first. When a function genuinely must change a caller's variable, C makes you ask for it explicitly — that is what the next lesson is about.

The copy changes; the original does not
#include <stdio.h>

void twice(int x);

int main(void)
{
    int n = 10;

    twice(n);
    printf("Back in main, n = %d\n", n);   /* 10, not 20 */

    return 0;
}

void twice(int x)
{
    x = x * 2;
    printf("Inside twice, x = %d\n", x);   /* 20 */
}
03

Types are converted silently

If a parameter is a double and you pass an int, C converts it for you — that is safe. The reverse is not: pass 3.7 to an int parameter and it arrives as 3, with the fraction gone and no warning unless you asked for warnings.

The prototype is what makes this checking possible. Without one, the compiler cannot know what type the parameter wants and cannot convert correctly.

Try it yourself

Write a function that takes marks out of 100 and returns the grade as a char, then use it for three students.

Need a hint?

The function should return the grade, not print it — that is the one-job rule from the design lesson.

Check the worked solution

grade_for returns a value and prints nothing, so main decides the format — that separation is what lets the same function feed a screen report, a file, or a test. Each return exits immediately, which is why no else is needed after the first one.

#include <stdio.h>

char grade_for(int marks);

int main(void)
{
    int marks[3] = {92, 64, 30};

    for (int i = 0; i < 3; i++)
        printf("Student %d: %c\n", i + 1, grade_for(marks[i]));

    return 0;
}

char grade_for(int marks)
{
    if (marks >= 80) return 'A';
    if (marks >= 70) return 'B';
    if (marks >= 60) return 'C';
    if (marks >= 35) return 'D';
    return 'F';
}

Quick check

void f(int x) { x = 99; } is called as f(n) where n is 5. What is n afterwards?

Select an answer to check your thinking.

Why this lesson exists

Syllabus mapping

Functions with Input Arguments · Library Functions

Maps to course outcomes CO3, CO4.