Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 14
FUNCTIONS
FUNCTION WITHOUT ARGUMENT
AND RETURN VALUE #include<stdio.h> void printName(); // Function declaration void main () { printf("Hello "); printName(); } void printName() // Function Body { printf("Javatpoint"); PRINT NAME FUNCTION EXAMPLE } FUNCTION WITHOUT ARGUMENT AND RETURN VALUE • #include<stdio.h> • void sum(); void sum() { • void main() int a,b; •{ printf("\ printf("\ nGoing to calculate the sum of t nEnter two numbers"); scanf("% wo numbers:"); d %d",&a,&b); sum(); printf("The sum is %d",a+b); } } SUM OF TWO NUMBERS FUNCTION WITHOUT ARGUMENT AND WITH RETURN VALUE #include<stdio.h> int sum(); // Function declaration void main() { int result; printf("\nGoing to calculate the sum of two numbers:"); result = sum(); printf("%d",result); } int sum() // Function Body : { int a,b; printf("\nEnter two numbers"); SUM OF TWO NUMBERS scanf("%d %d",&a,&b); return a+b; } FUNCTION WITHOUT ARGUMENT AND WITH RETURN VALUE #include<stdio.h> int sum(); // Function declaration void main() { printf("Going to calculate the area of the square\n"); float area = square(); printf("The area of the square: %f\n",area); } int square() // Function Body : { float side; printf("Enter the length of the side in meters: "); AREA OF SQUARE CALCULATION scanf("%f",&side); PROGRAM return side * side; } 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) { SUM OF TWO NUMBERS PROGRAM printf("\nThe sum is %d",a+b); } FUNCTION WITH ARGUMENT AND WITHOUT RETURN VALUE #include<stdio.h> void average(int, int, int, int, int); void main() { int a,b,c,d,e; printf("\nGoing to calculate the average of five numbers:"); printf("\nEnter five numbers:"); scanf("%d %d %d %d %d",&a,&b,&c,&d,&e); average(a,b,c,d,e); } void average(int a, int b, int c, int d, int e) { float avg; avg = (a+b+c+d+e)/5; AVERAGE OF FIVE NUMBERS printf("The average of given five numbers : %f",avg); } 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) { SUM OF TWO NUMBERS return a+b; } FUNCTION WITH ARGUMENT AND WITH RETURN VALUE #include<stdio.h> int even_odd(int); int even_odd(int n) void main() { { int n,flag=0; if(n%2 == 0) printf("\nGoing to check whether a number is even or odd"); { printf("\nEnter the number: "); return 1; scanf("%d",&n); } flag = even_odd(n); else if(flag == 0) { { return 0; printf("\nThe number is odd"); } } else } { printf("\nThe number is even"); } } CHECK EVEN OR ODD PROGRAM FUNCTION WITH CALL BY VALUE #include<stdio.h> void change(int num) { printf("Before adding value inside function num=%d \n",num); num=num+100; printf("After adding value inside function num=%d \n", num); } int main() { int x=100; printf("Before function call x=%d \n", x); change(x);//passing value in function printf("After function call x=%d \n", x); return 0; } CALL BY VALUE EXAMPLE #include <stdio.h> void swap(int , int); //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); // printing the value of a and b in main swap(a,b); printf("After swapping values in main a = %d, b = %d\n",a,b); / *The value of actual parameters do not change by changing the formal parameters in call by value, a = 10, b = 20 */ } void swap (int a, int b) { SWAPPING TWO VARIABLES 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 } FUNCTION EXAMPLE: PASSING ARRAY AS PARAMETER #include <stdio.h> void getarray(int arr[]) // FUNCTION WITH ARRAY AS PARAMETER { printf("Elements of array are : "); for(int i=0;i<5;i++) { printf("%d ", arr[i]); } } PASSING AN ARRAY EXAMPLE int main() { int arr[5]={45,67,34,78,90}; // ARRAY WITH FIVE VARIABLES getarray(arr); return 0; } FUNCTION EXAMPLE: PASSING ARRAY AS PARAMETER #include <stdio.h> void getarray(int arr[]) // FUNCTION WITH ARRAY AS PARAMETER { printf("Elements of array are : "); for(int i=0;i<5;i++) { printf("%d ", arr[i]); } } PASSING AN ARRAY EXAMPLE int main() { int arr[5]={45,67,34,78,90}; // ARRAY WITH FIVE VARIABLES getarray(arr); return 0; } FUNCTION EXAMPLE: PASSING ARRAY #include<stdio.h> int minarray(int arr[],int size){ AS PARAMETER int min=arr[0]; int i=0; for(i=1;i<size;i++){ if(min>arr[i]){ min=arr[i]; } // END OF IF BLOCK } //end of for return min; }//end of function
int main(){ PASSING AN ARRAY : FINDING MINIMUM
int i=0,min=0; NUMBER int numbers[]={4,5,7,3,8,9}; //declaration of array min=minarray(numbers,6); //passing array with size printf("minimum number is %d \n",min); return 0; }
Get The Student Details Like Name, Dept, m1, m2, m3. Find The Average and Grade of The Student (O, A, B, C) - Using Switch Case Convert The Grade Into O-Excellent, A - Good, B-Average, C-Fair
Get The Student Details Like Name, Dept, m1, m2, m3. Find The Average and Grade of The Student (O, A, B, C) - Using Switch Case Convert The Grade Into O-Excellent, A - Good, B-Average, C-Fair