FUNCTION ||.

Slides:



Advertisements
Similar presentations
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
Advertisements

BBS514 Structured Programming (Yapısal Programlama)1 Pointers.
1 CS 201 Pointers (2) Debzani Deb. 2 Overview Pointers Functions: pass by reference Quiz 2 : Review Q & A.
C Pointers Systems Programming. Systems Programming: Pointers 2 Systems Programming: 2 PointersPointers  Pointers and Addresses  Pointers  Using Pointers.
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.
1 Pointers ( מצביעים ). 2 Variables in memory Primitives Arrays.
Declarations/Data Types/Statements. Assignments Due – Homework 1 Reading – Chapter 2 – Lab 1 – due Monday.
1 ICS103 Programming in C Lecture 10: Functions II.
Programming Control Flow. Sequential Program S1 S2 S5 S4 S3 int main() { Statement1; Statement2; … StatementN; } Start End.
Programming Pointers. Variables in Memory x i c The compiler determines where variables are placed in memory This placement cannot.
12-2 Know how if and switch C statements control the sequence of execution of statements. Be able to use relational and logical operators in the conditional.
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.
CECS 121 EXAM 1. /* C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }
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.
NA2204.1jcmt CSE 1320 Intermediate Programming C Program Basics Structure of a program and a function type name (parameters) { /* declarations */ statement;
Programming C for Engineers An exercise is posted on the web site! Due in one week Single submission.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
UniMAP SemI-09/10EKT120: Computer Programming1 Week 5 – Functions (1)
Selection-making Decisions Selection allows you to choose between two or more possible program flow --- it lets you make decisions in your program. Examples.
C Programming - Structures. Structures containing arrays A structure member that is an array does not ‘behave’ like an ordinary array When copying a structure.
1 Unstructured Programming The Need for Procedures/Functions Procedural Programming Function Declaration/Prototypes/Invocation Example Functions Function.
Pointers PART - 2. Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer.
Department of Electronic & Electrical Engineering IO reading and writing variables scanf printf format strings "%d %c %f"
142 L -1 Pointers Chapter 6 C uses a call BY VALUE for functions for example: void add_one(int x, int y) { x=x+1; y=y+1; } int main(void) { int a,b; a=4;
2/23/2016Course material created by D. Woit 1 CPS 393 Introduction to Unix and C START OF WEEK 9 (C-3)
POINTERS IN C Pointer Basics, Pointer Arithmetic, Pointer to arrays and Pointer in functions.
 Real numbers representation - Floating Point Notation  First C Program  Variables Declaration  Data Types in C ◦ char, short, int, long, float, double,
LINKED LISTS.
Lesson #8 Structures Linked Lists Command Line Arguments.
EKT120: Computer Programming
User-Written Functions
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
CSE 220 – C Programming Pointers.
Lesson #6 Modular Programming and Functions.
Lesson #6 Modular Programming and Functions.
ICS103 Programming in C Lecture 10: Functions II
Functions, Part 2 of 2 Topics Functions That Return a Value
EPSII 59:006 Spring 2004.
Chapter 2 Overview of C.
INC 161 , CPE 100 Computer Programming
CS1010 Programming Methodology
C programming language
Arrays in C.
void Pointers Lesson xx
Lesson #6 Modular Programming and Functions.
Simple Data Types and Function Calls
CMSC202 Computer Science II for Majors Lecture 04 – Pointers
Pointers  Week 10.
Functions, Part 2 of 3 Topics Functions That Return a Value
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
Introduction to C Topics Compilation Using the gcc Compiler
A function with one argument
Conversion Check your class notes and given examples at class.
Lesson #6 Modular Programming and Functions.
Conversion Check your class notes and given examples at class.
Introduction to C Topics Compilation Using the gcc Compiler
Introduction to Problem Solving and Programming
ICS103 Programming in C Lecture 10: Functions II
DATA TYPES There are four basic data types associated with variables:
IPC144 Introduction to Programming Using C Week 5 – Lesson 1
C Pointers Systems Programming.
ICS103 Programming in C Lecture 10: Functions II
Functions, Part 2 of 3 Topics Functions That Return a Value
Presentation transcript:

FUNCTION ||

Functions with multiple results We know that functions can only have one result (one return statement), but we can trick a function into giving multiple results by using pointer parameters. Let's have a function that takes in a double number and give back 3 “results”: a sign, a whole part and a fraction. -3.1416 - 3 0.1416

Functions with multiple results The best thing to do next is to analyse our function by making a diagram of what goes in and of what comes out by labelling everything properly with their types. Notice the 3 arrows on the right side. Since a function can only have 1 "true" result, we will have to do something about the two "extra results". char sign double number int whole split double fraction

Functions with multiple results Of course, our function can only have one true result (one return), so we'll pick one (any one will do). Let's pick the sign as the result. Since the sign is a character, our function will be a char function. The two other “results” (the whole part and the fraction will be made accessible to the main program by the pointer parameters. The diagram on the next slide shows the outcome.

Functions with multiple results To get rid of the extra "results", we will pick one as the true result and the other two will be fake results. We then transfer the "fake" results to the left side but as pointers (we add a star to their names). Now our function has only one result, hence perfectly legal. double number int *whole split char sign double *fraction

Program--2 /* Problem: This program just demonstrates how to assign values to pointer variables. It serves no other purpose. */ #include <stdio.h> int main (void) { /* c and d are pointers to integers */ int a, b, *c, *d, e; a = 10; b = a * 3; c = &a; /* address of a goes into c */ d = &b; /* address of b goes into d */ e = *c + *d; /* *c is a and *d is b */ *d = a; d = &a; *c = *d - a % b + *c; printf ("%d hi \n",*c); printf ("Do you understand why\n"); printf ("a= %d, b= %d, e= %d ?\n", a, b, e); return (0); }

Passing Variables using Pointers – Call by Reference Returning a single value using pointer /* PROGRAM # 51 */ /* CALL BY REFERENCE*/ #include <stdio.h> /* Declare function SUM2 */ void SUM2(int num1, int num2, int *ptr); main( ){ int x, y, sum; int *psum; /* pointer to sum */ psum=∑ /* get the pointer to point to sum*/ printf(“Pls enter 2 int numbers: “); scanf(“%d%d”, &x,&y); /* NOTE: [1] According to the function prototype we need to provide 3 arguments where the 1st & the 2nd arguments are regular variable and the 3rd one should be a pointer. [2] The function is VOID, i.e., it does NOT return any value to the calling function. Therefore, when we call the function we don’t assign it to any variable */ /* call the function SUM2 */ SUM2(x, y, psum); printf("%d PLUS %d is: %d", x, y, sum); } /*Function SUM2, gets two integers and adds them */ void SUM2(int num1, int num2, int *ptr){ *ptr=num1+num2; }

Functions with multiple results int main (void) { double n, f; int w; char s; printf (“Enter a double number:”); scanf (“%lf”, &n); s = split (n, &w, &f); printf (“The sign is: %c\n”, s); printf (“The whole part is: %d\n”, w); printf (“The fraction is: %f\n”, f); return (0); } See that when a parameter is a pointer, you must send an address value to it.

Program--6 /* This function takes a real number and separates it into three parts: a sign (a character value), a whole part (an integer) and a fraction (double) */

Program--6 /* This function takes a real number and separates it */ /* into three parts: a sign (a character value), a */ /* whole part (an integer) and a fraction (double) */ #include <stdio.h> #include <math.h> /* the function returns the sign but the pointer parameters */ /* will modifiy the corresponding variable arguments in */ /* main program */ char separate (double number, int *w, double *f) { char s; double magnitude; /* the absolute value of the number */ /* testing if positive or negative and assignation of sign */ if (number < 0) s = '-'; else if (number == 0) s = ' '; else s = '+'; magnitude = fabs (number); *w = floor (magnitude); *f = magnitude - *w; return (s); }

Program--6 int main (void) { double n; /* the original number */ /* the variables that will be filled by the function */ char sign; int whole; double fraction; printf ("Enter a real number: "); scanf ("%lf", &n); /* calling the function */ sign = separate (n, &whole, &fraction); /* printing the report */ printf ("Parts of %f: \n", n); printf ("===============\n\n"); printf ("Sign is: %c \n", sign); printf ("Whole part is %d \n", whole); printf ("Fraction part is %f \n", fraction); return (0); }

/* PROGRAM # 56 */ /* DECOMPOSE FLOAT NUMBER INTO INT & FRAC. PARTS. */ /* VAR is the variable that the user inputs */ /* VAR_FRACTION is the fraction part */ /* VAR_INTEGER is the integer part */ #include<stdio.h> void decompose(float number, int *int_part, float *frac_part); main( ){ float VAR, VAR_FRACTION; int VAR_INTEGER; printf("Please enter a float number: "); scanf("%f", &VAR); /* Call the function decompose*/ decompose(VAR, &VAR_INTEGER, &VAR_FRACTION); printf("The float number is %f.\n", VAR); printf("It's integer part is %d.\n", VAR_INTEGER); printf("And it's fraction part is %f.\n", VAR_FRACTION); } /*Function decompose, takes a float number, decompose the number into integer part and fraction part using pointers */ void decompose(float number, int *int_part, float *frac_part){ *int_part=(int) number; *frac_part=number - *int_part; RUN: Please enter a float number: 123.4567 The float number is 123.4567. It's integer part is 123. And it's fraction part is 0.4567.

Program--7 /* Problem: This is simple addition reader/parser for 2 doubles. */ #include <stdio.h> /* function to read expression */ void readex (double *op1, double *op2) { char plus; scanf ("%lf%c%lf", op1, &plus, op2); } int main(void) double a, b, c; printf ("Enter an addition expression (eg: 4.5+3.5): "); readex (&a, &b); c=a+b; printf("%f + %f is %f.\n",a,b,c); return(0); /* can you expand this program to cover substractions? */

Pointer Application – SWAP /*SWAPPING 2 VARS USING POINTERS*/

/* PROGRAM # 52 */ /*SWAPPING 2 VARS USING POINTERS*/ #include <stdio.h> /* Declare functions SWAP and prt */ void SWAP(int *x, int *y); void prt(int A, int B); main( ){ int num1=5, num2=9; /* Declare pointers ptr1 and ptr2 */ int *ptr1, *ptr2; /* Initialize ptr1 to point to num1 and ptr2 to point to num2 */ ptr1=&num1; ptr2=&num2; /* Call functions ptr and SWAP */ prt(num1, num2); SWAP(ptr1, ptr2); } /* Function SWAP, swaps two integer numbers using pointers */ void SWAP(int *x, int *y){ int temp; temp=*x; *x=*y; *y=temp; } /*Function ptr, gets two integers, prints the value */ void prt(int A, int B){ printf(“The value of the 1st variable is %d AND the value of the 2nd variable is %d.\n”, A, B); }

fabs & abs fabs Syntax: #include <math.h> double fabs( double arg ); The function fabs() returns the absolute value of arg. abs Syntax: #include <stdlib.h> int abs( int num ); The abs() function returns the absolute value of num.

Functions with multiple results char split (double number, int *whole, double *fraction) { char sign; if (number < 0) sign = '-'; else if (number > 0) sign = '+'; sign = ' '; *whole = abs ((int)number); *fraction = fabs (number) - *whole; return (sign); }

Practice on - Cal1-Cal2 program given at the class on Friday