How to understand the c functions.
When we try to write any solution for a complex problem where certain set of statements or code needs to be called multiple times with different set of values, immediately a thought comes to mind that is there any way to call these set of statements as a single task, which may further be called in different ways and different places.
The solution to your thought process can be handled in C by using the functions as single entity where multiple tasks are arranged in way to produce a desired output. But before going into details of the fundamentals of C functions, let us have a brief description about monolithic and Modular Programming.
A function is a group of statements that together perform a task. Every C program has at least one function, which is main (), and all the most trivial programs can define additional functions. Following points will help you to understand functions in c programming language.
Functions in c programming are divided into two different types: 1. Library defined functions and 2. User defined functions.
Library functions, like scanf(), printf(), gets(), puts(), ceil(), floor(), etc., are declared in C header files. They are essential for common tasks in C programming, offering a broad range of functionalities that can be extensively accessed and utilized in various programs. These functions are pre-defined in a header file, allowing direct usage in the program without the need for redefinition. This efficiency streamlines the coding process and saves time for the programmer.
User defined functions are the functions which are created by the C programmer, so that he/she can use it many times. These functions allow for better organization of code, easier debugging, and the ability to reuse code across different parts of a program. By defining functions, programmers can encapsulate a set of instructions into a single unit, making the code more modular and easier to understand. This, in turn, leads to more efficient and maintainable code.
In C programming, no two functions have the same function name and same function declaration. That means no two functions in C have the same function prototype.
The major advantage of User defined function is that it reduces the complexity of a big program and optimizes the code.
return_type function_name (datatype1 argument1 , datatype2 argument2, ........ );
OR
return_type function_name (datatype1, datatype2, ....... );
Calling function need information about called function. If called function is place before calling function, then the declaration is not needed.
//Example
int addNumbers(int a, int b);
From the above figure of parts of functional programming int addNumbers(int a, int b); is a function prototype.
• Formal arguments are declared within the ( ) whereas local variables are declared at beginning.
• Formal arguments are automatically initialized when a value of actual argument is passed.
• Where other local variables are assigned variable through the statement inside the function body.
return type function_name (datatype argumentList) // morethan one aggument should be sapareted by comma
{
local variable;
statements ;
return (expression);
} //end function definition
Function definition can be placed anywhere in the program but generally placed after the main function. Local variable declared inside the function is local to that function. It cannot be used anywhere in the program and its existence is only within the function. Function definition cannot be nested. Return type denote the type of value that function will return and return type is optional if omitted it is assumed to be integer by default.
This is the section of a functional programming where inside any function, another function is being used.
function_name (argument_list);
or
variable = function_name (argument_list);
For example, a function named printf() defined inside head file stdio.h is called inside definition of function main().
//Example1:
#include<stdio.h>
int main()
{
prntf("hello world"); // calling a function
return 0;
}
// Example2:
void myFun ( int , int ); // function declaration
//=====================================================================
int main ( )
{
int a,b;
a= 2;
b=3;
myFun( a, b); // calling a function
return 0;
}
//=====================================================================
void myFun ( int x , int y) // function
{
int s ;
sum = x+y;
printf (“sum = %d” , s ) ;
}
There are four main categories of the functions these are as follows:
void funct (void);
int main ( )
{
funct ( );
return 0;
}
//=====================================================================
void funct ( void );
{
}
// type 1: no argument no return
void findFact(void); // function prototype declaration
int main()
{
findFact(); // calling a function
return 0;
}// end main
//=====================================================================
void findFact() // function definition
{
int num,i;
long long int fact=1;
printf("\n Enter any number ");
scanf("%d",&num);
for(i=1;i<=num;++i)
fact=fact*i;
printf("\n fact of num=%d is =%lld", num, fact);
}// end findFact
return_type function (void);
main ( )
{
datatype var= function ( );
}
//=====================================================================
return_type function ( void );
{
}
//type 2: no argument with return
long long findFact();
int main()
{
long long fct;
fct=findFact();
printf("\n factorial is = %lld",fct);
return 0;
}// end main
//=====================================================================
long long findFact()
{
int num, i;
long long int fact=1;
printf("\n Enter any number ");
scanf("%d",&num);
for(i=1;i<=num;++i)
fact=fact*i;
return fact;
}// end find fact
void function (datatype arg_list);
main ( )
{
function ( ary_list);
}
//=====================================================================
void function ( datatype ary_list );
{
}
// type 3: with argument no return
void findFact(int n);
int main()
{
int num;
printf("\n Enter any number ");
scanf("%d",&num);
findFact(num);
return 0;
}// end main
//=====================================================================
void findFact(int n)
{ int i;
long long fact=1;
for(i=1;i<=n;++i)
fact=fact*i;
printf("\n factorial is = %lld",fact);
}// end findFact
return_type function (datatype arg_list);
main ( )
{
datatype var= function ( ary_list);
}
//=====================================================================
void function ( datatype ary_list );
{
}
// type 4: with argument with return
long long findFact(int n);
int main()
{
int num;
long long fct;
printf("\n Enter any number ");
scanf("%d",&num);
fct=findFact(num);
printf("\n factorial is = %lld",fct);
return 0;
}// end main
//========================================================================
long long findFact(int n)
{ int i;
long long fact=1;
for(i=1;i<=n;++i)
fact=fact*i;
return fact;
}// end findFact
Functions in C programing is a utility to use/create library defined / user defined snippet of code which can be used in many ways. The best thing about the function is that they can be treated as “Write Once, Use Many” concept. Functions are classified as two types, one which are already comes with C compiler called Library Functions and another, which are coded by the programmer as per the need of the problem domain. In case of user defined function programmer has to do three things, a. declare b. define and c. call a function. The functions may be of anyone type of its FOUR categories, i. function with no return, no argument. ii. function with return and no argument. iii. Function with no return, with argument(s), and iv. Function with return and with argument(s).
All about java servlets Part-2 What is a website? Website is a collection of related…
Let's train the machine. Machine learning is a growing technology which enables computers to learn…
“Unless you try to do something beyond what you have already mastered, you will never…
Whenever you decide to start coding and you want to become budding coder or programmer…
C components are essential elements of a C program, including comments, preprocessor directives, functions, statements,…
All about java servlets Part-2 What is a website? Website is a collection of related…