0
0
Cprogramming~20 mins

Function declaration and definition - Mini Project: Build & Apply

Choose your learning style9 modes available
Function declaration and definition
📖 Scenario: You are creating a simple calculator program in C that adds two numbers. To keep your code organized, you will declare and define a function that adds two integers.
🎯 Goal: Build a C program that declares a function add which takes two integers and returns their sum. Then call this function from main and print the result.
📋 What You'll Learn
Declare a function int add(int a, int b); before main
Define the function add after main
Call the function add inside main with arguments 5 and 7
Print the returned sum using printf
💡 Why This Matters
🌍 Real World
Functions help organize code into reusable blocks, making programs easier to read and maintain.
💼 Career
Understanding function declaration and definition is essential for writing modular and clean C programs in software development.
Progress0 / 4 steps
1
Declare the function
Write a function declaration for add that takes two int parameters named a and b, and returns an int. Place this declaration before the main function.
C
Need a hint?

Function declaration tells the compiler about the function name, return type, and parameters before it is used.

2
Define the function
Write the function definition for add after the main function. It should take two int parameters a and b, and return their sum.
C
Need a hint?

The function definition includes the code block that returns the sum of a and b.

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

Store the returned value from add(5, 7) in a variable result.

4
Print the result
Add a printf statement inside main to print the value of result with the message: "Sum is: %d\n".
C
Need a hint?

Use printf("Sum is: %d\n", result); to display the sum.