Lab 8 User Defined Function.

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Advertisements

C Functions. What are they? In general, functions are blocks of code that perform a number of pre-defined commands to accomplish something productive.
Chapter Five Functions
1 Lecture 16:User-Definded function I Introduction to Computer Science Spring 2006.
Call By Address Parameters (Pointers) Chapter 5. Functions that “return” more than a single value What if we need more than one value to be returned from.
Write a function to calculate the cubic function: y = 4x 3 + 2x 2 –5x – 4 The function should return y for any given value of x. Question One #include.
What is shape function ? shape function is a function that will give the displacements inside an element if its displacement at all the node locations.
Write a program step by step. Step 1: Problem definition. Given the coordinate of two points in 2-D space, compute and print their straight distance.
Modular Programming With Functions
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
By Senem Kumova Metin 1 POINTERS + ARRAYS + STRINGS REVIEW.
Appendix B Solving Recurrence Equations : With Applications to Analysis of Recursive Algorithms.
Software Development Method. Assignments Due – Homework 0, Warmup Reading – Chapter 2 –
1 Modularity In “C”. 2 General Syntax data_type function_Name (parameter list) { … return expression; // return output } Body of the function is a compound.
Parameter Passing to Functions in C. C Parameter passing Review of by-value/by-reference.
Declarations/Data Types/Statements. Assignments Due – Homework 1 Reading – Chapter 2 – Lab 1 – due Monday.
Graph the function y = |4 cos x|
LAB 2 : PROBLEM SOLVING TECHNIQUES, ALGORITHM: PSEUDO CODE AND FLOWCHART LAB INTRODUCTION Prepared by: Cik Noor Syazana bt Arshad.
CSCI 130 Chapter 5 Functions. Functions are named uniquely Performs a specific task Is independent –should not interfere with other parts of program May.
Array Cs212: DataStructures Lab 2. Array Group of contiguous memory locations Each memory location has same name Each memory location has same type a.
Nested LOOPS.
CMSC 1041 Functions II Functions that return a value.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
NA2204.1jcmt CSE 1320 Intermediate Programming C Program Basics Structure of a program and a function type name (parameters) { /* declarations */ statement;
Calculations Chapter 11 Library of math functions, and constants cos, sin, tan, abs, min, max, log, random, sqrt, pow, exp Constants.PI,.E Use care with.
PHYS 2020 Basic C An introduction to writing simple but useful programs in C In these lectures I will take you through the basics of C, but you will need.
Chapter 5 – Functions II Outline Recursion Examples Using Recursion: The Fibonacci Series.
CSC1030 HANDS-ON INTRODUCTION TO JAVA Introductory Lab.
Procedural Programming Criteria: P2 Task: 1.2 Thomas Jazwinski.
Sudeshna Sarkar, IIT Kharagpur 1 Functions Lecture
1 10/18/04CS150 Introduction to Computer Science 1 Functions Divide and Conquer.
Computer programming Outline Functions [chap 8 – Kochan] –Defining a Function –Arguments and Local Variables Automatic Local.
How to design and code functions Chapter 4 (ctd).
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
7/31: Math.random, more methods About DrawLine.java modifications –allow user input –draw a curve Method definitions Math.random()
Function prototype A function must be declared before it can be referenced. One way to declare a function is to insert a function prototype before the.
Modularity using Functions Chapter 4. Modularity In programming blocks of code often can be "called up" and reused whenever necessary, for example code.
1 CSC103: Introduction to Computer and Programming Lecture No 16.
Simple “VICO” (“VIPO”) Programs (Variables, Input, Calculating or Processing, Output)
Functions Venkatesh Ramamoorthy 21-March Examples #include double y ; … y = sin(45*3.1416/180) ; std::cout
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
1 CSC103: Introduction to Computer and Programming Lecture No 17.
Sin x = Solve for 0° ≤ x ≤ 720°
NOTE: C programs consist of functions one of which must be main. C programs consist of functions one of which must be main. Every C program begins executing.
CCSA 221 Programming in C CHAPTER 3 COMPILING AND RUNNING YOUR FIRST PROGRAM 1 ALHANOUF ALAMR.
The Binomial Theorem Section 9.2a!!!. Powers of Binomials Let’s expand (a + b) for n = 0, 1, 2, 3, 4, and 5: n Do you see a pattern with the binomial.
Looping I (while statement). CSCE 1062 Outline  Looping/repetition construct  while statement (section 5.1)
User-Written Functions
Functions, Part 2 of 2 Topics Functions That Return a Value
Functions in C Mrs. Chitra M. Gaikwad.
C++ Arrays.
2008/11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park
2011/11/10: Lecture 21 CMSC 104, Section 4 Richard Chang
CSCE 206 Lab Structured Programming in C
Functions, Part 2 of 3 Topics Functions That Return a Value
Cs212: DataStructures Computer Science Department Lab 3 : Recursion.
Exercise Solution First questions What's output What's input
Extra Practice for Recursion
In C Programming Language
CSCE 206 Lab Structured Programming in C
CSCE 206 Lab Structured Programming in C
Functions, Part 2 of 3 Topics Functions That Return a Value
Functions, Part 2 of 42 Topics Functions That Return a Value
Functions, Part 2 of 3 Topics Functions That Return a Value
CPS125.
Presentation transcript:

Lab 8 User Defined Function

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

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

Function example #include <stdio.h> #include <math.h> 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()

no variable values are exchanged between main() and f1() Function example: f1() #include <stdio.h> #include <math.h> 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()

Function example: f2() #include <stdio.h> #include <math.h> 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); } a and b are two integer inputs to function f2() f2() is given two integer inputs r is defined

Function example: f2() contd #include <stdio.h> #include <math.h> 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 calculated by f2() is stored in result f2() returns an integer value of r is returned to main()

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

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