C Tutorial – Functions and Global/Local variables

Most languages allow you to create functions of some sort. Functions are used to break up large programs into named sections. You have already been using a function which is the main function. Functions are often used when the same piece of code has to run multiple times.

In this case you can put this piece of code in a function and give that function a name. When the piece of code is required you just have to call the function by its name. (So you only have to type the piece of code once).

In the example below we declare a function with the name MyPrint. The only thing that this function does is to print the sentence: Printing from a function. If we want to use the function we just have to call MyPrint() and the printf statement will be executed. (Don’t forget to put the round brackets behind the function name when you call it or declare it).

Take a look at the example:


	#include<stdio.h>

	void MyPrint()
	{
		printf("Printing from a function.\n");
	}

	int main()
	{
		MyPrint();
		return 0;
	}

Parameters and return

Functions can accept parameters and can return a result. (C functions can accept an unlimited number of parameters).

Where the functions are declared in your program does not matter, as long as a functions name is known to the compiler before it is called. In other words: when there are two functions, i.e. functions A and B, and B must call A, than A has to be declared in front of B.

Let’s take a look at an example where a result is returned:


	#include<stdio.h>

	int Add(int output1,int output2 )
	{
		printf("%d", output1);
		printf("%d", output2);
		return output1 + output2;
	}

	int main()
	{
		int answer, input1, input2;

		scanf("%d", &input1);
		scanf("%d", &input2);

		answer = Add(input1,input2);

		printf(" answer = %d\n", answer);
		return 0;
	}

The main() function starts with the declaration of three integers. Then the user can input two whole numbers. These numbers are used as input of function Add(). Input1 is stored in output1 and input2 is stored in output2. The function Add() prints the two numbers onto the screen and will return the result of output1 + output2. The return value is stored in the integer answer. The number stored in answer is then printed onto the screen.

Void

If you don’t want to return a result from a function, you can use void return type instead of the int.
So let’s take a look at an example of a function that will not return an integer:



	void our_site()
	{
		printf("www");
		printf(".NextDawn");
		printf(".nl");
	}

Note: As you can see there is not an int before our_site() and there is not a return 0; in the function.

The function can be called by the following statement: our_site();

Global and local variables

A local variable is a variable that is declared inside a function. A global variable is a variable that is declared outside all functions. A local variable can only be used in the function where it is declared. A global variable can be used in all functions.

See the following example:


	#include<stdio.h>

	// Global variables
	int A;
	int B;

	int Add()
	{
		return A + B;
	}

	int main()
	{
		int answer; // Local variable
		A = 5;
		B = 7;
		answer = Add();
		printf("%d\n",answer);
		return 0;
	}

As you can see two global variables are declared, A and B. These variables can be used in main() and Add().
The local variable answer can only be used in main().

That’s all for this tutorial.

This entry was posted in C Tutorials. You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed. Tweet This! Tweet This! or use to share this post with others.

There are currently 23 responses to “C Tutorial – Functions and Global/Local variables”

Why not let us know what you think by adding your own comment!

  1. Karl Wilson on September 13th, 2010:

    nice tutorial – thanks.

    I think there is a typo in the “Parameters and return” section:

    “Input1 is stored in output1 and output2 is stored in Second.”

    shouldn’t that be:

    “input1 is stored in output1 and input2 is stored in output2.”

  2. admin on September 14th, 2010:

    Yes, you are right. Changed it. Thx.

  3. Mujeeb on March 9th, 2011:

    There is another typo in the “Parameters and return” code.
    anwser = Add(input1,input2);

    This should be
    answer = Add(input1,input2);

    (The spelling of answer is wrong in the 1st statement)

    This is an awesome site!!!

  4. admin on March 9th, 2011:

    @Mujeeb Thx, i’ve changed it!

  5. Xeon on June 20th, 2012:

    I think it is best to tell the usage of global variables is not recommended. Especially in a large program.

  6. ding on June 27th, 2012:

    this is more understandable than the prg. professors of seneca college.

  7. Gunasekaran on August 24th, 2012:

    very much useful…, great wrk,,,

  8. reshmy on November 26th, 2012:

    clearly understandable….. thanks

  9. Viky on December 19th, 2012:

    great teaching….;)

  10. Sayed Akram on December 30th, 2012:

    Thanks, for your great teaching…

  11. karthikeyan from peyankuzhi on January 11th, 2013:

    nice tutorial thanks

  12. Prem Gandhi on April 27th, 2013:

    Better And more understandable than profs at BITS.
    Thanks .

  13. akn on July 15th, 2013:

    nice tutorial

  14. seuli on July 17th, 2013:

    useful article ..thanks

  15. kshar on July 26th, 2013:

    Thank you for a great tutorial. My book goes on and on about functions, but never really ties anything together like you did. Examples were used well and explanation was simple and clear. Best ten minutes I ever spent. Thank you!

  16. Narmada on September 8th, 2013:

    great

  17. bhaskara on October 6th, 2013:

    thank you very much….best site

  18. venkatesh on October 12th, 2013:

    awesome !!!! btr teaching than VIT….. high five !

  19. sajjad ahmad on November 12th, 2013:

    Its good and easy way to understand tnx

  20. manasi on January 1st, 2014:

    nice , easily understandable

  21. sujata joshi on January 20th, 2014:

    thanks .. this was helpful !!

  22. Raj on March 9th, 2014:

    clearly understandable….. thanks

  23. DJ on March 25th, 2014:

    Thanx for the tutorial.It helped a lot.