Today we shall learn about functions. It is a very simple and very useful concept. Pls see the images below.


Function is just a name that we give to a block of code.
And whenever we want to execute that code, instead of writing the entire code, we can simply call the function by its name, and the code will get executed.

Ler’s see an example:

#include <stdio.h>
int main()
{
int a = 10, b = 5;
printf("Sum = %d\n", a + b); // display sum
printf("Thanks\n");
printf("Have a nice day\n"); // display thank you message
printf("Difference = %d\n", a - b); // display difference
printf("Thanks\n");
printf("Have a nice day\n"); // display thank you message
printf("Product = %d\n", a * b); // display product
printf("Thanks\n");
printf("Have a nice day\n"); // display thank you message
return 0;
}
output:
Sum = 15
Thanks
Have a nice day
Difference = 5
Thanks
Have a nice day
Product = 50
Thanks
Have a nice day

After each arithmetic computation, we are printing thank you messages. The code for that is repeated multiple times in our program.

printf("Thanks\n");
printf("Have a nice day\n");
...
printf("Thanks\n");
printf("Have a nice day\n");
...
printf("Thanks\n");
printf("Have a nice day\n");

Same code repeated multiple times!


Wouldn’t it be nice if we could write the code for that just once, give it some name or label, and whenever we want, we can call it by its name?

No need to write the same code again and again!


void ThankYouMessage()
{
printf("Thanks\n");
printf("Have a nice day\n");
}

What we have written above is known as function definition.
We’re defining a function containing the code we saw earlier.
We’ve given the function the name ThankYouMessage(). (We can give any appropriate name that we like.)
The function doesn’t return anything. Hence, ‘void’ is written as the return type of the function.

When we call ThankYouMessage();, it will print “Thanks” and “Have a nice day”.


Program without function calls (same code is repeated multiple times)

#include <stdio.h>
int main()
{
int a = 10, b = 5;
printf("Sum = %d\n", a + b);
printf("Thanks\n");
printf("Have a nice day\n");
printf("Difference = %d\n", a - b);
printf("Thanks\n");
printf("Have a nice day\n");
printf("Product = %d\n", a * b);
printf("Thanks\n");
printf("Have a nice day\n");
return 0;
}

Same program, written using function calls. (Neater, and code is not repeated multiple times)

#include <stdio.h>
void ThankYouMessage()
{
printf("Thanks\n");
printf("Have a nice day\n");
}
int main()
{
int a = 10, b = 5;
printf("Sum = %d\n", a + b);
ThankYouMessage();
printf("Difference = %d\n", a - b);
ThankYouMessage();
printf("Product = %d\n", a * b);
ThankYouMessage();
return 0;
}

output:
Sum = 15
Thanks
Have a nice day
Difference = 5
Thanks
Have a nice day
Product = 50
Thanks
Have a nice day

Both programs produce the same output.


This is function definition.
void ThankYouMessage()
{
printf("Thanks\n");
printf("Have a nice day\n");
}

Just because we defined a function doesn’t mean that the code within it gets executed.
In order to execute that code, we need to call the function by its name.
We’ve done that in lines 14, 17 and 20 of the above program.

Whenever we call a function, the code within the function definition gets executed.

How to call a function?
Write the function name, followed by parentheses, followed by semicolon.
That’s the way to call a function.
For example, ThankYouMessage(); —> This is a function call.


Another example of a program using functions:
Program with a greet function.

#include <stdio.h>
void greet() // function definition
{
printf("Good morning\n");
printf("Jai Sriman Narayana\n");
}
int main()
{
greet(); // function call
greet(); // another function call
printf("Thank you\n");
return 0;
}
output:
Good morning
Jai Sriman Narayana
Good morning
Jai Sriman Narayana
Thank you
notes:
  • The above program has a greet function.
  • The name of the function is greet().
  • It is defined at the beginning of the program before main().
  • It is called two times within main().
  • Note: A function is defined once, but can be called multiple times.
  • Each time that the function is called, the computer runs the code written within the function definition.

Summary:

  • Function is just a name that we give to a block of code.
  • Whenever we want to run that code, we simply call the function by its name.
  • A function is defined once, and can be called many times as we want.
  • Functions help to make the program neater, easier to understand, and easier to maintain.
Posted in

Leave a comment

Is this your new site? Log in to activate admin features and dismiss this message
Log In