








Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!









A large C program is divided into basic building blocks called C function. C function contains set of instructions enclosed by “{ }” which performs specific operation in a C program. Actually, Collection of these functions creates a C program.
USES OF C FUNCTIONS:
Predefined Function
Call by reference
User Defined Function
Call By value Function^ Buit in PredeORfined Function
printf() stdio.h scanf() getch(); conio.h
Types of functions in C programming Depending on whether a function is defined by the user or already included in C compilers, there are two types of functions in C programming There are three types of functions in C programming:
The printf() is a standard library function to send formatted output to the screen (display output on the screen). This function is defined in "stdio.h" header file.There are other numerous library functions defined under "stdio.h" , such as scanf() , fprintf() , getchar() etc. Once you include "stdio.h" in your program, all these functions are available for use.
Advantages of user-defined function
Function Definition in C Programming:
#include<stdio.h> #include<conio.h> int sum(int n1,int n2) { return(n1+n2); }
int main() { int num1=11,num2=22; int result;
result = sum(num1,num2); printf("\nResult : %d",result); return(0); } Explanation of Live Example :
Function Nane Sum Return Type Integer Calling Function Main
Programmes:-Write a programme to display add two numbers using Function.
With Argument & with Return Value No Argument & with Return value
With Argument & No Return value No Argument & No Return Value
#include <stdio.h> #include<conio.h> int add (int, int ); //Function prototype main() { Int ans,b,c; clrscr(); printf(“enter no.”); scanf(“%d%d”,&b,&c); ans=add(int a,int b);/ /Function Call printf(“%d”,ans);
#include <stdio.h> #include<conio.h> int add (void ); //Function prototype main() { Int ans,b,c; clrscr(); ans=add();/ /Function Call printf(“%d”,ans); getch(); }
#include <stdio.h> #include<conio.h> vod add (int ,int); //Function prototype main() { Int b,c; clrscr(); printf(“enter no”); scanf(“%d%d”,&a,&b); add(int a, int b); //Function Call getch(); }
#include <stdio.h> #include<conio.h> void add (void); //Function prototype
main() { clrscr(); add();/ /Function Call getch(); } void add(void ) // Function definition {
getch(); } int add(int x,int y ) / / Function definition { Int c; c=x+y; return c; } Output:-enter no. 2 3 5
int add(void) // Function definition { Int c,x,y; Printf(“enter no.”); Scanf(“%d%d”,&x,&y); c=x+y; return c; } Output:-enter no. 2 3 5
int add(int x,int y) // Function definition {
Int c; c=x+y; printf(“%d”,c); } Output:-enter no. 2 3 5
Int a,b,c; Printf(“enter no.”); Scanf(“%d%d”&a,&b); c=a+b; printf(“%d”,c); } Output:-enter no. 2 3 5
Two Ways of Passing Argument to Function in C Language :
return (0); } Output :
Number 1 : 50 Number 2 : 70 Explanation : Call by Value
B .Call by Reference/Pointer/Address : #include<stdio.h> void interchange( int *num1, int *num2) { int temp; temp = *num1; *num1 = *num2; *num2 = temp;
Summary of Call By Value and Call By Reference :
Point Call by Value Call by Reference
Copy Duplicate Copy of Original Parameter is Passed Actual Copy of Original Parameter is Passed
Modification No effect on Original Parameter after modifying parameter in function
Original Parameter gets affected if value of parameter changed inside function