Learn C Programs
Monday, 20 March 2017
Sunday, 19 March 2017
User defined Functions
Function Declarations
Prototype to perform this task, a user-defined function add Numbers () is
defined.
#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
}
A function Declarations is simply the declaration of a function that specifies function's
name, parameters and return type. It doesn't contain function body.
A function Declarations gives information to the compiler that
the function may later be used in the program.
Syntax of function Declarations
Declarations In the above example,
int addNumbers(int a, int b);
is the function Declarations which provides following information to
the compiler:
1. name
of the function is addNumbers()
2. return
type of the function is int
3. two
arguments of type int are passed to the function
The function Declarations is not needed if the user-defined
function is defined before the main() function.
int addNumbers(int a, int b);addNumbers()intint are passed to the functionmain() function.
Calling a
function
Control of the program is transferred to
the user-defined function by calling it.
Syntax of function call
functionName(argument1, argument2, ...);
In the above example, function call is made using addNumbers(n1,n2); statement
inside the main().
addNumbers(n1,n2); statement
inside the main().
Function
definition
Function definition contains the block
of code to perform a specific task i.e. in this case, adding two numbers and
returning it.
Syntax
of function definition
returnType
functionName(type1 argument1, type2 argument2, ...)
{
//body of the function
}
When a function is called, the control
of the program is transferred to the function definition. And, the compiler
starts executing the codes inside the body of a function.
Passing arguments to a function
In programming, argument refers to the
variable passed to the function. In the above example, two variables n1 and n2 are passed during function call.
The parameters a and b accepts the passed arguments in the
function definition. These arguments are called formal parameters of the
function.
Friday, 17 March 2017
Functions
Function Definition: Function is a group of
statements which performs a specific task. It is also known as Sub-routine or
Procedure or Method.
return_type functions name(arguments list)
{
body of the functions
}
Here are all the
parts of a function −
- Return Type − A function may
return a value. The return_type is the data type of the
value the function returns. Some functions perform the desired operations
without returning a value. In this case, the return_type is the
keyword void.
- Function Name − This is the actual
name of the function. The function name and the parameter list together
constitute the function signature.
- Parameters − A parameter is like
a placeholder. When a function is invoked, you pass a value to the
parameter. This value is referred to as actual parameter or argument. The
parameter list refers to the type, order, and number of the parameters of
a function. Parameters are optional; that is, a function may contain no
parameters.
- Function Body − The function body
contains a collection of statements that define what the function does.

· There are two kinds
of c functions:
1. Library
Functions (also known as System defined function)
1. Library
Functions are used to perform standard operations eg: squreroot of a number
sqrt(x), absolute value fabs(x),scanf(),printf(),and so on.These functions are
available alog with the compiler and are used along with the required header
files such as math.h, stdio.h, string.h, and so on at the beginning of the
programe.
2. User
defined functions are self-contained blocks of statement which are written by
the user to compute a value or to perform a task.They can be called by
the main() function repeatedly as per the requirement.
Friday, 10 March 2017
Wednesday, 1 March 2017
Even And Odd Numbers
- Two methods available in c programing...
#include <stdio.h>
int main()
{
int i;
printf("Enter an integer: ");
scanf("%d", &i);
if(i % 2 == 0)
printf("%d is even.", i);
else
printf("%d is odd.", i);
return 0;
};
Output :
(2) Methods Condition Operator
Program :
#include <stdio.h>
int main()
{
int A;
printf("Enter an integer: ");
scanf("%d", &A);
(A % 2 == 0) ? printf("%d is even.", A) : printf("%d is odd.", A);
return 0;
}
Output :
Sunday, 19 February 2017
c program to sort an array using function
Program :
#include<stdio.h>
#include<conio.h>
#define MAX 100 // maximum no of elements of array
int sortArray(int);
int array[MAX];
int main()
{
int i,size;
printf("\n>>>> PROGRAM TO SORT ARRAY USING FUNCTION <<<<\n\n");
printf("\n Enter the size of array: ");
scanf("%d",&size);
printf("\n Enter the %d elements of array: \n",size);
for(i=0;i<size;i++)
{
printf("\n array[%d]=",i);
scanf("%d", &array[i]);
}
sortArray(size); //calling sortArray() function
printf("\n The Sorted elements of array are:");
for(i=0;i<size;i++)
{
printf(" %d",array[i]);
}
getch();
return 0;
}
sortArray(n) // function for sorting array elements
{
int temp=0,i,j; // temp var is temporary variable used for swapping
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(array[i]>array[j])
{
temp=array[i]; //swapping for the array to be sorted
array[i]= array[j];
array[j]=temp;
}
}
}
}
Output :
#include<stdio.h>
#include<conio.h>
#define MAX 100 // maximum no of elements of array
int sortArray(int);
int array[MAX];
int main()
{
int i,size;
printf("\n>>>> PROGRAM TO SORT ARRAY USING FUNCTION <<<<\n\n");
printf("\n Enter the size of array: ");
scanf("%d",&size);
printf("\n Enter the %d elements of array: \n",size);
for(i=0;i<size;i++)
{
printf("\n array[%d]=",i);
scanf("%d", &array[i]);
}
sortArray(size); //calling sortArray() function
printf("\n The Sorted elements of array are:");
for(i=0;i<size;i++)
{
printf(" %d",array[i]);
}
getch();
return 0;
}
sortArray(n) // function for sorting array elements
{
int temp=0,i,j; // temp var is temporary variable used for swapping
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(array[i]>array[j])
{
temp=array[i]; //swapping for the array to be sorted
array[i]= array[j];
array[j]=temp;
}
}
}
}
Subscribe to:
Comments (Atom)



