C Function
C Function
C Function
Topic: Function
Programming in C
Dr. Nivedita Palia
Functions: Definition & Advantages
A function is a block of code defined to perform a particular task. {…..}
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
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.
int i=3;
Example
* 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.