Categories: C

Functions in C

A computer program cannot handle all the tasks by itself, instead, it requests other programs like entities called, functions in C, to get its task done. A function is a self-contained block of statements that perform a coherent task of some kind. Every C program can be thought of as a collection of these functions. A function can also be referred to as a method or a sub-routine or a procedure, etc.

Why use functions:
  • Writing a function avoids rewriting the same code over and over, i.e., code reusability increases.
  • By using the function, it becomes easier to write programs and keep track of what they are doing.
  • If the operation of a program can be divided into separate activities, and each activity is placed in a different function, then each could be written and checked more or less independently.
  • Separating the code into modular functions also makes the program easier to design and understand.
Types of Functions

There are two types of functions in C programming:

  1. Library Functions: are the functions which are declared in the C header files such as scanf(), printf(), gets(), puts(), ceil(), floor() etc.
  2. User-defined functions: are the functions that are created by the user (programmer), so that he/she can use them as per their requirement. It reduces the complexity of a big program and optimizes the code.
The general syntax of a function:
return-type functionName( parameters)//function definition
{
  //Body of function
  return statement;
}
Return statement:

A function in C may or may not return a value, it depends on the function declaration that whether a return type is mentioned or not. for instance, void type does not return any value, whereas, int, float, etc returns some value on function call.

Example: here we are only printing a value but there is no return type.

void hello()
{
  printf("hello coderzpy");
}

Example: here we are calculating sum of two integers and returning the sum.

int sum_of_integers()
{
  int a=9,b=1;
   sum=a+b;
  return sum;
}
Key facts :
  • A c program is a collection of one or more functions.
  • If a c program contains only one function, it is main().
  • There is no limit on the number of function present in a c program.
  • Program execution always begins from the main() function.
  • A function can be called any number of times.
  • A function can be called from another function, but it cannot be defined by another function.
Example of a simple function:
int max(int x, int y)
{
    if (x > y)
      return x;
    else
      return y;
}
  
// main function that doesn't receive any parameter and
// returns integer.
int main()
{
    int a = 10, b = 20;
  
    // Calling above function to find max of 'a' and 'b'
    int m = max(a, b);
  
    printf("m is %d", m);
    return 0;
}
output:
m is 20

In the above code, we have created a function to calculate the max value among two integers and return it. The max function is called inside the main function and the returned value gets printed.

Note: also read about the Jump statements in C.

Follow Me

If you like my post please follow me to read my latest post on programming and technology.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Share
Published by
Rabecca Fatima

Recent Posts

What is object oriented design patterns

A design pattern is a reusable solution to a commonly occurring problem in software design. They…

5 months ago

Factory Method Design Pattern in OODP

Factory Method is a creational design pattern that deals with the object creation. It separates…

6 months ago

Find Intersection of Two Singly Linked Lists

You are given two singly linked lists that intersect at some node. Your task is…

11 months ago

Minimum Cost to Paint Houses with K Colors

A builder plans to construct N houses in a row, where each house can be…

11 months ago

Longest Absolute Path in File System Representation

Find the length of the longest absolute path to a file within the abstracted file…

12 months ago

Efficient Order Log Storage

You manage an e-commerce website and need to keep track of the last N order…

1 year ago