Download presentation

Presentation is loading. Please wait.

Published byBrenna Barret Modified about 1 year ago

1
Lab 8 User Defined Function

2
What is a function? main() {. } function1(..) function1(..){..} examples: cos(a); sin(a); pow(a,b);

3
Using a function Declaring name and type of function Using the function within main() Writing the actual function #include output function (input) void main (void) {………. function() ………...} output function (input) {…. …… …}

4
Function example #include void main (void) { int x=10,n=3,r; printf("This line will appear on your screen\n"); r=pow(x,n); printf("\nr = %d\n\n",r); } function f1() function f2()

5
Function example: f1() #include void f1 (void); void main (void) {int x=10,n=3,r; f1(); r=pow(x,n); printf("\nr = %d\n\n",r); } void f1 (void) { printf("This line will appear on your screen\n"); } no variable values are exchanged between main() and f1()

6
Function example: f2() #include void f1 (void); void f2 (int a, int b); void main (void) {int x=10,n=3; f1(); f2(x,n); } void f1 (void) { printf("This line will appear on your screen\n"); } void f2 (int a, int b) { int r; r=pow(a,b); printf("\nr = %d\n\n",r); } r is defineda and b are two integer inputs to function f2() f2() is given two integer inputs

7
Function example: f2() contd #include void f1 (void); int f2 (int a, int b); void main (void) {int x=10,n=3,result; f1(); result=f2(x,n); printf("\nr = %d\n\n",result);} void f1 (void) { printf("This line will appear on your screen\n"); } int f2 (int a, int b) { int r; r=pow(a,b); Return r;} value of r is returned to main() f2() returns an integer value of r calculated by f2() is stored in result

8
Factorial Write a program to calculate the factorial of an inputted number n n! = n * n-1 * n-2 * …..* 2 * 1 Examples: 5! = 120 4! = 24

9
Binomial Coefficient Write a program that calculates the binomial coefficient Three factorials are needed Calculate factorials through a function called factorial()

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google