The C Programming language Chapter8 Functions Exp: X3 #include <stdio. h> a1.2 float cube(float x product 1.728 return(x*x*x) mal float a, product printf("please input value of a: " scanf(%of, &a) product=cube(a) printf("Cube of %.4f is %0.4fn", a, product)
The C Programming Language Chapter 8 Functions Exp : x3 #include <stdio.h> float cube(float x) { return(x*x*x); } main() { float a, product; printf("Please input value of a:"); scanf("%f",&a); product=cube(a); printf(”Cube of %.4f is %.4f\n",a,product); } x a product ×× ×× 1.2 1.2 1.728
The c Programming language Chapter8 Functions Exp: exchange two numbers /*ch72c* ginnings X #include <stdio. h> ma X y {intx-7,y=11; calling: printf("x%d, ty=%, x,y) a b printf("swapped: n") swap(x,y) swap y printf("x=%d, ty=%dn, y) Swal ap(int a, int b) Int temp temp temp=a;a=b; b=temp finish
The C Programming Language Chapter 8 Functions beginning: x: 7 y: 11 finish: x: 7 y: 11 Exp: exchange two numbers /*ch7_2.c*/ #include <stdio.h> main() { int x=7,y=11; printf("x=%d,\ty=%d\n",x,y); printf("swapped:\n"); swap(x,y); printf("x=%d,\ty=%d\n",x,y); } swap(int a,int b) { int temp; temp=a; a=b; b=temp; } calling: a: 7 b: 11 x: 7 y: 11 swap: x: 7 y: 11 a: 11 b: 7 temp
The c Programming language Chapter8 Functions 88.5 Function Invocation main( af mainO i int i-2, p i int j-2, p p=f(,+1) f(,i++) printf(%od"p) eters printf(%od" p) g int f(int a, int b) Tu int f(int a, int b) nt c i int c F if(a>b)c=1 if(a>b) else if(a==b )c=0 s else if(a==b) else c=-1 else c=-1 return(c as return(c) result 0 result 1 m-max(a, maX(b,c
The C Programming Language Chapter 8 Functions §8.5 Function Invocation ❖Specifications: ➢The arguments match in number and type the parameters; ➢Evaluating order of the arguments in the argument list depends on the system(Turbo C right-to-left) function-name( argument list ) ❖Forms of function Invocation ➢Function as statement: as printstar(); printf(“Hello,World!\n”); ➢Function in expression: as m=max(a,b)*2; ➢Function as argument: as printf(“%d”,max(a,b)); m=max(a,max(b,c)); main() { int i=2,p; p=f(i,++i); printf("%d",p); } int f(int a, int b) { int c; if(a>b) c=1; else if(a==b) c=0; else c=-1; return(c); } main() { int i=2,p; p=f(i, i++); printf("%d",p); } int f(int a, int b) { int c; if(a>b) c=1; else if(a==b) c=0; else c=-1; return(c); result:0 } result:1
The c Programming language Chapter8 Functions s Function Declarations(Function Prototypes) type function-name(parameter type parameter..) Functions should be declared before they are used Tells the compiler the number and type of arguments and the type of the function Function prototypes is different from Function definitions Function prototypes occurs in or outside the function Function prototypes can be omitted, when the return type of the function is int or char The called function is defined before the calling funtion
The C Programming Language Chapter 8 Functions ➢Functions should be declared before they are used; ➢Tells the compiler the number and type of arguments and the type of the function; ➢Function prototypes is different from Function definitions; ➢Function prototypes occurs in or outside the function ➢Function prototypes can be omitted,when: ▪ the return type of the function is int or char; ▪The called function is defined before the calling funtion . type function-name(parameter type [parameter1],….. ); ❖Function Declarations (Function Prototypes):