Menu
Coddy logo textTech

Parameters

Part of the Fundamentals section of Coddy's C journey — lesson 48 of 63.

Functions in C can accept parameters (also called arguments), which are values passed to the function when it is called.

Declare a function that takes parameters:

int add(int a, int b) {
    return a + b;
}

In this example, add is a function that takes two integer parameters, a and b, and returns their sum.

Call a function with parameters:

int result = add(5, 3);

After executing the above code, result contains:

8

You can also use variables as arguments:

int x = 10;
int y = 20;
int sum = add(x, y);

After executing the above code, sum contains:

30
challenge icon

Challenge

Easy

Create a function named calculateArea that takes two parameters:

  • An integer length
  • An integer width

The function should calculate and return the area (length × width) of a rectangle.

Then, in the main function, read two integers from the user representing the length and width of a rectangle, call the calculateArea function with these values, and print the result in the format: "Area: X", where X is the calculated area.

Cheat sheet

Functions in C can accept parameters (arguments) - values passed when the function is called.

Declare a function with parameters:

int add(int a, int b) {
    return a + b;
}

Call a function with parameters:

int result = add(5, 3);  // result = 8

Use variables as arguments:

int x = 10;
int y = 20;
int sum = add(x, y);  // sum = 30

Try it yourself

#include <stdio.h>

// Write your calculateArea function here

int main() {
    // Read variables
    
    // Call calculateArea and print the result
    
    return 0;
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals