1 ICS103 Programming in C Lecture 10: Functions II.

Slides:



Advertisements
Similar presentations
Computer Programming w/ Eng. Applications
Advertisements

Programming and Data Structure
Kernighan/Ritchie: Kelley/Pohl:
POINTER Prepared by MMD, Edited by MSY1.  Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference.
Lecture 2 Introduction to C Programming
Introduction to C Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
1 ICS103 Programming in C Lecture 13: Arrays II. 2 Outline Review on Arrays Using array elements as function arguments  Examples Using arrays as function.
1 ICS103 Programming in C Lecture 3: Introduction to C (2)
1 ICS103 Programming in C Lecture 13: Arrays II. 2 Outline Review on One-dimensional Arrays Using array elements as function arguments  Examples Using.
Function with Output Parameters 4 We have seen that functions can return a single value or no value (void return type) 4 It is quite often useful to be.
Functions Definition: Instruction block called by name Good design: Each function should perform one task and do it well Functions are the basic building.
CS 201 Functions Debzani Deb.
1 ICS103 Programming in C Lecture 12: Arrays I. 2 Outline Motivation for One-dimensional Arrays What is a One-dimensional Array? Declaring One-dimensional.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 14P. 1Winter Quarter Pointers Lecture 14.
C Stack Frames / Pointer variables Stack: Local Variables Pass & Return values Frame Ptr linkage (R5) and PC linkage (R7) Pointer Variables: Defining &
GE 211 Programming in C Matrix Dr. Ahmed Telba. Example Write function to take coefficients of quadratic equation a, b and c as input parameter and return.
Computer Science 210 Computer Organization Pointers.
Lecture No: 16. The scanf() function In C programming language, the scanf() function is used to read information from standard input device (keyboard).
Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.
Programming Variables. Named area in the computer memory, intended to contain values of a certain kind (integers, real numbers, characters etc.) They.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 14P. 1Winter Quarter Pointers Lecture 14.
Chapter 2 : Overview of C By Suraya Alias. /*The classic HelloWorld */ #include int main(void) { printf(“Hello World!!"); return 0; }
CP104 Introduction to Programming Modular Programming Lecture 16__ 1 Modular Programming II Functions with single output Functions with multiple outputs.
18-2 Understand “Scope” of an Identifier Know the Storage Classes of variables and functions Related Chapter: ABC 5.10, 5.11.
Chapter 7: Pointers Basic concept of pointers Pointer declaration Pointer operator (& and *) Parameter passing by reference.
1 ICS103 Programming in C Ch6: Pointers and Modular Programming.
NA2204.1jcmt CSE 1320 Intermediate Programming C Program Basics Structure of a program and a function type name (parameters) { /* declarations */ statement;
1 C Programming Week 2 Variables, flow control and the Debugger.
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman Chapter 6 (Pointers) © CPCS
1 CHAPTER 5 POINTER. 2 Pointers  Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference  Dynamic.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
1 Structures Etter Chapter 7 In engineering, we need to process or visualize data –Some of you have done Matlab simulations and visualizations Sometimes.
CSEB 114: PRINCIPLE OF PROGRAMMING Chapter 7: Pointers.
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
CSEB 114: PRINCIPLE OF PROGRAMMING Chapter 7: Pointers.
ICS103 Programming in C Lecture 11: Arrays I
+ Pointers. + Content Address of operator (&) Pointers Pointers and array.
Pointers PART - 2. Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the concept and use of pointers ❏ To be able to declare, define,
1 CSC103: Introduction to Computer and Programming Lecture No 16.
 Most C programs perform calculations using the C arithmetic operators (Fig. 2.9).  Note the use of various special symbols not used in algebra.  The.
Pointers. Addresses in Memory Everything in memory has an address. C allows us to obtain the address that a variable is stored at. scanf() is an example.
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.
1 Lecture Three I/O Formatting and Arithmetic Dr. Sherif Mohamed Tawfik.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Simple C Programs.
User-Written Functions
Computer Science 210 Computer Organization
CSE 220 – C Programming Pointers.
Input/output.
ICS103 Programming in C Lecture 10: Functions II
Functions, Part 2 of 2 Topics Functions That Return a Value
ICS103 Programming in C Lecture 3: Introduction to C (2)
INC 161 , CPE 100 Computer Programming
Input/Output Input/Output operations are performed using input/output functions Common input/output functions are provided as part of C’s standard input/output.
Computer Science 210 Computer Organization
Chapter 2 - Introduction to C Programming
ICS103 Programming in C Lecture 13: Arrays II
Functions, Part 2 of 3 Topics Functions That Return a Value
A function with one argument
ICS103 Programming in C Lecture 10: Functions II
FUNCTION ||.
ICS103 Programming in C Lecture 10: Functions II
Functions, Part 2 of 3 Topics Functions That Return a Value
ICS103: Programming in C 6: Pointers and Modular Programming
Presentation transcript:

1 ICS103 Programming in C Lecture 10: Functions II

2 Outline Introducing Functions that return multiple results What is a Pointer variable? Functions returning multiple results Triple use for Asterisk (*) More Examples

3 Introducing Functions that return multiple results So far, we know how to pass inputs into a function and how to use the return statement to send back at most one result from a function. However, there are many situations where we would like a function to return more than one result. Some Example are:  Function to convert time in seconds into hours, minutes and seconds  Function to find the quotient and remainder of a division  Function to return maximum, minimum and average from a set of values In this lecture, we discuss how a function can return more than one result, which is achieved through output parameters, which are pointer variables. Thus, to be able to write functions that return multiple results, we first need to learn about pointer variables.

4 What is a Pointer variable? A pointer variable is a special variable, that stores the address of other normal variable. If a pointer variable stores the address of a char variable, we call it a character pointer and so on. A normal variable directly contains a specific value. A pointer variable on the other hand, contains an address of a variable that contains a specific value. Pointers like any other variables must be declared before they can be used. A pointer variable is declared by preceding its name with an asterisk. Example: int *p; How can we initialize p? First we must have an integer variable, then we use the & operator to get the address of the variable and assign it to p. int n = 84; p = &n; Suppose that the int variable n is stored in the memory cell # 1024, then the following figure figure shows the relationship between n and p. n p

5 What is a Pointer variable? … A pointer variable such as p above, has two associated values: Its direct value, which is referenced by using the name of the variable, p. In this example, this value is We can print the direct value of a pointer variable using printf by using %p as the place holder. Its indirect value, which is referenced by using the indirection operator (*). So the value of *p is 84. n p ReferenceValue pPointer (1024) *p84

6 Example 1: The following example demonstrate the relationship between a pointer variable and the character variable it is pointing to. /* Shows the relationship between a pointer variable * and the character variable it is pointing to */ #include int main(void) { char g='z'; char c='a'; char *p; p=&c; printf("%c\n",*p); p=&g; printf("%c\n",*p); *p='K'; printf("%c\n",g); system("pause"); return 0; }

7 Functions returning multiple results As we saw in the last example, pointer variables allow us indirect access to variables (e.g *p = ‘K’) This ability to indirectly access a variable is the key to writing functions that return multiple results. We declare such functions to have pointer variables as their formal parameters. From the calling function, instead of passing values of variables as actual arguments, we pass addresses of these variables. This will allow the function to indirectly manipulate the variables of the calling function – thus achieving the desired effect.

8 Example 2: /* shows how function can return multiple results */ #include void test1(int m, int n); void test2(int *m, int *n); void test3(int a, int *b); int main(void) { int a=10, b=16; printf("a=%d, b=%d\n",a,b); test1(a,b); printf("a=%d, b=%d\n",a,b); test2(&a,&b); printf("a=%d, b=%d\n",a,b); test3(a,&b); printf("a=%d, b=%d\n",a,b); system("pause"); return 0; } void test1(int m, int n) { m=5; n=24; } void test2(int *m, int *n) { *m=5; *n=24; } void test3(int a, int *b) { a=38; *b=57; }

9 Triple use for Asterisk (*) We have now seen three distinct meanings of the symbol *. As Multiplication operator: x * y => x times y In declaration  * tells the compiler that a new variable is to be a pointer (read as “pointer to”)  Thus, in this case, it is a part of the name of the type of the variable. As unary indirection operator :  It provides the content of the memory location specified by a pointer. It mean “follow the pointer”.  It can also stand on the left side of an assignment.  Here the type depends on the variable being pointed – char in the above case.  It is a common mistake by students to interpret the above as a pointer type. int * p x = * p * p = ‘K’

10 Example 3: /* computes the area and circumference of a circle, given its radius */ #include void area_circum (double radius, double *area, double *circum); int main (void) { double radius, area, circum ; printf ("Enter the radius of the circle > ") ; scanf ("%lf", &radius) ; area_circum (radius, &area, &circum) ; printf ("The area is %f and circumference is %f\n", area, circum) ; system("pause"); return 0; } void area_circum (double radius, double *area, double *circum) { *area = 3.14 * radius * radius ; *circum = 2 * 3.14 * radius ; }

11 Example 4: /* Takes three integers and returns their sum, product and average */ #include void myfunction(int a,int b,int c,int *sum,int *prod, double *average); int main (void) { int n1, n2, n3, sum, product; double av_g; printf("Enter three integer numbers > "); scanf("%d %d %d",&n1, &n2,&n3); myfunction(n1, n2, n3, &sum, &product, &av_g); printf("\nThe sum = %d\nThe product = %d\nthe avg = %f\n",sum,product,av_g); system("pause"); return 0; } void myfunction(int a,int b,int c,int *sum,int *prod, double *average) { *sum=a+b+c; *prod=a*b*c; *average=(a+b+c)/3.0; }

12 Example 5: /* takes the coefficients of quadratic equation a, b and c and returns its roots */ #include void quadratic(double a,double b, double c, double *root1, double *root2); int main(void) { double a,b,c,r1,r2; printf("Please enter coefficients of the equation: [a b c] > "); scanf("%lf%lf%lf",&a,&b,&c); quadratic(a,b,c,&r1,&r2); printf("\nThe first root is : %f\n",r1); printf("The second root is : %f\n", r2); system("pause"); return 0; } void quadratic(double a,double b, double c, double *root1, double *root2) { double desc; desc =b*b-4*a*c; if(desc < 0) { printf("No real roots\n"); system("pause"); exit(0); } else { *root1=(-b+sqrt(desc))/(2*a); *root2=(-b-sqrt(desc))/(2*a); }

13 Example 6: /* swaps the values between 2 integer variables */ #include void readint(int *a, int* b); void swap (int *a, int *b); int main (void ) { int num1,num2; readint(&num1,&num2); printf("before swapping num1= %d, num2=%d\n",num1,num2); swap(&num1,&num2); printf("after swapping num1= %d, num2=%d\n",num1,num2); system("pause"); return 0; } void readint (int *a, int *b) { printf("enter first integer number > "); scanf("%d",a); printf("enter second integer number > "); scanf("%d",b); } void swap (int *a, int*b) { int temp; temp=*a; *a=*b; *b=temp; } Because a and b are pointer variables, we do not use the & operator for scanf.