Intermediate · C PROGRAMMING

Functions and return values

What you will learn

Split a program into reusable operations.

Understand the concept

A function declares its return type, name and parameters. Parameters receive copies of argument values. Returning a result lets the caller decide how to use it. Define the function before calling it, or provide a matching prototype.

Read the example

The complete program appears below. Read it first, predict the output, then run it.

Expected output

Square: 36

Practise

Write a function that accepts two integers and returns the larger one.

Common mistake

A function returning int must return a suitable value on every normal path. Multiplication can overflow for large inputs.

Lesson discussion

Log in to participate.

#include <stdio.h>
int square(int value) {
    return value * value;
}

int main(void) {
    int result = square(6);
    printf("Square: %d\n", result);
    return 0;
}
PRACTICE LAB

Read. Edit. Run.

C source · .c

Experiment with the program below. Your code and input are sent to the selected compiler only when you run it.

Tab indents · Shift+Tab unindents · Esc then Tab leaves the editor · Ctrl/Cmd+Enter runs · Ctrl/Cmd+S saves a draft

Changes save automatically to your account.

Input / output example

Example input
No input required.
Expected output for the original program
Square: 36
Output will appear here.

Log in to save your progress

Ready when you are

Need help with your laptop or computer?

Send us the model and fault details. Start your repair request online or contact TCS Pune directly.

WA