C - Functions, nested loops
Tags: c, programming, functions, loop
Published: 2022-09-18 18:18:27

Now we are going to see how we use nested loops and function in c.

---

## # C - Nested loops

Nested loop is a way of adding a loop inside a loop and the syntax for a nested loops are:

**Nested for loop syntax**

```
for ( initialization; condition; increment ) {

   for ( initialization; condition; increment ) {
      
      // statement of inside loop
   }

   // statement of outer loop
}

```



**Nested while loop syntax**

```
while(condition) {

   while(condition) {
      
      // statement of inside loop
   }

   // statement of outer loop
}
```
**Nested do while loop syntax**

```
do{

   do{
      
      // statement of inside loop
   }while(condition);

   // statement of outer loop
}while(condition);

```

**Example**

```
#include <stdio.h>

/**
 * main - Entry point
 * Description: nested loop
 *
 *Return: Always 0 (Success)
 */

int main(void)
{
       int i,j;
       
       printf("Nested for loop with @\n");
       for (i = 0;i < 10; i++)
       {
            for(j = 0;j < 10; j++)
            {
                    printf("@");
            }
            printf("\n");
       }
       return (0);
}
```
**output**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7eg9iw60e2bybr941kqy.png)

All the loops have one thing in common, that is once one loop of the outside loop is active then the inner loop starts to activate, the second loop of the outside loop won't be activated until the inner loop finishes.

---

## # C - Functions

A function is a group of statements that are designed to perform a certain task.

Most of the time functions are used to divide up our code into simpler parts that make the code easier to read and debug. 

**void**: is used to indicate that a function don't need a return value.

Syntax

`void FUNCTION_NAME( PARAMETER ){}`

Functions must be declared before usage.

A function declaration tells the compiler about a function's name, return type, and parameters. A function definition provides the actual body of the function.

Now that we understood the basic concepts of functions let's see the anatomy one by one.

Example: a function that add two numbers, we are going to be using this example all throughout this course. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tnojq18y4yxl1bjmjcxs.png)

**Function prototype (signature of the function)**

Function prototype is used to specify the input/output interface of the function, what to input and what return to expect.

In the example given above the function prototype is 
`int add(int a, int b)`

This prototype is used to:

- Specify the return type of the function.e.g on this example we get int value.

- Specify the number and data type of the arguments given to the function. e.g this example has 2 argument and both have an int data type.

- Specify the name of the function. in the example the function is named **add**.


**automatic variable**

This are the variable that are declared inside the function and are only access in the scope of the function.

Example

```
int add(int a,int b)
{
       int c = 1;
       return (a + b + c);
}
```

In this example the `int c = 1` is an automatic variables and only available in the scope of the function.

**Return value**

Are the values returned by the function.

Example

`return (a + b);`

If we don't specify a data type for the return value of the function the compiler will place an int data type by default 

```
add ( int a, int b)
{
  return(a + b);
}
```
 
By Default this is an int return type


**function invocation**

This the calling or usage of the function from the other function statement.

To call a function, you simply need to pass the required parameters along with the function name, and if the function returns a value, then you can store the returned value.

Example

`add(5,6)`


**Forward  Declaration**

Is the defalcation of a function before or after defining a function by using prototype.

Example


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bchsmmw4k9honee5jzg1.png)

This future of a function is mainly used in Header file and we will be covering it in the next topic.

**Function Arguments**

Arguments are values that are accepted through the function parameter.

There are two ways in which arguments can be passed to a function:

- **Call by value**: in this case the value passed as an argument is just the copies of the actual values, Making a change to the value in the function doesn't change the argument in the parent function.

- **Call by reference**: in this case the value passed as an argument is the address of  the actual values, Making a change  to the value in the function affects or changes the argument in the parent function.  

Arrays are always going to be called by reference that means they are passed by address.

We will dive deeper into call by reference in the C - pointers.

---

## # C - Header Files

A header file is a file with **.h** extension which contains C function prototype and macro definitions to be shared between several source files.

Including a header file is like copying the content of the header file.

**syntax**

For system header files 
`#include <file.h>`

It searches for a file named 'file.h' in a standard list of system directories. 

For your own custom header files
`#include "file.h"`

It searches for a file named 'file.h' in the directory containing the current file

**Creating a Header**

To create a header file we include this syntax

```
#ifndef HEADER_FILE_NAME
#define HEADER_FILe_NAME

the entire header file file

#endif
```