# 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. Reusing code that has already been written &
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 05/09/2010 for the course ICS 103 taught by Professor Baleh during the Spring '10 term at Abilene Christian University.

### Page1 / 19

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

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online