Showing posts with label C Programming. Show all posts
Showing posts with label C Programming. Show all posts

20 November 2013

C Functions

Today, I am going to discuss about "Functions".

Functions are a group of statements that is created to perform a special task and returns a value as its result.
Example: The main() function.


From the above definition, you can get:

-Function is a group of statements.

-Function performs special tasks.
 Example: The factorial function is used to calculate the factorial of a given number.

-Function returns a value.
 Please note this: Functions return one value only no matter how many arguments it has as its input(s).


So, how do we create a function?

Use this syntax:
return_type function_name (data_type var1, data_type var2, ....){
 //statement1;
 //statement2;
 //...
return data
}

Let's go straight to creating our own function.
I would like to show you how to create a factorial function by using iteration rather than recursion.

First of all, I need to explain what factorial is and how it works.

n factorial, denoted by n!, is the factorial of n which equals to n * (n-1) * (n-2) * ... * 3 * 2 * 1.

So, 5! = 5*4*3*2*1 = 120 and 7! = 7*6*5*4*3*2*1 = 5040.

If you analyze it thoroughly, you will see that 5! can also be 1*2*3*4*5 and it still equals to 120. The same thing works for 7! too.

Therefore, we can conclude that:
n! = 1 * 2 * 3 *.... * (n-2) * (n-1) * n
We have learned about the For Loop before, now we are going to put it into use.


How are we going to calculate the factorial of n?

It's simple, all we have to do is loop from 1 to n, multiply each values respectively, store it in a variable, then return the result.


The code would be like this:
int fact(int x){
 int i=1;
 int result=1;
 for(i=1; i<=x; i++){
   result = result * i;
 }
 return result;
}

Explanation:
Integer x is the input, Integer i is the loop counter, Integer result is the value that will be returned as the result of the operation performed.

Let me simulate the flow of this program.

Let the input (x) be 5. The program will loop for 5 times.

Loop 1 :
i = 1, result = 1*1 = 1

Loop 2 :
i = 2, result = 1*2 = 2

Loop 3 :
i = 3, result = 2*3 = 6

Loop 4 :
i = 4, result = 6*4 = 24

Loop 5 :
i  = 5, result = 24 * 5 = 120 (answer)


Assuming you want to display your result, you would want to create your main function. The whole program will look like this :

#include <stdio.h>
#include <stdlib.h>


int fact(int x){
 int i = 1;
 int result = 1;
 for(i = 1; i <= x; i++){
   result = result * i; 
 }
 return result;
}


int main(){
  printf("%d\n",fact(5)); 
  system("pause"); 
  return 0; 
}

Description of the code:
The main program prints the factorial of 5. If you want it to print the factorial of different number, you can do so by changing the 5 to another number.

Please note that he result variable is an Integer and an Integer has its limit. It will not be able to calculate the factorial of large numbers. If you wish to calculate the factorial of large numbers, you might want to change the return_type of the function.

Anyway, here's a preview of the program we have just created:





Happy Coding,
Cyber Frost

27 October 2013

Variables in C

Just like any other programming languages, C also has variables.
If you do not know already, a variable is a storage location for several types of values.
There are 4 basic types of variable type specifiers in C, according to Wikipedia :

-char
-int 
-float 
-double

 
which can be combined with optional specifiers :

-signed 
-unsigned 
-short 
-long


In total, there are 28 basic data types in C. Here is the complete list along with the description: http://en.wikipedia.org/wiki/C_data_types

The syntax to declare a variable in C is simple, which is :
data_type var_name = initial_value;
Let's see how we use it in a C source code:

#include <stdio.h>
#include <stdlib.h>

int main(){
//declaring the variables
int a = 5;
int b = 2;
int c = a + b;
//printing the value of c, which is a+b, or 5+2
printf("%d",c);
//printing a new line
printf("\n");
//pausing the program to see the result
system("Pause");
return 0;
}
If you can see, I wrote several lines of words using a "//" sign in front. They are called comments. Comments will not be run by the compiler, they are only used to aid programmers to understand the source code. More about comments in the next post.

From the code you can see that I declared an Integer a with the initial value of 5 and Integer b with the initial value of 2. I then declared another Integer c with the value of a + b. Next, I print out the result, which is the value of c using printf.
You can also use other data types for different operations. Let's do division with integer and float data types for example. Here is the code :
#include <stdio.h>
#include <stdlib.h>

int main(){
//declaring the variables
float a=5;
int b=2;
float c=a/b;
//printing the value of c, which is a/b, or 5/2
printf("%f",c);
//printing a new line
printf("\n");
//pausing the program to see the result
system("Pause");
return 0;
}
The code is pretty much the same, with small modifications here and there.
If you notice, we cannot use integers as the dividend here. Why?
An Integer will always be a non-decimal number, which will give you a wrong result of the division if you declare a or c, or both, as an Integer. Ex : 5/2 = 2.

There you go. Now you can apply variables in your codes. Congratulations !
 


Happy Coding,
Cyber Frost

Basic Structure of C Programs

Every programming language has its basic structure, that is, how and where the lines of code are written.

In C, it looks like this :
#include <stdio.h>

int main(){
    //statement1;
    //statement2;
    //..........;

return 0;
}
Let's start from the top.

The #include statement is used for including a header file into your program. In this case, stdio.h.
It must precede any other lines of code in your source file.
The int is a return type specifier for the function main(). Every C program must have a main() function in order to run. Even though your program has a thousand lines of code, it will start by executing the main() function.

More about return type of main():
The main() function returns 0. which is an int as an indicator to the compiler that the program was well-executed (without error).

Then, there are these symbols, the {parentheses}, to indicate the start and the end of the main() function. Every line of code, or statement, within the parentheses must be followed by a semicolon (;).

Finally, there is the return statement, which pretty much describes its function already : to return a value from the function it represents. In this case, it is the main() function.

The End. By now you should have the basic knowledge to start writing codes in C.



Happy Coding,
Cyber Frost

Search