C Function

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

Unit 2

Topic: Function

Programming in C
Dr. Nivedita Palia
Functions: Definition & Advantages
A function is a block of code defined to perform a particular task. {…..}

C language has 2 categories of function:


1. Library functions 2. User defined functions

There are many advantages of using functions:

Length of the source program is reduced.


Easy to locate and debug errors.
Saves debugging time
A function can be used by many other programs.
It facilitates top to down modular approach.
Promotes reusability of code.
Top –down modular approach
Elements of User Defined Functions
Function Declaration //Function Prototype
Function Call
Function Definition // body define

Function definition includes the following:


1. Function name
2. Function Type Function header
3. List of Parameters
4. Local Variable declarations
5. Function statements Function Body
6. A return statement
C function
#include<stdio.h>
void message(); /* function prototype declaration */
int main()
{
message(); /* function call , main() calling function whereas message() called function */
printf (“Cry, and you stop the monotony!\n”);
return 0; }
void message() /* function definition */
{
printf (“Smile, and the world smiles with you…\n”);
}

Output
Smile, and the world smiles with you…
Cry, and you stop the monotony!
Multi Function Program Structure
Tips about function
• A C program is collection of one or more functions.
• If a C program contains only one function, it must be main().
• Each function in a program is called in sequence specified by the function calls
in main().
• After each function has performed , controls returns to main(). When main
runs out of statements and function call, the program ends
• Program execution always begin with main().
• A function cannot defined in another function
• A function can be called from any other function .
• A function can be called any number of times.
• The order in which function defined and the order in which they called need
not necessarily be same.
Type of function

1. Function with no argument and no return


2. Function with argument and no return
3. Function with argument and return
4. Function with no argument and return
Function with no argument and no return

No data communication between function


Function with no arguments and no return value
#include <stdio.h>
//Function with no arguments and no return type
void add(); // function declaration
void main()
{
add(); //function call
}
void add()
{
int a,b,c;
printf("enter numbers to be added");
//function definition
scanf("%d %d", &a, &b);// 10 34

c=a+b; // 10 ,34
printf("result=%d",c);
}
Output
Function with Arguments and no return value
Function with Arguments and no return value
#include <stdio.h>
//Function with arguments and no return type
void add(int ,int ); // function declaration
void main()
{
int a,b;
printf("enter numbers to be added");
scanf("%d %d", &a, &b);// 10 ,34
add(a,b); // function call ,add(10,34) // a and b Actual Argument
}
void add(int c, int d) // (10,34) // c and d Formal Agrument
{
int e; // function definition

e=c+d; // 10 ,34
printf("result=%d",e);
}
output
Tips
1.Type , order and number of actual and formal arguments must
always be same.
2. It is not compulsory to use variable names in the prototype
declaration.
void sum (int , int);
3. No separate return statement was necessary to send control
back
4. If the value of formal argument changed in the function the
corresponding change does not take place in the calling function
Function with Argument & Return Statement
#include <stdio.h>
/* function with argument and return */
float add(float,float); // function declaration
void main()
{
float a,b,s;
printf("enter numbers to be added");
scanf("%f %f", &a, &b);
s=add(a,b);// function call a,b actual argument
printf("Sum=%f",s);
}
float add(float c, float d) //return type of a function //formal argument
{
float e;
e=c+d;
return e;
}
output
return statement
1. On executing the return statement , it immediately
transfer the control back to the calling function
2. It returns the value present in the parentheses after return
to the calling function
3. There is no restriction on the number of return
statements in a function but A function can return only one
value at a time. For eg: to find largest of two numbers
Function with no argument with return
int get_num(void);
void main()
{
int m=get_num();
printf("the value of m= %d",m);
}
int get_num(void)
{
int n;
printf("enter value of n");
scanf("%d",&n);
return(n);
}
output
Question
1)WAP to calculate simple interest using functions.

2)WAP to check whether a number is an Armstrong number or not


using functions with return value.

3)WAP to calculate factorial of a number using functions with


return statement.
Recursion: Definition & Conditions
Recursion is a process in which a function calls itself.
Necessary Conditions for recursion:
Base case / Stopping condition
Call to itself
void main()
{ fun_a(); }

void fun_a() // recursive fun


{ ……..
fun_a();
}
Recursion example
#include <stdio.h>
int fact(int);
/* resursive function */
int main() {
int n, factorial;
printf("enter the value of n\n");
scanf("%d",&n);
factorial=fact(n);
printf("the factorial of %d is %d ", n, factorial);
return 0;
}
int fact(int a)
{ int fac;
if(a==1)
return(1);
else
fac=a*fact(a-1);
return(fac);
}
Question

WAP to print the terms of Fibonacci series using recursion.


Pointer

A pointer is a derived data type in C.

It is built from one of the fundamental data type available in


C

It contains memory addresses as their value


Variable Declaration (memory concept)

int i=3;
Example

& address of operator

* Indirection
operator/value at
address
Example of Call by Reference
#include <stdio.h>
/* call by reference*/
void change(int*);
int main()
{
int x;
x=20;
change(&x);
printf("%d",x);
return 0;
}
void change(int* p)
{
*p=*p+10;
}
Swapping of two numbers
using call by value & call by reference
#include <stdio.h> #include <stdio.h>
/* swap call by value*/ /* swap call by reference*/
void swap(int, int ); void swap(int *, int * );
int main() int main()
{ {
int a,b; int a,b;
a=20; a=20;
b=30; b=30;
swap(a,b); swap(&a,&b);
printf("a=%d\tb=%d",a,b); printf("a=%d\tb=%d",a,b);
return 0; return 0;
} }
void swap(int x, int y) void swap(int *x, int *y)
{ {
int t; int t;
t=x; t=*x;
x=y; *x=*y;
y=t; *y=t;
printf("x=%d\ty=%d\n",x,y); printf("x=%d\ty=%d\n",*x,*y);
} }
Call by Value Vs Call by reference

In call by value, the values of the function arguments are passed during
function call while in call by reference, the reference/address of the
function arguments are passed during function call.

In call by value, changes are made to the local copies of the arguments,
while in call by reference, changes are made at the address values.

In call by value, old values are retained. In call by reference, old values are
updated with new values.

In call by value, changes are temporary but in call by reference, changes


made are permanent.
Return more than one value
Question

Q Write a function that receives marks of 3 subject and returns


average and percentage of these marks
END

You might also like