Loading [MathJax]/jax/output/HTML-CSS/fonts/TeX/fontdata.js

In today's lesson, we will learn about functions. In a program, a function is a group of statements that perform a specific operation.

So far, in each and every C program, we started with a void main().

If you look carefully, you'll notice that the statements contained within main()perform a specific operation. So, going by the definition of a function, main() is also a function.

Notice the brackets next to main. Where else have you seen round brackets just next to a keyword?

Yes, that's right! printf() is another such example. Well, printf() is a function as well. But there's a difference between main() and printf(). Can you guess what it is?

We don't know what the statements that make up printf() are, but we do know what the statements within main() are. This is because the contents of printf()are stored within the stdio.h that we include in every program. It is what we call apre-defined function.

You can create your own functions as well. These are called user-defined functions. Let us create a function that will find out the square of a number—that is, the number multiplied by itself.


#include<stdio.h>

void square();

void main()
{
    int num=5;
    square(num);
}

void square(int num)
{
    int result;
    result = num * num;
    printf("The result is %i", result);
}
http://codepad.org/QLUCPxB7

 

The line void square(); tells the computer that we are about to work with a function called square(). We don't do this with the main(), but we need to write this line for every function that we build on our own; otherwise, the computer will not be able to recognize our function.

The line square(num); means that we are sending the value of num to the functionsquare().

We write the contents of square() in a manner similar to the way we write the contents of main(). The int num inside the round brackets next to void squaremeans that square() has to accept an int (integer) value in the variable callednum.

But we have already declared a variable called num in main(), you say. Why do we have to do this again? Well, that's because whatever you do inside main() stays inside main(). The moment you are outside main(), the values that you had worked with are lost, unless you specifically send a value to another function, which is, in our case, square().

So, the num variable has no existence outside main(). The num variable insquare() is a new variable that is being assigned the same value as the num ofmain(). You can use a different name for the num variable of square() and your program will work just as well: http://codepad.org/I7d4HIJ6.

What square() does essentially is multiply the given number by itself and display the final answer using yet another function, which is printf(). So, we see that a function can call another function to do its bidding.

In tomorrow's lesson, we will learn more about functions. Till then, happy coding!

Hey there!

Yesterday's lesson was all about functions. Today, we are going to look at some important pre-defined functions like printf() that will really come in handy for your programs.

Remember I told you that we need to include stdio.h if we want to useprintf()? Well, it is quite the same for other pre-defined functions too. For the next pre-defined function, we will need to include math.h.


#include<stdio.h>
#include<math.h>
void main()
{
    int num = 25;
    int result;
    result = sqrt(num);
    printf("%i",result);
}
http://codepad.org/EKkuGvev


The pre-defined function here is sqrt(). The sqrt() finds out the square root of a number. We have to indicate the number whose square root we want to find within the round brackets.

Another common pre-defined function is pow().


#include<stdio.h>
#include<math.h>
void main()
{
    int num = 10, power = 2;
    int result;
    result = pow(num, power);
    printf("%i",result);
}
http://codepad.org/5je45P5U


pow() finds out the result of a number raised to a given power. The number we use as a base is to be written first, followed by the power to which we want to raise it.

For working with strings, we need to include string.h at the beginning of our program.


#include<stdio.h>
#include<string.h>
void main()
{
    char word[5]={'h','a','p','p','y'};
    int result;
    result = strlen(word);
    printf("%i", result);
}
http://codepad.org/yHCGc8W6


strlen() finds out the length of the string, which is the number of characters or alphabets in the string. In the above example, it might seem obvious that the length of the string is 5, because we have taken a character array of size 5. However, it is useful when you are working with a large character array that is storing a string of any size that is less than the array size.


#include<stdio.h>
#include<string.h>
void main()
{
    char word[10]={'h','a','p','p','y'};
    int result;
    result = strlen(word);
    printf("%i", result);
}
http://codepad.org/zZSrlqvn


There are hundreds of pre-defined functions in C language that will aid you in simplifying the way you code. You don't have to reinvent the wheel—just use it. Some other .h files of interest to you are math.h and stdlib.h.

As You Like It

Diego Melo
Module by Diego Melo, updated more than 1 year ago
No tags specified