50 likes | 136 Views
No-return functions. #include<stdio.h> void printA(void) { printf(“AAAAA<br>”); } void main() { printf(“First line<br>”); printA(); printf(“Second line<br>”); printA(); }. Result. First line AAAAA Second line AAAAA. No-return functions. #include<stdio.h>
E N D
No-return functions #include<stdio.h> void printA(void) { printf(“AAAAA\n”); } void main() { printf(“First line\n”); printA(); printf(“Second line\n”); printA(); } Result First line AAAAA Second line AAAAA
No-return functions #include<stdio.h> void write(int num) { printf(“The number is %d.\n”, num); } void main() { int a; printf(“Enter a number: “); scanf(“%d”, &a); write(a); printf(“End of program.\n”); } Result Enter a number: 5 The number is 5. End of program.
Return functions #include<stdio.h> int area(int width, int height) { return width*height; } void main() { int a, b, result; printf(“Enter width and height: “); scanf(“%d %d”, &a, &b); result = area(a, b); printf(“Area is %d.\n”, result); } Result Enter width and height: 5 10 Area is 50.
switch(choice) { case ‘a’: result = add(a, b); break; case ‘s’: result = sub(a, b); break; } printf(“Result = %d”, result); return 0; } Return functions #include<stdio.h> int add(int a, int b); int sub(int a, int b); int main() { int choice, a, b, result; printf(“Select operation: \n”); printf(“a for addition\ns for subtraction\n: “); scanf(“%d”, &choice); printf(“Enter two integers: “); scanf(“%d %d”, &a, &b); Result Select operation: a for addition s for subtraction : a Enter two integers: 2 3 Result = 5
int main() { int x = 5; printf(“x is %d when starting.\n”, x); add1(x); printf(“x is %d after add1.\n”, x); add2(&x); printf(“x is %d after add2.\n”, x); return 0; } Call by value VS Call by ref #include<stdio.h> void add1(int a) { a += 10; } void add2(int *b) { *b+=10; } Result x is 5 when starting. x is 5 after add1. x is 15 after add2.