ProgramGuru

C Functions, Parameters and Return Values

C Functions, Parameters and Return Values

Several student reports need the same total calculation. Give that calculation a name so callers can supply different marks without copying its logic.

A function is a callable block with a declared parameter and return-value contract. C passes argument values by value: the function receives copies, including when a copied value is a pointer.

  1. Explain

    Trace arguments into parameters and a returned result back to the caller.

  2. Trace

    Call the same function with 45 and 38.

  3. Apply

    Explain why changing a copied parameter does not change the caller’s value.

Functions: follow the data

  1. Start

    Several student reports need the same total calculation. Give that calculation a name so callers can supply different marks without copying its logic.

  2. Operation

    A function is a callable block with a declared parameter and return-value contract. C passes argument values by value: the function receives copies, including when a copied value is a pointer.

  3. Result

    Total: 75

A guided model of this example. The illustration explains the process; run the complete C program to check its output.

Understand the operation

int total(int maths, int science) names two integer parameters and an integer result. Calling total(40, 35) supplies values for those parameters. return ends that call and provides the computed result.

Declare a function before the caller uses it. In C17, an empty parameter list in a declaration is not the same clear no-parameter contract as (void). Use main(void) and explicit prototypes.

Read the complete C program

This complete C17 program uses a small school example. Read declarations before the statements that use their values. Each printf format describes how the corresponding value is written to standard output.

C17 · complete program

#include <stdio.h>
static int total(int maths, int science) {
  return maths + science;
}
int main(void) {
  int marks = total(40, 35);
  printf("Total: %d\n", marks);
  return 0;
}

Expected output

Total: 75
Run in C compiler →

Follow the values through the code

C17 · code used in this explanation

#include <stdio.h>
static int total(int maths, int science) {
  return maths + science;
}
int main(void) {
  int marks = total(40, 35);
  printf("Total: %d\n", marks);
  return 0;
}

The call initializes the function’s maths parameter to 40 and science to 35.

The function computes 75 and returns it; main stores that result before printing.

Follow the worked calculation

  1. Read the starting values

    The call initializes the function’s maths parameter to 40 and science to 35.

  2. Perform the operation

    The function computes 75 and returns it; main stores that result before printing.

  3. Check the observable result

    Total: 75

    Total: 75
A guided model of this example. The illustration explains the process; run the complete C program to check its output.

Try a small variation

C17 · code used in this explanation

#include <stdio.h>
static int total(int maths, int science) {
  return maths + science;
}
int main(void) {
  int marks = total(40, 35);
  printf("Total: %d\n", marks);
  return 0;
}

Call the same function with 45 and 38.

Worked answer

The function definition stays unchanged. Different supplied values produce 83, demonstrating that reuse means sharing the operation rather than freezing the first student’s data.

C17 · complete program

#include <stdio.h>
static int total(int maths, int science) {
  return maths + science;
}
int main(void) {
  printf("Total: %d\n", total(45, 38));
  return 0;
}

Expected output

Total: 83
Run in C compiler →

Recognize the important boundaries

A local parameter assignment does not change the caller’s integer object. Later pointer lessons show how a copied address can grant access to that caller-owned object.

Do not return the address of a local automatic object. Its lifetime ends when the call returns.

Check your understanding

Choose the answer, then compare the reasoning for every option.

Does assigning to an int parameter change the caller’s int variable?
Choose one answer. You can try again.

Answer and explanation

No; the parameter holds a copy of the argument value.

Correct. A separate mechanism is needed to access and change a caller-owned object.

Use the idea independently

A function is a callable block with a declared parameter and return-value contract. C passes argument values by value: the function receives copies, including when a copied value is a pointer.

Continue with access through pointers.

Continue with recursive calls.

Language reference: WG14 C11 committee draft. These examples use the C17-compatible subset of the explained language features.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: C Arrays, Indexes and Bounds →