0
0
Cprogramming~15 mins

Return values in C - Mini Project: Build & Apply

Choose your learning style9 modes available
Return values
📖 Scenario: You are creating a simple calculator program in C that adds two numbers. You will write a function that returns the sum of two integers.
🎯 Goal: Build a C program with a function called add that takes two integers and returns their sum. Then call this function and print the result.
📋 What You'll Learn
Create a function named add that takes two int parameters
The add function must return the sum of the two integers
Call the add function from main with the values 5 and 7
Print the returned result using printf
💡 Why This Matters
🌍 Real World
Functions that return values are used everywhere in programming to calculate results and pass data between parts of a program.
💼 Career
Understanding return values is essential for writing reusable code and working with APIs, libraries, and complex software systems.
Progress0 / 4 steps
1
Create the add function signature
Write the function signature for a function called add that takes two int parameters named a and b and returns an int. Do not write the function body yet.
C
Need a hint?

Function signature includes return type, function name, and parameters.

2
Define the add function body
Write the full definition of the add function that returns the sum of a and b. Use a return statement.
C
Need a hint?

Use return a + b; inside the function body.

3
Call add in main and store result
Write the main function. Inside it, call the add function with arguments 5 and 7, and store the result in an int variable named result.
C
Need a hint?

Call add(5, 7) and assign it to result.

4
Print the result
Add a printf statement in main to print the value of result followed by a newline. Use the format specifier %d.
C
Need a hint?

Use printf("%d\n", result); to print the sum.