Programming in C—Introduction to Functions.steemCreated with Sketch.

in hive-175254 •  5 years ago 
Hello Everyone

images.jpeg

Image Source

I hope you all are fine and safe inside your homes. This is a weird time ongoing in the whole world and I hope it will get over soon. As during this time, everyone is locked into their homes. I want to share the C programming language with you. I hope you guys like it, so let's happen today's topic.

Introduction to Functions

A function could be a block of code that performs a selected task.
Suppose, a program associated with graphics has to create a circle and color it depending
upon the radius and color from the user. you'll be able to create two functions to resolve this
problem:

  • create a circle function
  • color function
    Dividing complex problem into small components makes program easy to grasp and use.
Types of functions in C programming:

Depending on whether a function is defined by the user or already included in C compilers, there are two sorts of functions in C programming.
There are two sorts of functions in C programming:

  • Standard library functions
  • User defined functions
Standard library functions:

The standard library functions are built-in functions in C programming to handle tasks such as mathematical computations, I/O processing, string handling etc.
These functions are defined within the header file. after you include the header file, these functions are available to be used.
For example:
The printf() could be a standard library function to send formatted output to the screen
(display output on the screen). This function is defined in "stdio.h"header file.
There are other numerous library functions defined under "stdio.h", such as scanf(), fprintf(), getchar() etc. Once you include "stdio.h" in your program, all these
functions are available to be used.

User-defined functions:

As mentioned earlier, C allow programmers to define functions. Such functions created by the user are called user-defined functions.
Depending upon the complexity and requirement of the program, you'll be able to create as many user-defined functions as you wish.

How User-Defined Functions Work?
#include <stdio.h>
void functionName()
{
 ... .. ...
 ... .. ...
}
int main()
{
 ... .. ...
 ... .. ...
 functionName();
 
 ... .. ...
 ... .. ...
}

In C programing, Execution begins from main() function. When compiler encounters functionName(); inside main function, control of the program moves to

Void functionName()

Now the compilers starts executing code inside the user-defined function.
The control of the program jumps to statement next to functionName(); once all the
codes inside the function definition are executed.
A function could be a block of code that performs a selected task.
C allows you to define functions consistent with your need. These functions are called user-defined functions. For example:
Suppose, you would like to make a circle and color it depending upon the radius and color.
You can create two functions to resolve this problem:

  • createCircle() function
  • color() function
Example—Using User-Defined function:

Addition of two integers:

#include <stdio.h>
int addNumbers(int a, int b); // function prototype
int main()
{
 int n1,n2,sum;
 printf("Enters two numbers: ");
 scanf("%d %d",&n1,&n2);
 sum = addNumbers(n1, n2); // function call
 printf("sum = %d",sum);
 return 0;
}
int addNumbers(int a,int b) // function definition 
{
 int result;
 result = a+b;
 return result; // return statement
}

Logo.png

Thank you.

I hope you guys liked my post.

Keep Supporting.

STAY TUNED FOR NEXT POST

UPVOTECOMMENTRESTEEM
IF YOULIKEDMY POST

Logo.png

Created by @zord189

Stay Home, Stay Safe

@peerzadazeeshan

PEACE✌️✌️

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!