# C Exercises: A simple structure of function

## C Function : Exercise-1 with Solution

Write a program in C to show the simple structure of a function.

**Pictorial Presentation:**

**Sample Solution:**

**C Code:**

```
#include <stdio.h>
int sum (int, int);//function declaration
int main (void)
{
int total;
printf("\n\n Function : a simple structure of function :\n");
printf("------------------------------------------------\n");
total = sum (5, 6);//function call
printf ("The total is : %d\n", total);
return 0;
}
int sum (int a, int b) //function definition
{
int s;
s=a+b;
return s; //function returning a value
}
```

Sample Output:

Function : a simple structure of function : ------------------------------------------------ The total is : 11

**Explanation:**

int sum(int a, int b) //function definition { int s; s = a + b; return s; //function returning a value }

The above function ‘sum’ takes two integer arguments, a and b. It computes the sum of a and b and stores the result in a local integer variable s. Finally, it returns the value of s.

**Time complexity and space complexity:**

The time complexity of the function 'sum' is O(1), as the computation it performs is constant and independent of the input size.

The space complexity of the function 'sum' is also O(1), as it only uses a fixed amount of memory to store the integer variable 's'.

**Flowchart: **

**C Programming Code Editor:**

**Previous:** C Function Exercises Home

**Next:** Write a program in C to find the square of any number using the function.

**What is the difficulty level of this exercise?**

Test your Programming skills with w3resource's quiz.

**Weekly Trends**- Python Interview Questions and Answers: Comprehensive Guide
- Scala Exercises, Practice, Solution
- Kotlin Exercises practice with solution
- MongoDB Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - JOINS
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join