{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

# 5 - ICS103 Programming in C Lecture 5 Introduction to...

This preview shows pages 1–7. Sign up to view the full content.

1 ICS103 Programming in C Lecture 5: Introduction to Functions

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
2 Outline Introduction to Functions Predefined Functions and Code Reuse User-defined void Functions without Arguments Function Prototypes Function Definitions Placement of Functions in a Program Program Style
3 Introduction to Functions So far, we have learnt how to use operators, +, -, *, / and % to form simple arithmetic expressions. However, we are not yet able to write many other mathematical expressions we are used to. For example, we cannot yet represent any of the following expressions in C: C does not have operators for “square root”, “absolute value”, sine, log, etc. Instead, C provides program units called functions to carry out these and other mathematical operations.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
4 Introduction to Functions … A function can be thought of as a black box that takes one or more input arguments and produces a single output value. For example, the following shows how to use the sqrt function that is available in the standard math library: y = sqrt (x); If x is 16, the function computes the square root of 16. The result, 4, is then assigned to the variable y. The expression part of the assignment statement is called function call . Another example is: z = 5.7 + sqrt (w); If w = 9, z is assigned 5.7 + 3, which is 8.7.
5 Example /* Performs three square root computations */ #include <stdio.h> /* definitions of printf, scanf */ #include <math.h> /* definition of sqrt */ int main(void) { double first, second, /* input - two data values */ first_sqrt, /* output - square root of first input */ second_sqrt, /* output - square root of second input */ sum_sqrt; /* output - square root of sum */ printf("Enter a number> "); scanf("%lf", &first); first_sqrt = sqrt(first); printf("Square root of the number is %.2f\n", first_sqrt); printf("Enter a second number> "); scanf("%lf", &second); second_sqrt = sqrt(second); printf("Square root of the second number is %.2f\n", second_sqrt); sum_sqrt = sqrt(first + second); printf("Square root of the sum of the 2 numbers is %.2f\n",sum_sqrt); return (0); }

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
6 Predefined Functions and Code Reuse The primary goal of software engineering is to write error-free code.
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern