Hello Everyone!
Welcome to the 7th post. This post is contributed by Sumanth Nidamanuri.
In the previous post we have discussed about switch case statements.
Basics of Programming in C (part-5):
In this post we are going to discuss Functions. This post will take less than 10 minutes to completely read and to understand it.
Function
A
computer cannot perform all the tasks by itself.So it utilizes the entities
named functions.
But
what is a function?
According to Wikipedia a function means to work or operate in a particular way.Similarly
a function in C also performs a desired task according to the user
requirements.
Definition
A function is a self
contained block of code that performs a particular task of acting on data and
may return a value.Function prototype:
Datatype
function_name(datatype variable-1,datatype variable-2,_ _ _ _)
{
Local
declarations;
Executable
statements;
Return
statement;
}
The part within the
curly braces is called the body of the program and the initial line
is called
the header.The header consists of the function name,the return type of the function and the number and the type of the arguments passed to the function.
Example:
Code:
Output:
Explanation:
As
the C program executes from top to bottom initially the main() calls the
function call_fun().This means that the control passes to function call_fun().The functioning of
main is temporarily suspended and the call_fun() starts working.When all the
statements in the call_fun() are executed then again the control passes to the
main() exactly at the point where the control has been transferred to the
call_fun().
Here
main() is the calling function and
call_fun() is the called function.
Basically
there are two types of functions.
1.user
defined functions
2.built-in
functions
User
defined functions: these are created by the user as per the requirement of the program
Built-in
functions:
these are the part f the standard library and are made available by the
compiler as a part of the compiler package.
Passing
values between functions:
Example:
#include<stdio.h>
int sum(int x,int y,int z);
int main(){
int a,b,c,result=0;
printf("\n enter the values of
a,b,c:");
scanf("%d %d
%d",&a,&b,&c);
result=sum(a,b,c);
printf("\n THE SUM OF THE NUMBERS IS %d
",result);
return 0;
}
int sum(int x,int y,int z){
int p;
p=x+y+z;
return (p);
}
Output:
enter the value of a,b,c: 2 4 9
THE SUM OF THE NUMBERS IS 15
Explanation:
The
values of a,b,c which are read from the user are passed to the function sum()
from the main().
In
the sum() these values get collected in the variables x,y,z.The variables a,b,c
are called the actual parameters and
x,y,z are formal parameters.
Any
numbers of arguments can be passed to a function.But the number of parameters
and the data type must match otherwise it results in an error.sum() returns a
value p which is of data type integer .So in the function prototype the return
type of the function sum() is declared int.
On execution of the return the
control directly passes to the calling function and the sum of the three
numbers is being returned back to the main().
The
important point to note here is the scope.The scope of the variables x,y,z is limited only between the braces of the
function sum().That means that these cannot be used outside the braces.
That’s all for this post.In next post we will discuss about recursion functions
Share it with your co-geeks and help us to improve by giving feedback in the comments section below.
Thank you.
ABOUT US
Linkedin profile: https://www.linkedin.com/in/sumanth-nidamanuri-25507b17a/
