About Us

About Us.
The Problem
When I started learning programming language in college, I found that there was a lack of simple programming codes with examples. Due to this it was really hard for students like me to find information that is simple to grasp.
In my free time, I started writing Programming codes in easy way and sharing them on dumbcodes in 2018. I have started this as my hobby, so I try to give it a time for giving more and more easy programming codes.

 Our main focus at dumbcodes is simplicity. There are programmers all over the world of different levels of programming experience and having different English proficiency. 

What we offer
Our goal at dumbcodes is to create simple programming codes that are easy to read and understand. 
It is the group right now I am alone posting the programming codes as my hobby so later on I shall grow it as my full time job.

What we’re working on
The dumbcodes is always working to make it better resource for our users. I constantly update our articles to improve their quality and correctness.

This blog was developed by Adarsh Srivastav.
Instagram - @adarsh.777
For more details contact : as92093@gmail.com

C Programming Recursion

A function that calls itself is known as a recursive function. And, this technique is known as recursion.

How recursion works?

void recurse()
{
    ... .. ...
    recurse();
    ... .. ...
}

int main()
{
    ... .. ...
    recurse();
    ... .. ...
}
How recursion works in C programming?
The recursion continues until some condition is met to prevent it.
To prevent infinite recursion, if...else statement (or similar approach) can be used where one branch makes the recursive call and other doesn't.

Example: Sum of Natural Numbers Using Recursion

#include <stdio.h>
int sum(int n);

int main()
{
    int number, result;

    printf("Enter a positive integer: ");
    scanf("%d", &number);

    result = sum(number);

    printf("sum=%d", result);
}

int sum(int num)
{
    if (num!=0)
        return num + sum(num-1); // sum() function calls itself
    else
        return num;
}
Output
Enter a positive integer:
3
6
Initially, the sum() is called from the main() function with number passed as an argument.
Suppose, the value of num is 3 initially. During next function call, 2 is passed to the sum()function. This process continues until num is equal to 0.
When num is equal to 0, the if condition fails and the else part is executed returning the sum of integers to the main() function.
Calculation of sum of natural number using recursion

Advantages and Disadvantages of Recursion

Recursion makes program elegant and cleaner. All algorithms can be defined recursively which makes it easier to visualize and prove. 
If the speed of the program is vital then, you should avoid using recursion. Recursions use more memory and are generally slow. Instead, you can use loop.

Types of User-defined Functions in C Programming

For better understanding of arguments and return value from the function, user-defined functions can be categorized as:
  • Function with no arguments and no return value
  • Function with no arguments and a return value
  • Function with arguments and no return value
  • Function with arguments and a return value.
The 4 programs below check whether an integer entered by the user is a prime number or not. And, all these programs generate the same output.

Example #1: No arguments passed and no return Value

#include <stdio.h>

void checkPrimeNumber();

int main()
{
    checkPrimeNumber();    // no argument is passed to prime()
    return 0;
}

// return type of the function is void becuase no value is returned from the function
void checkPrimeNumber()
{
    int n, i, flag=0;

    printf("Enter a positive integer: ");
    scanf("%d",&n);

    for(i=2; i <= n/2; ++i)
    {
        if(n%i == 0)
        {
            flag = 1;
        }
    }
    if (flag == 1)
        printf("%d is not a prime number.", n);
    else
        printf("%d is a prime number.", n);
}
The checkPrimeNumber() function takes input from the user, checks whether it is a prime number or not and displays it on the screen.
The empty parentheses in checkPrimeNumber(); statement inside the main() function indicates that no argument is passed to the function.
The return type of the function is void. Hence, no value is returned from the function.

Example #2: No arguments passed but a return value

#include <stdio.h>
int getInteger();

int main()
{
    int n, i, flag = 0;

    // no argument is passed to the function
    // the value returned from the function is assigned to n
    n = getInteger();

    for(i=2; i<=n/2; ++i)
    {
        if(n%i==0){
            flag = 1;
            break;
        }
    }

    if (flag == 1)
        printf("%d is not a prime number.", n);
    else
        printf("%d is a prime number.", n);

    return 0;
}

// getInteger() function returns integer entered by the user
int getInteger()
{
    int n;

    printf("Enter a positive integer: ");
    scanf("%d",&n);

    return n;
}
The empty parentheses in n = getInteger(); statement indicates that no argument is passed to the function. And, the value returned from the function is assigned to n.
Here, the getInteger() function takes input from the user and returns it. The code to check whether a number is prime or not is inside the main() function.

Example #3: Argument passed but no return value

#include <stdio.h>
void checkPrimeAndDisplay(int n);

int main()
{
    int n;

    printf("Enter a positive integer: ");
    scanf("%d",&n);

    // n is passed to the function
    checkPrimeAndDisplay(n);

    return 0;
}

// void indicates that no value is returned from the function
void checkPrimeAndDisplay(int n)
{
    int i, flag = 0;

    for(i=2; i <= n/2; ++i)
    {
        if(n%i == 0){
            flag = 1;
            break;
        }
    }
    if(flag == 1)
        printf("%d is not a prime number.",n);
    else
        printf("%d is a prime number.", n);
}
The integer value entered by the user is passed to checkPrimeAndDisplay() function.
Here, the checkPrimeAndDisplay() function checks whether the argument passed is a prime number or not and displays the appropriate message.

Example #4: Argument passed and a return value

#include <stdio.h>
int checkPrimeNumber(int n);

int main()
{
    int n, flag;

    printf("Enter a positive integer: ");
    scanf("%d",&n);

    // n is passed to the checkPrimeNumber() function
    // the value returned from the function is assigned to flag variable
    flag = checkPrimeNumber(n);

    if(flag==1)
        printf("%d is not a prime number",n);
    else
        printf("%d is a prime number",n);

    return 0;
}

// integer is returned from the function
int checkPrimeNumber(int n)
{
    /* Integer value is returned from function checkPrimeNumber() */
    int i;

    for(i=2; i <= n/2; ++i)
    {
        if(n%i == 0)
            return 1;
    }

    return 0;
}
The input from the user is passed to checkPrimeNumber() function.
The checkPrimeNumber() function checks whether the passed argument is prime or not. If the passed argument is a prime number, the function returns 0. If the passed argument is a non-prime number, the function returns 1. The return value is assigned to flag variable.
Then, the appropriate message is displayed from the main() function.

Which approach is better?

Well, it depends on the problem you are trying to solve. In case of this problem, the last approach is better.
A function should perform a specific task. The checkPrimeNumber() function doesn't take input from the user nor it displays the appropriate message. It only checks whether a number is prime or not, which makes code modular, easy to understand and debug.

C Programming User-defined functions

A function is a block of code that performs a specific task.
C allows you to define functions according to your need. These functions are known as user-defined functions. For example:
Suppose, you need to create a circle and color it depending upon the radius and color. You can create two functions to solve this problem:
  • createCircle() function
  • color() function

Example: User-defined function

Here is a example to add two integers. To perform this task, a user-defined function addNumbers() is defined.
#include <stdio.h>

int addNumbers(int a, int b);         // function prototype

int main()
{
    int n1,n2,sum;

    printf("Enters two numbers: ");
    scanf("%d %d",&n1,&n2);

    sum = addNumbers(n1, n2);        // function call

    printf("sum = %d",sum);

    return 0;
}

int addNumbers(int a,int b)         // function definition   
{
    int result;
    result = a+b;
    return result;                  // return statement
}

Function prototype

A function prototype is simply the declaration of a function that specifies function's name, parameters and return type. It doesn't contain function body.
A function prototype gives information to the compiler that the function may later be used in the program.

Syntax of function prototype

returnType functionName(type1 argument1, type2 argument2,...);
In the above example, int addNumbers(int a, int b); is the function prototype which provides following information to the compiler:
  1. name of the function is addNumbers()
  2. return type of the function is int
  3. two arguments of type int are passed to the function
The function prototype is not needed if the user-defined function is defined before the main() function.

Calling a function

Control of the program is transferred to the user-defined function by calling it.

Syntax of function call

functionName(argument1, argument2, ...);
In the above example, function call is made using addNumbers(n1,n2); statement inside the main().

Function definition

Function definition contains the block of code to perform a specific task i.e. in this case, adding two numbers and returning it.

Syntax of function definition

returnType functionName(type1 argument1, type2 argument2, ...)
{
    //body of the function
}
When a function is called, the control of the program is transferred to the function definition. And, the compiler starts executing the codes inside the body of a function.

Passing arguments to a function

In programming, argument refers to the variable passed to the function. In the above example, two variables n1 and n2 are passed during function call.
The parameters a and b accepts the passed arguments in the function definition. These arguments are called formal parameters of the function.
Passing arguments to a function
The type of arguments passed to a function and the formal parameters must match, otherwise the compiler throws error.
If n1 is of char type, a also should be of char type. If n2 is of float type, variable b also should be of float type.
A function can also be called without passing an argument.

Return Statement

The return statement terminates the execution of a function and returns a value to the calling function. The program control is transferred to the calling function after return statement.
In the above example, the value of variable result is returned to the variable sum in the main() function.
Return statement of a function

Syntax of return statement

return (expression);     
For example,
return a;
return (a+b);
The type of value returned from the function and the return type specified in function prototype and function definition must match.

C Programming Functions

A function is a block of code that performs a specific task.
Suppose, a program related to graphics needs to create a circle and color it depending upon the radius and color from the user. You can create two functions to solve this problem:
  • create a circle function
  • color function
Dividing complex problem into small components makes program easy to understand and use.

Types of functions in C programming

Depending on whether a function is defined by the user or already included in C compilers, there are two types of functions in C programming
There are two types of functions in C programming:
  • Standard library functions
  • User defined functions

Standard library functions

The standard library functions are built-in functions in C programming to handle tasks such as mathematical computations, I/O processing, string handling etc.
These functions are defined in the header file. When you include the header file, these functions are available for use. For example:
The printf() is a standard library function to send formatted output to the screen (display output on the screen). This function is defined in "stdio.h" header file.
There are other numerous library functions defined under "stdio.h", such as scanf()fprintf()getchar() etc. Once you include "stdio.h" in your program, all these functions are available for use.
Visit this page to learn more about standard library functions in C programming.

User-defined functions

As mentioned earlier, C allow programmers to define functions. Such functions created by the user are called user-defined functions.
Depending upon the complexity and requirement of the program, you can create as many user-defined functions as you want.

How user-defined function works?

#include <stdio.h>
void functionName()
{
    ... .. ...
    ... .. ...
}

int main()
{
    ... .. ...
    ... .. ...

    functionName();
    
    ... .. ...
    ... .. ...
}
The execution of a C program begins from the main() function.
When the compiler encounters functionName(); inside the main function, control of the program jumps to
 void functionName()
And, the compiler starts executing the codes inside the user-defined function.
The control of the program jumps to statement next to functionName(); once all the codes inside the function definition are executed.
How function works in C programming?
Remember, function name is an identifier and should be unique.
This is just an overview on user-defined function. Visit these pages to learn more on:
  • User-defined Function in C programming
  • Types of user-defined Functions

Advantages of user-defined function

  1. The program will be easier to understand, maintain and debug.
  2. Reusable codes that can be used in other programs
  3. A large program can be divided into smaller modules. Hence, a large project can be divided among many programmers.

C goto Statement

The goto statement is used to alter the normal sequence of a C program. 

Syntax of goto statement

goto label;
... .. ...
... .. ...
... .. ...
label: 
statement;
The label is an identifier. When goto statement is encountered, control of the program jumps to label: and starts executing the code.
How goto statement works?

Example: goto Statement


// Program to calculate the sum and average of maximum of 5 numbers
// If user enters negative number, the sum and average of previously entered positive number is displayed

# include <stdio.h>

int main()
{

    const int maxInput = 5;
    int i;
    double number, average, sum=0.0;
    
    for(i=1; i<=maxInput; ++i)
    {
        printf("%d. Enter a number: ", i);
        scanf("%lf",&number);

    // If user enters negative number, flow of program moves to label jump
        if(number < 0.0)
            goto jump;

        sum += number; // sum = sum+number;
    }

    jump:

    average=sum/(i-1);
    printf("Sum = %.2f\n", sum);
    printf("Average = %.2f", average);

    return 0;
}
Output
1. Enter a number: 3
2. Enter a number: 4.3
3. Enter a number: 9.3
4. Enter a number: -2.9
Sum = 16.60

Reasons to avoid goto statement

The use of goto statement may lead to code that is buggy and hard to follow. For example:
one: 
for (i = 0; i < number; ++i)
{
    test += i;
    goto two;
}
two: 
if (test > 5) {
  goto three;
}
... .. ...
Also, goto statement allows you to do bad stuff such as jump out of scope.
That being said, goto statement can be useful sometimes. For example: to break from nested loops.

Should I or shouldn't I use goto statement?

If you think the use of goto statement simplifies your program. By all means use it. The goal here is to create code that your fellow programmers can understand easily.