Engineering Research Project Proposal 2016
Engineering Research Project Proposal 2016
Topics included:
User-Defined Functions
Standard Library Functions
Recursive Functions
Comparison of Recursion and Iteration
I. User-Defined Functions:
C functions can be classified in two categories: Library functions and User defined functions.
The difference between this two functions is, Library functions are already built in and we
need not to write its code, whereas a User defined function has to be written to get it executed
during the output.
Syntax :
1) Function Header :
The first line in the above given syntax is known as header function. The function header
consists of three parts: function type, function name and the function parameter list.
-Function type: This may consist of the datatypes that you use. For example float, int, double.
NOTE: If datatype is not specified then C will assume it as int type. And if the function does
not return any value then use void.
Programming with C Lecture Notes on Functions:
-Function name: This may consist of any variable that is suitable for user's understanding. For
example: If you have made user defined function for factorial then use fact and if for simple
multiplication then mul.
-Parameter List: It declares the variables that are to be used in the function and that are going
to be called in the program
2) Function Body:
The function body contains the declaration statement necessary for performing the required
task. The body enclosed in braces contain three parts:
-Function statement that perform the task of the function.Using void as shown below:
NOTE: If function does not return any value, we can omit the return statement.
Function call :
Programming with C Lecture Notes on Functions:
A function can be called simply using the function name followed by a list of actual
parameters(or arguments), if any enclosed in parentheses. Let's take an example for
multiplication of two numbers.
When the compiler encounters a function call, the control is transferred to the function mul( ).
This function is then executed line by line as described and a value is returned when a return
statement is encountered. This value is assigned to y. This is illustrated below:
Function prototype(declaration):
Every function in C programming should be declared before they are used. These type of
declaration are also called function prototype. Function prototype gives compiler information
about function name, type of arguments to be passed and return type.
In the above example,int add(int a, int b); is a function prototype which provides
following information to the compiler:
Function prototype are not needed if user-definition function is written before main()
function.
Function call
Control of the program cannot be transferred to user-defined function unless it is called
invoked).
In the above example, function call is made using statement add(num1,num2); from main().
This make the control of program jump from that statement to function definition and
executes the codes inside that function.
Function definition
Function definition contains programming codes to perform specific task.
Programming with C Lecture Notes on Functions:
Passing arguments to functions
In programming, argument/parameter is a piece of data(constant or variable) passed from a
program to the function.
In above example two variable, num1 and num2 are passed to function during function call
and these arguments are accepted by arguments a and b in function definition.
Arguments that are passed in function call and arguments that are accepted in function
definition should have same data type. For example:
If argument num1 was of int type and num2 was of float type then, argument variable a
should be of type int and b should be of type float,i.e., type of argument during function call
and function definition should be same.
Return Statement
Return statement is used for returning a value from function definition to calling function.
For example:
return;
return a;
return (a+b);
In above example, value of variable add in add() function is returned and that value is stored
in variable sum in main() function. The data type of expression in return statement should
also match the return type of function.
Programming with C Lecture Notes on Functions:
Let's take an example to find whether a number is prime or not using above 4 cateogories of
user defined functions.
/*C program to check whether a number entered by user is prime or not using
function with no arguments and no return value*/
#include <stdio.h>
void prime();
int main(){
prime(); //No argument is passed to prime().
return 0;
}
void prime(){
/* There is no return value to calling function main(). Hence, return type
of prime() is void */
int num,i,flag=0;
printf("Enter positive integer enter to check:\n");
scanf("%d",&num);
for(i=2;i<=num/2;++i){
if(num%i==0){
flag=1;
}
}
if (flag==1)
printf("%d is not prime",num);
else
Programming with C Lecture Notes on Functions:
printf("%d is prime",num);
}
Function prime() is used for asking user a input, check for whether it is prime of not and
display it accordingly. No argument is passed and returned form prime() function.
/*C program to check whether a number entered by user is prime or not using
function with no arguments but having return value */
#include <stdio.h>
int input();
int main(){
int num,i,flag;
num=input(); /* No argument is passed to input() */
for(i=2,flag=i;i<=num/2;++i,flag=i){
if(num%i==0){
* printf("%d is not prime",num);
++flag;
break;
}
}
if(flag==i)
printf("%d is prime",num);
return 0;
}
int input(){ /* Integer value is returned from input() to calling
function */
int n;
printf("Enter positive enter to check:\n");
scanf("%d",&n);
return n;
}
There is no argument passed to input() function But, the value of n is returned from
input() to main() function.
Here, check_display() function is used for check whether it is prime or not and display it
accordingly. Here, argument is passed to user-defined function but, value is not returned from
it to calling function.
Here, check() function is used for checking whether a number is prime or not. In this
program, input from user is passed to function check() and integer value is returned from it.
If input the number is prime, 0 is returned and if number is not prime, 1 is returned.
A list of the most common libraries and a brief description of the most useful functions they
contain follows:
Programming with C Lecture Notes on Functions:
1. stdio.h: I/O functions:
1. getchar() returns the next character typed on the keyboard.
2. putchar() outputs a single character to the screen.
3. printf() as previously described
4. scanf() as previously described
stdio.h
In the C Programming Language, the Standard Library Functions are divided into several
header files.
The following is a list of functions found within the <stdio.h> header file:
Programming with C Lecture Notes on Functions:
Formatted Input/Output functions
fprintf Formatted File Write
#include <stdio.h>
int sum(int n);
int main(){
int num,add;
printf("Enter a positive integer:\n");
scanf("%d",&num);
add=sum(num);
printf("sum=%d",add);
}
int sum(int n){
if(n==0)
return n;
else
return n+sum(n-1); /*self call to function sum() */
}
Output
Programming with C Lecture Notes on Functions:
Enter a positive integer:
5
15
In, this simple C program, sum() function is invoked from the same function. If n is not equal
to 0 then, the function calls itself passing argument 1 less than the previous argument it was
called with. Suppose, n is 5 initially. Then, during next function calls, 4 is passed to function
and the value of argument decreases by 1 in each recursive call. When, n becomes equal to 0,
the value of n is returned which is the sum numbers from 5 to 1.
sum(5)
=5+sum(4)
=5+4+sum(3)
=5+4+3+sum(2)
=5+4+3+2+sum(1)
=5+4+3+2+1+sum(0)
=5+4+3+2+1+0
=5+4+3+2+1
=5+4+3+3
=5+4+6
=5+10
=15
Every recursive function must be provided with a way to end the recursion. In this example
when, n is equal to 0, there is no recursive call and recursion ends.
In other hand, it is hard to think the logic of a recursive function. It is also difficult to debug
the code containing recursion.
Recursion is the term given to the mechanism of Iteration is the block of statement executed
defining a set or procedure in terms of itself. repeatedly using loops.
A conditional statement is required in the body The iteration control statement itself contains
of the function for stopping the function statement for stopping the iteration. At every
Programming with C Lecture Notes on Functions:
At some places, use of recursion generates extra All problems can be solved with iteration.
overhead. Hence, better to skip when easy
solution is available with iteration.
Recursion is expensive in terms of speed and Iteration does not create any overhead. All the
memory. programming languages support iteration.
The C programming language supports recursion, i.e., a function to call itself. But while
using recursion, programmers need to be careful to define an exit condition from the function,
otherwise it will go in infinite loop.
Recursive function are very useful to solve many mathematical problems like to calculate
factorial of a number, generating Fibonacci series, etc.
Number Factorial
Following is an example, which calculates factorial for a given number using a recursive
function:
#include <stdio.h>
When the above code is compiled and executed, it produces the following result:
Factorial of 15 is 2004310016
/* Iterative Version */
unsigned int iter_factorial(int n)
{
Programming with C Lecture Notes on Functions:
int f = 1;
int i;
for(i = 1; i <= n; i++)
{
f *= i;
}
return f;
}
Fibonacci Series
Following is another example, which generates Fibonacci series for a given number using a
recursive function:
#include <stdio.h>
int fibonaci(int i)
{
if(i == 0)
{
return 0;
}
if(i == 1)
{
return 1;
}
return fibonaci(i-1) + fibonaci(i-2);
}
int main()
{
int i;
for (i = 0; i < 10; i++)
{
printf("%d\t%n", fibonaci(i));
}
return 0;
}
When the above code is compiled and executed, it produces the following result:
0 1 1 2 3 5 8 13 21 34
int Fibonacci(int n)
{
int f1 = 0;
int f2 = 1;
int fn;
for ( int i = 2; i < n; i++ )
Programming with C Lecture Notes on Functions:
{
fn = f1 + f2;
f1 = f2;
f2 = fn;
}
}
-----------------------------------------------------END---------------------------------------------------------------------------