Sunday, March 31, 2019

Basics of Programming in C (part-6)


Hello Everyone!


Welcome to the 7th post. This post is contributed by Sumanth Nidamanuri.

In the previous post we have discussed about switch case statements.
Basics of Programming in C (part-5):

In this post we are going to discuss Functions. This post will take less than 10 minutes to completely read and to understand it.

Function
A computer cannot perform all the tasks by itself.So it utilizes the entities named functions.
But what is a function?
According to Wikipedia a function means to work or operate in a particular way.Similarly a function in C also performs a desired task according to the user requirements.  

Definition
A function is a self contained block of code that performs a particular task of acting on data and may return a value.

Function prototype:
Datatype function_name(datatype variable-1,datatype variable-2,_ _ _ _)
{
            Local declarations;
            Executable statements;
            Return statement;

}
The part within the curly braces is called the body of the program and the initial line 
is called the header.The header consists of the function name,the return type of the function and the number and the type of the arguments passed to the function.  

Example:

Code:


Output:

Explanation:


As the C program executes from top to bottom initially the main() calls the function call_fun().This means that the control passes  to function call_fun().The functioning of main is temporarily suspended and the call_fun() starts working.When all the statements in the call_fun() are executed then again the control passes to the main() exactly at the point where the control has been transferred to the call_fun().

Here main() is the calling function and call_fun() is the called function.

Basically there are two types of functions.
1.user defined functions
2.built-in functions

User defined functions: these are created by the user as per the requirement of the program


Built-in functions: these are the part f the standard library and are made available by the compiler as a  part of the compiler package.

Passing values between functions:
Example:
#include<stdio.h>
int sum(int x,int y,int z);
int main(){
  int a,b,c,result=0;
  printf("\n enter the values of a,b,c:");
  scanf("%d %d %d",&a,&b,&c);
  result=sum(a,b,c);
  printf("\n THE SUM OF THE NUMBERS IS %d ",result);
  return 0;
}
int sum(int x,int y,int z){
  int p;
  p=x+y+z;
  return (p);

}

Output:

enter the value of a,b,c: 2 4 9

THE SUM OF THE NUMBERS IS 15

Explanation:
The values of a,b,c which are read from the user are passed to the function sum() from the main().

In the sum() these values get collected in the variables x,y,z.The variables a,b,c are called the actual parameters and x,y,z are formal parameters.

Any numbers of arguments can be passed to a function.But the number of parameters and the data type must match otherwise it results in an error.sum() returns a value p which is of data type integer .So in the function prototype the return type of the function sum() is declared int.

On execution of the return the control directly passes to the calling function and the sum of the three numbers is being returned back to the main().

The important point to note here is the scope.The scope of the variables x,y,z  is limited only between the braces of the function sum().That means that these cannot be used outside the braces.

That’s all for this post.In next post we will discuss about recursion functions

Share it with your co-geeks and help us to improve by giving feedback in the comments section below.

Thank you.

 ABOUT US
SUMANTH NIDAMANURI, B.tech SECOND YEAR  COMPUTER SCIENCE AND ENGINEERING STUDENT AT ANITS and SUJITH SAGAR, B.tech SECOND YEAR COMPUTER SCIENCE AND ENGINEERING STUDENT AT NIT DELHI.


Basics of Programming in C (part-5)


Hello Everyone!


Welcome to my 6th post. First of all, I should thank my parents, sister and my friends for supporting me and encouraging me.

In the previous post we have discussed about switch case statements.
Basics of Programming in C (part-4):

In this post we are going to discuss for loops and while loops. This post will take less than 5 minutes to completely read and to understand it.

Loop statements

Loop statements are used when we want to execute some line of code repeatedly. 
For loops are used when we know the number of times we run the loop. 
while loops are used when we are not sure about the number of times we run the loop.

Syntax:

FOR loop:

for(counter_variable initialization; for_condition; counter_variable increment/decrement )
{
        statements;
}


WHILE loop:

while(while_condition)
{
      statements;
}

Do WHILE loop:

do
{
      statements;
}while(while_condition);


In For loop parenthesis we have three parts. First is for initialization, Second is for condition and third is to increment or decrement  the counter variable.

First the variable is initialized. Then the condition is checked. If the condition evaluates to be true then compiler will execute the statements inside the loop block. after execution, it will increment/decrement the counter variable and will check the condition again. if condition is true then executes the block statements, otherwise the flow of control comes out of the loop and continues to execute the statements outside the loop.

Similarly in WHILE loop also, first the condition is evaluated. If it is true then statement inside the block are executed else loop is terminated. After executing the statements inside the loop compiler will check for the while condition.

In DO WHILE loops, the while condition is evaluated after executing the statements for the first time. Therefore, in Do while loops even if the condition evaluates to be false the statements are executed once.

Note: In both types of loops we need to take care of termination condition. Otherwise, the loop runs infinitely many times(😈).

Example:
Code:


Output:



That’s all for this post. In the next post we’ll discuss about  Functions.

Share it with your co-geeks and help me to improve by giving feedback in the comments section below.

Thank you.

 ABOUT ME






















MYSELF SUJITH SAGAR. I AM A B.tech SECOND YEAR  COMPUTER SCIENCE AND ENGINEERING STUDENT AT NATIONAL INSTITUTE OF TECHNOLOGY DELHI.

Basics of Programming in C (part-4)


Hello Everyone!


Welcome to my 5th post. First of all, I should thank my family and my friends for supporting me and encouraging me.

In the previous post we have discussed Conditional statements.
Basics of Programming in C (part-3):

In this post we are going to discuss about switch case statements. This post will take less than 5 minutes to completely read and to understand it.

Switch case

Switch case changes the direction of flow of control with respect to the input. The condition in switch statement is compared with case statements in the switch block.when the switch condition and case condition both are equal, the statements in that specific case block are executed. Using default statement is optional but it is preferred to handle errors when switch condition does not matches with any of the case conditions.

Therefore, the statements in the default block executes only if the switch condition does not matches with any of the case conditions.

Using break statement is optional.

Note: If there is no break statement in case blocks then after executing the necessary  case block instead of exiting the switch block compiler executes all other case block which are followed by the necessary case block. We should use break statement in each case block in order to prevent the flow of control in entering into next case block. 




Flow chart:





















Syntax :

 switch(choice_variable)
{
          case case_variable-1:
          Statements-A;
          break;

          case case_variable-2:
          Statements-B;
          break;
          .
          .
          .
          case case_variable-n:
          Statements;
          break;
           
          default:
          Statements;
}


Example:
Code:


Output:



That’s all for this post. In the next post we’ll discuss about  for loops.

Share it with your co-geeks and help me to improve by giving feedback in the comments section below.

Thank you.

 ABOUT ME






















MYSELF SUJITH SAGAR, B.tech SECOND YEAR  COMPUTER SCIENCE AND ENGINEERING STUDENT AT NATIONAL INSTITUTE OF TECHNOLOGY DELHI.


How Can You Test and Revert a Failover in Azure Cache for Redis Premium with Geo-Replication?

If you're using Azure Cache for Redis Premium with geo-replication across different regions, knowing how to test failover and revert it ...