That's how a program can be divided into functions and can be called and reused in many ways !

Welcome to the Newbie Programmer Series. In the last post we had an introduction with the program structure. In this post, we will discuss functions. If you have not read that previous part then don't worry I have the introduction here too. So if you are new to this series, please go to the index (click here) and read out all the previous parts so that you can easily understand what is going down below.

*This is a big post, involves many concepts, please try a second reading or contact me if you didn't get it at once. Please continue reading below.*

## Introduction

*f (x) = 3x**'f'*. It takes the value of

*x*and then the specific work is to multiply that

**by 3. It is not necessary to have some given value**

*x**x .*For example the function

**Will always give value 4 Or can make functions with many variables like**

*g() = 4***Programming is not only about maths, try to think different ways through which you can complete some of your work. Lets take an example of a bulb switch :**

*add (x , y) = x + y*

*switch ( button )*

*{*

*if button = on, then turn the bulb 'on'*

*if button = off, then turn the bulb 'off'***Now that is a function too. So to turn a bulb on, just command**

*}**"switch ( on )"*! Well coding is easy, the hard part is to make concept of a function. Anyways one should think how he can break his problem into simpler problems. That will help him to design a function just as we did above.

__NOTE:-__*There is a little more theory below. It may look awkward but please read it and after that coding stuff is down below and that will make your confusion clear.*## Variable data type

**muly ( x, y ) = x * y**has to specify

**int x, y.**

**float x, y.**

## Function returning data type

**muly ( x, y )**function for multiplication involves two integers x and y but after multiplying x and y, it will return the product. That product will also be integer. And that makes the function

**muly**itself an integer function.

## Void

## Return

**C = x * y,**then we will write

**return(C)**to give the result of the function.

**return 0**

What we have used so far is return 0, which means there is no error and the program is completed successfully And it exits !

*Lets do the coding stuff :*

## Defining a Function in C

```
int add ( int x, int y )
{
int c;
c = x + y;
return ( c );
}
```

## Using self-made functions

More easy, just write :

function-name( variable values if involved );

note the semicolon.

So you can use the add function in your program as :

add ( 3, 4 );

## Practically :

```
#include<stdio.h>
main()
{
int sum;
sum = add ( 3, 4 );
printf("Sum of 3 and 4 is %d", sum);
}
int add ( int x, int y )
{
int c;
c = x + y;
return (c);
}
```

```
#include<stdio.h>
main()
{
printf("Sum of 3 and 4 is %d", add(3,4) );
}
int add ( int x, int y )
{
int c;
c = x + y;
return (c);
}
```

```
#include<stdio.h>
main()
{
printf("Sum of 3 and 4 is %d", add(3,4) );
}
int x, y;
int add (x, y)
{
int c;
c = x + y;
return (c);
}
```

```
#include<stdio.h>
main()
{
printf("Sum of 3 and 4 is %d", add(3,4) );
}
int x, y;
int add (x, y)
{
return ( x + y );
}
```

All of the above four programs are exactly the same. And have the same output :

Sum of 3 and 4 is 7

And the functions didn't ends here, we have discussed very little yet, there are many important concepts tricks and tips that we will be discussing. We did a very simple mathematical function but what about other types ? specially the void one ? We will also discuss different ways of using functions. But for the sake of page limit, We will discuss it in the next post. So stay connected !

Please share this post as much as you can so that we can get connected to more geeks who wants to learn programming but have no clue. Thanks for reading :)

Please share this as much as you can so that we can get connected with more and more people who want to learn programming.