# Functions — Reusable Logic

Published in

--

Recall, the software is like a factory, where a product is produced by processing the raw materials. Even though that is the big picture, the reality is that the factory itself consists of many pieces of machinery, and each of these machines takes some raw materials and processes them to produce a good. The same can be done in programming.

The whole program can be divided into functions. Each function will take an input and produce an output.

# Why Functions?

This is a quiz program we made in the last article. You might have noticed that for each question and the answers in the quiz, about 7 lines of codes should be written. The pattern for each question is the same.

1. Display the question
2. Display the options
3. Ask the user to enter an option
4. Store the input in a variable
5. Compare the variable and the answer
6. Tell the answer is correct if the variable and the answer match or wrong if it does not match.

Since every question repeats the same pattern, programming this way is just redundant and redundancy is to be reduced in programming.

What we can do instead is create a function. The function takes the question, the options, and the answer as the input. It then checks whether the answer is correct and outputs if the answer is wrong or correct.

# Syntax

`return_type function_name (arg1_type arg1_name, ...) {   statements}`

‘return type’ is what the function returns after processing. If the function returns an integer, the return type will be int and so on. If the function does not return anything, the return type will be void.

Arguments are the inputs the function takes. The type and the name of the argument should be given between the parentheses after the function name. If there are multiple arguments, separate the arguments by a comma.

In the curly braces, provide the code which should be executed by the function.

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

This function takes two integer inputs, adds them, and returns the resulting integer.

# Calling a function

What we have done earlier is defining the function. Defining its return type, name, arguments, and the body. When we need to use the function, we should call it. To call a function we just give the function name and put parentheses after it.

`add(5, 6);`

This statement calls the function and provides the inputs or the argument values. If the function is void, this is enough but the function above returns an integer. So, the returned integer should be stored somewhere. A variable is a good place for that.

`int sum;sum = add(5, 6);`

Now, the returned integer will be stored in ‘sum’. The function takes two inputs: 5 and 6, adds them, and store the result in sum. This is a small function, so it does not make sense to make a function for adding two integers. It would be easier to just write

`sum = 5 + 6;`

The function comes in handy when there is more code or logic to be repeated. With the functions, the code should be written only once and thereafter just call the function.

`void add(int a, int b) {  printf("%d", a + b);}`

This is how a void function is defined. See that there is no return statement.

`add(5, 6);`

The void function will be called like this and the function prints the sum of a and b on the screen.

# Built-in vs user-defined function

The above functions are user-defined functions because we defined them. But in every programming language, there will be built-in functions which are functions made available by the language itself. We just need to call them.

The ‘printf()’ is such a built-in function. We just need to call the ‘printf()’ and provide the value to be printed as the input. This function is provided by the C language when we include the ‘iostream.h’ file.

We have seen the ‘int main() { }’, in every program we write. See that is a function. The return type is int and the name of the function is main. The main function is where the program starts execution. The main function tells the compiler: ‘Hey compiler, this is the starting point for execution’. The compiler keeps this in mind and marks that area as the starting point for the execution of the code. The return type of the main function is int and that is the reason for the ‘return 0;’ statement at the end of the main function. As the return type is int, we have to return some integer.

# Default value

We can give the default value for the argument of a function. So, if there is no argument provided, the default value is set to the argument.

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

Argument b has a default value ‘6’. So, if the function is called ‘add(5)’, the value for b will be set as ‘6’ and the function adds 5 and 6. If there is a value provided for ‘b’, the provided value will be used.

# Quiz Program by using function

Here, the question, option A, option B, option C, option D, and the answer is provided to the ask function. The function after taking the input from the user and comparing the input with the answer returns 1 if the answer is correct or 0 if the answer is wrong.

See, now we do not need to write code for evaluating each question and their answer. We can just call the function and it evaluates. The code has become easier to read too. Now, if we want to change the logic in the evaluation process, we just need to change the code in the ask function.

For example, imagine we now want to award 2 points for each correct answer. Previously we have to update the ‘points++’ line to ‘points = points + 2’ on each question. With the help of the function, we just need to change the value of the points variable in the function. Just update ‘points = 1’ to ‘points = 2’.

Functions are awesome!!

Next => Array of Strings

Previous => String — An array of characters with ‘\0’