Study about Functions in C Programming - Codzify

3 min 7 sec read Basic


Functions in C programming are defined as simply a set of statements which are executed one by one. Every program in C contains various functions which perform a particular task defined.

Assume Functions in C language like,

Inside IT Companies there are different persons performing different functions like managers Function is different, Employees Function is different. Each Person performs his own task. Similarly, In Programming Language Functions are different blocks of statements which performs their own tasks.

Syntax

returnType functionName()
{
  statement 1;
  statement 2; 
}

returnType indicates the type that function returns. Type may be Integer , Float ,Double , void etc.

functionName() indicates the name of user-defined function.

statement 1; statement 2 indicates the statements you want to execute.

void main()
{
  displayMessage ();
  printf("Hey ! This is my first function :-: ");
}

displayMessage()
{
 printf("Yeppiee ! Main Method Called me :-: ");
}

Output:-

Yeppiee ! Main Method Called me :-: 
Hey ! This is my first function :-:

This was the very basic example for understanding what actually function does .

Main() Function

This is the entry point of any C Program. This is where the program starts. Yeah, of course, you may write every 10's of thousands of lines inside the main. But think Is it readable later in future ? So why not to break those 1000 lines statements into Functions and let that function handle the statements. That's why Functions are introduced into C Language.

Program showing how to perform function declaration

#include 
int main( )
{
    /* This is how you declare a function */
    float square (float) ;
    
    float a, b ;
    printf ("
Enter any number ") ;
    scanf ("%f", &a) ;
    
    b = square (a) ;
    printf ("
Square of %f is %f", a, b) ;
    return 0;
}

/* Function Square with float returnType */
float square (float x)
{
    float y ;
    y = x * x;
    return (y) ;
}

Output

Enter any number 1.5
Square of 1.5 is 2.250000 

Enter any number 2.5 
Square of 2.5 is 6.250000

Function Definition in C Programming

Function defination provides the actual body of the function. The statements we had written inside square function is called as providing the body to any function.So for square function , Function Defination will look like this

float square ( float x )
{
float y ;
y = x * x;
return (y) ;
}

Codzify's

1:1 & 1:Many Mentorship Program

Be job-ready. Learn how to crack the interviews with industry ready training program.


Learn More

Data Structures & Algorithms

What is Dynamic Programming and how to use it

This is a quick introduction to Dynamic Programming.

3 ways to check anagrams

Let's discuss the 3 ways to check anagrams and their time complexity.

Bubble Sort Algorithm

Topics covered: 1) Basics of Bubble Sort Algorithm 2) Worst Case, Best Case complexity

Introduction to Trees

What is a Tree and Terminologies like root, sibling, parent node, children, Leaf node, Ancestor & Descendant, Properties of Tree Data Structure, Depth of node, Height of node

Address Calculation in 2d array

Let's discuss about the Address Calculation using row-major order & column-major order.

Javascript

Javascript - Postfix Vs Prefix

Let's discuss the difference between postfix and prefix with a practical example.

Javascript Identifiers, Variables

In this session, we will discuss the concept of identifiers, variables and operators in javascript with a practical example.

Bootstrap

Bootstrap 4 Tutorial: Modal Popups Made Easy | Live Demo

Let's discuss the Modal Popups in Bootstrap.

How to create a dropdown list in navbar bootstrap

In this video, we will discuss How to create a dropdown list in navbar bootstrap with a practical example.

Navigation tabs in Bootstrap

Let's discuss about the navigation tabs in Bootstrap

Bootstrap Buttons

Let's discuss about the button classes in bootstrap.


Recommended tutorials