Functions
Advanced C Project
Benefits of Fuctions
What are fuctions?
Function design
Types Of Function Calls
Call by
Call by value reference
Sends values of the
arguments Sends addresses of
the arguments
main() Call by value
int a=20,b=10;
swap(a,b); \\Calling function
Value of a and b is
printf(“a=%d,b=%d”,a,b);
copied into x and y
swap(int x,int y) \\Called function
int t;
t=x; Output :
x=y; x=10 , y=20
y=t;
a=20 , b=10
printf(“x=%d,y=%d”,x,y);
Conclusion:
★ With this method, the changes made to the
arguments in the called function have no effect on
the arguments in the calling function.
main() Call by reference a b
int a=20,b=10; 20 10
1001 1002
swap(&a,&b);
printf(“a=%d,b=%d”,a,b);
a b
swap(int *x,int *y) 10 20
int t; 1001 1002
t=*x;
x y
*x=*y; Output:
1001 1002
a=10 b=20
*y=t;
2003 2004
Conclusion:
★ With this method, using addresses in the called function,
we can have an access to the arguments in the calling
function and hence we can manipulate them.
★ Using a call by reference, we can make a function return
more than one value at a time, which is not possible
ordinarily.
PRE-DEFINED FUNCTIONS IN C LANGUAGE:
● Basically, predefined functions in C program means whose defintion is already
saved in the header files.
● There are many Inbuilt functions in C program, those are also said to be library
functions.
Few Pre-defined functions are explained below:
Syntax for printf: printf(“hello“);// when any comment has to be
printed//
printf(“format specifier“,var_name);//when value of a
variable has to be displayed.//
2.Syntax for scanf: scanf("access modifier",&var_name);
The defination of printf & scanf functions is stored in <stdio.h> file.
3.Syntax for getch getch();
4.Syntax for clrscr clrscr();
The defination of getch & clrscr functions is stored in <conio.h> file.
5. Syntax for pow() var_name=pow(base,index);
6.Syntax for sqrt() var_name=sqrt(var_name);
The defination of pow() & sqrt() is stored in <math.h> header file.
Example for printf,scanf & getch,clrscr functions.
#include<stdio.h>
#include<conio.h>
void main()
int a;
clrscr();
printf(“Enter any no: “);
scanf(“%d“,a);
getch();
Example for pow & sqrt functions.
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int a,p,rt;
clrscr();
printf(“Enter any no: “);
scanf(“%d“,a);
p=pow(a,4);
rt=sqrt(a);
printf(“power of the number is:%d “,p);
printf(“square root of the number is:%d “,rt);