C Programming IT Programming Tutorials

Functions in C

Functions in C are defined as a block of statements that perform a specific task and can be executed repeatedly any number of times.

Every C program has a main function and in addition to that, there can be many user-defined functions.

Types of Functions in C:

1. Built-in or predefined function

2. User-defined functions

Predefined functions in C

The standard functions defined in the library are known as predefined functions.
Some of the examples of the predefined functions are the main function, printf, scanf functions defined in stdio.h library. The functions strlen, strcpy, strcat are defined in string.h. Functions such as sqrt, pow, ceils, and log are defined in math.h

User-Defined Functions in C

All the functions which are defined by the user to split the large code and reuse the code are known as the user-defined functions.

All functions have 

1. Function Declaration

2. Function Call and

3. Function Definition

The syntax to write a function call, declaration and definition are as follows

Declaration of Functions in C

 Syntax:

return-type function-name(datatype var-name);

 example:

void square(int i);
or 
void product(int,int);

1. Return-type is nothing but the data-type such as void, int, float, etc.

 2. Function name is the name given to the function that can be any name, but it is always good to use the name that represents the process involved in the function.

 3. The function name must have a data type and variable name(optional) inside the brackets. 

 4. The function declaration informs the compiler about the function name, its return type, and its process.

Function Call

Syntax:

  function-name(par1,par2);

  example:

 max(a,b);
 largest(x, y, z);

There are two ways to pass parameters to the function.

Call By Value

This method copies values of the actual argument to formal parameters. Changing the value inside the function does not have any impact on the actual parameter.

Call By Reference

In this method, the address of the variable is passed to formal parameters and the value inside the address is accessed and processed, so any change made will have an impact on the original value passed.

 All the functions explained below are examples for Call By Value, we will learn about 

Call By Reference in the future while learning the pointer concept.

Defining Functions in C

   Syntax:

       return-type function-name(datatype varname);
        {
         body of the function;
        } 

 example:

        int average(int a, int b, int c)
         {
          int z=(a+b+c)/3;
          printf("%d",z);
          }

Types of user-defined functions in C:

     1. Function with arguments and without return-type.

     2. Function with argument and with return-type.

     3. Function without argument and with return-type.

     4. Function without argument and without return-type.

Function with arguments and without return-type

Example 1:

Explanation:
In the above code we declare three variables of integer type, then first printf, prints the string given within the quotes, then we get three values from the user in the same line using scanf statement.

To get the grades of marks stored in a variable we pass the integer value of each number to the function, we call getgrades function, and pass variable a.
Once the function is called, the program execution flow jumps to function definition, in parameter x (formal parameter) we receive the value of ‘a'(actual parameters) and it checks with the condition, here it is 30 which is less than 35, then fail and d grade will be printed.

The program execution flow returns back to the position where the function is called and the second function call will be executed with b as an argument and moves to the function definition and x is assigned with b value and then the condition is checked and the same process is repeated.

Function with arguments and with the return type

Example 2:

Explanation:

The entered character is passed to the function definition and checked using the conditional expression.

The expression checks entered character ASCII value is between ASCII value of a and z, if true the difference between ASCII value of entered character and ‘a’ is obtained, which when added with ‘A’ gives equivalent uppercase character.

If the condition is false, the statement after the colon( 🙂 will be executed, here it returns the value of l, entered a character, and assigns to a variable uppercase.

Function without arguments and with the return type

Example 3:

Explanation:

The working of the function is similar to previous examples, the only difference is that the average function has no arguments.
It returns an average which is of float type to the main function and prints the value.

Function without arguments and without the return type

Example 4:

example:

void initialize();
void getinput();

The void type function does not return any value and no parameters are passed to the function.

Recursion Functions in C:

The function that calls itself multiple times until certain conditions are satisfied is known as the recursion function. If proper exit condition is not provided the loop continues infinite times.

The recursion functions are used in mathematical calculations.

Syntax:

void recursion(int x)
 {
   if(x!=0)
   {
    printf("%d",x);
   recursion(--x);
    }
 }
 void main()
  {
   int i=5;
   recursion(i);
  }

Example 5:

Explanation:
The first printf statement displays enter a value, then input integer value is received from the user in the variable no and then the function is called with no and temp variable as parameters.
Now the program execution flow jumps to the function definition and the values passed from the actual parameters no and temp are passed to the formal parameters x and y.

The x value is divided by 10, which gives 15632, which is stored in the quotient, and x %10 gives the remainder which is 4.
To check whether each digit is odd or not we divide the remainder value by 2, if it is not equal to zero, then it is an odd number, we need to add all the odd numbers, so we add y(a temporary variable that holds oddnototal, initially y is assigned 0 ) to remainder, else we add simply y to 0, the process repeats until the quotient is not zero.

When the quotient becomes zero, else part is executed, the remainder is checked and added to oddnototal but the function is not called, hence it prints q, r, and oddnototal and returns to the place from where the function is called and assigns 9 to sum and prints the sum of odd numbers.

Storage Classes in C

In C Programming storage classes are used to determine four important factors of variables such as their lifetime, visibility(scope), memory location in which the variable is stored, and initial value.
The Different storage classes available in C programming are 

auto, static, register, and extern

Different Storage classes in C

 Auto Storage Class

  • All local variables are automatic variables by default.
  • The memory for the auto variables is created during the run time and freed automatically during exit from the block.
  • The automatic variables are referred to by the keyword auto.

Example 1:

 Explanation:

In the above example, both auto int z and int z in printfunction belongs to the same storage class “auto”. The value of z in the main function is 10 and its memory is freed when it exits from the block.

Now in the printfunction, the value of z is 20, only within that function.
Again when we print the value of z inside the while, it prints z=9, since the local value of z is 9 within that block.

Static Storage Class

  • The static variable retains its values between the different function calls.
  • The global variables can also be declared static which extends their visibility until the end of the file.
  • The static keyword is used to represent the static variables. 

Example 2:

Explanation:

 Since the count is declared as a static variable, it retains value between the functions. The value of the count is initialized to 5, then it is post decremented so now the value of the count will be 4.
   Now the execution flow moves to func function and checks the condition, and prints the value of the count which is 4. The same process continues until the count becomes zero.

Register Storage Class

  • The register variables are local variables that are stored in registers instead of RAM.
  • Only the variables that need to be accessed faster are used as register variables.
  • The & operator cannot be used with register variables since memory is not allocated and can have the maximum size equal to that of register size. 
  •  Syntax: register int x=0;

Extern Storage Class

  • The extern storage class is used when the same variable or function is used in multiple files.
  • The variable declared as an extern is used as a reference to denote that the variable is declared elsewhere in the program.
  • The extern variable can be declared multiple times but initialized only once. 
  •  Syntax:extern int

Example 3:

Explanation:

The variable is declared with the keyword extern since it is initialized in the examplefn and extern_egfn is defined in the externfn.h file so when the externfn is included in the main function, it will produce the result x is 5.

Recommended Articles

4 Comments

  1. Hi there, for all time i used to check website posts here early in the morning, because i enjoy to gain knowledge of more and more.|

  2. Oh my goodness! Awesome article dude! Thank you, However I am going through troubles with your RSS. I don’t know why I am unable to subscribe to it. Is there anybody else having identical RSS issues? Anybody who knows the answer will you kindly respond? Thanks!!|

Leave a Reply

Your email address will not be published. Required fields are marked *