Nothing Special   »   [go: up one dir, main page]

unit4_cprogramming_function

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

Unit 4 – C programming

Please write a note of topics “Review of c programming“


from class 11 note.
Functions: Functions are the self-contained program that contains several
block of statement which performs the defined task. In C language, we can
create one or more functions according to the requirements.

Usually, In C programs flows from top left to right bottom of main() functions.
We can create any number of functions below that main() functions. These
function are called from main() function.

Advantage:

1. Big programs can be divided into smaller module using functions.

2. Program development will be faster.

3. Program debugging will be easier and faster.

4. Use of functions reduce program complexity.

5. Program length can be reduced through code reusability.

6. Use of functions enhance program readability.

7. Several developer can work on a single project.

8. Functions are used to create own header file i.e mero.h

9. Functions can be independently tested.

Different types of functions are:-

1. Library Function

C programming language provides some library functions to perform some predefined tasks.
These functions are also called the built-in or predefined function in the C header files whose
meaning cannot change. When we use these functions on any program, we call the function
name with appropriate header files because these functions are defined inside the header
files. In other words, we did not require to write the complete code to perform a specific task;
instead, we can directly call the function in our program whenever it is required. For example:
printf(), scanf(), getch(), etc., are the predefined library functions.

2. User Defined Function

As the name suggests, a user-defined function is a function written by the user to write any
program code and execute specific actions. These user-defined functions can be modified and
execute according to the requirement of the programmer. A programmer can change the user-
defined function, but these functions are not defined in the C header files. A user-defined
function is made up using the function declaration, function definition, and the function call.

Difference between library function and user defined functions are:-

Requirement while creating a functions are


Function Declaration
The function declaration defines the function name, return type and the passed arguments in it. A
function definition is always defined outside of the main() function in any C program.

Syntax:
return_type function_name ( data_type arg1, data_type arg2, ..) ;

Function Calling
Here return type represents the function that can return any defined data type values. The return
type value can be int, float, char, double, etc., where the function_name represents the name of a
function that contains more than one argument in it.

After defining the function definition, we need to call the defined function in a program to execute
its tasks. However, a function can be called multiple times by writing the function name followed by
arguments lists.

Syntax:

function_name(arg1, arg2, ...)

Here, arg1, arg2 are the actual arguments passed to the function_name.

The function definition


The function definition defines the actual body of the function that perform some specific tasks in a
program.

Syntax:

return_type function_name ( data_type arg1, data_type arg2, ... ) {

// define the variables

statement to be executed;

return (expr);

WAP to calculate simple interest using function


#include<stdio.h>
float interest(void); //function declaration
int main()
{ float si;
si=interest(); //function call
printf("Simple interst is %.2f\n",si);
return 0;
}
float interest() //function definition
{
float p,t,r,i;
printf("Enter Principal, Time and Rate");
scanf("%f%f%f",&p,&t,&r);
i=(p*t*r)/100;
return i; //function return value
}
WAP to calculate area of rectangle using function.
#include<stdio.h>
Int area (void);
int main()
{
int a;
a = area();
printf(“area is %d”,a);
return 0;
}
int area()
{
int l,b,ar;
printf(“Enter length and breadth”);
scanf(“%d%d”,&l,&b);
ar = l*b
return ar;
}

Different aspects of function calling


A function may or may not accept any argument. It may or may not return any value.
Based on these facts, There are four different aspects of function calls.

o function without arguments and without return value


o function without arguments and with return value
o function with arguments and without return value
o function with arguments and with return value
Function without argument and return value

#include<stdio.h>
void sum();
void main()
{
printf("\nGoing to calculate the sum of two numbers:");
sum();
}
void sum()
{
int a,b;
printf("\nEnter two numbers");
scanf("%d %d",&a,&b);
printf("The sum is %d",a+b);
}

Function without argument and with return value

#include<stdio.h>
int sum();
void main()
{
int result;
printf("\nGoing to calculate the sum of two numbers:");
result = sum();
printf("%d",result);
}
int sum()
{
int a,b;
printf("\nEnter two numbers");
scanf("%d %d",&a,&b);
return a+b;
}
Function with argument and without return value

#include<stdio.h>
void sum(int, int);
void main()
{
int a,b,result;
printf("\nGoing to calculate the sum of two numbers:");
printf("\nEnter two numbers:");
scanf("%d %d",&a,&b);
sum(a,b);
}
void sum(int a, int b)
{
printf("\nThe sum is %d",a+b);
}

Function with argument and with return value

#include<stdio.h>
int sum(int, int);
void main()
{
int a,b,result;
printf("\nGoing to calculate the sum of two numbers:");
printf("\nEnter two numbers:");
scanf("%d %d",&a,&b);
result = sum(a,b);
printf("\nThe sum is : %d",result);
}
int sum(int a, int b)
{
return a+b;
}
Function Methods:-
There are two methods to pass the data into the function in C language, i.e., call by value and call by
reference.

a. Call by Value
 In call by value method, the value of the actual parameters is copied into the formal
parameters. In other words, we can say that the value of the variable is used in the function
call in the call by value method.
 In call by value method, we can not modify the value of the actual parameter by the formal
parameter.
 In call by value, different memory is allocated for actual and formal parameters since the
value of the actual parameter is copied into the formal parameter.
 The actual parameter is the argument which is used in the function call whereas formal
parameter is the argument which is used in the function definition.
Example:- WAP to Swap two numbers using call by value method.

#include <stdio.h>

int swap(int x , int y); //prototype of the function

int main()

int a = 10;
int b = 20;

printf("Before swapping the values in main a = %d, b = %d\n",a,b);

swap(a,b);

printf("After swapping values in main a = %d, b = %d\n",a,b);

int swap (int a, int b)

int temp;

temp = a;

a=b;

b=temp;

printf("After swapping values in function a = %d, b = %d\n",a,b); // Formal parameters, a=20, b=10
}

b. Call by reference:-
 In call by reference, the address of the variable is passed into the function call as the actual
parameter.
 The value of the actual parameters can be modified by changing the formal parameters since
the address of the actual parameters is passed.
 In call by reference, the memory allocation is similar for both formal parameters and actual
parameters. All the operations in the function are performed on the value stored at the address
of the actual parameters, and the modified value gets stored at the same address.

Example:- WAP to swap two numbers using call by reference method.

#include <stdio.h>

int swap(int *x, int *y); //prototype of the function

int main()

int a = 10;

int b = 20;
printf("Before swapping the values in main a = %d, b = %d\n",a,b);

swap(&a,&b);

printf("After swapping values in main a = %d, b = %d\n",a,b);

int swap (int *a, int *b)

int temp;

temp = *a;

*a=*b;

*b=temp;

printf("After swapping values in function a = %d, b = %d\n",*a,*b);

Recursive Function:-
A function which calls itself directly or indirectly again and again until some specified condition is
satisfied is known as Recursive Function.

A recursive function is a function defined in terms of itself via self-calling expressions. This means
that the function will continue to call itself and repeat its behavior until some condition is satisfied
to return a value.

Recursion is the process of defining something in terms of itself. Recursion has many negatives. It
repeatedly invokes the mechanism, and consequently increases the overhead of function calls. This
repetition can be expensive in terms of both processor time and memory space.

E.g WAP to find out the factorial of given number using recursive function.

include <stdio.h>
int fact (int);
int main()
{
int n,f;
printf("Enter the number whose factorial you want to calculate?");
scanf("%d",&n);
f = fact(n);
printf("factorial = %d",f);
}
int fact(int n)
{
if (n==0)
{
return 0;
}
else if ( n == 1)
{
return 1;
}
else
{
return n*fact(n-1);
}
}
); } }

// to calculate sum of n-natural


number using recursion/recursive
function.
#include<stdio.h>
int sum (int);
int main()
{
int n,s;
printf("Enter any number");
scanf("%d",&n);
s = sum(n);
printf("Sum is %d\n",s);
return 0;
}
int sum (int n)
{
if (n<=0)
return 0;
else
return (n+sum(n-1));
}

You might also like