Introduction to Problem Solving and Programming

Slides:



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

1 ICS103 Programming in C Lecture 5: Introduction to Functions.
1 Lecture 6 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
Chapter 6: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
1 Chapter 7 Functions Dale/Weems/Headington. 2 Functions l Control structures l every C++ program must have a function called main l program execution.
1 Chapter 3 Topics Constants of Type int and float l Evaluating Arithmetic Expressions l Implicit Type Coercion and Explicit Type Conversion l Calling.
Chapter 6: User-Defined Functions I
Functions Modules in C++ are called functions and classes
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
 Introduction Introduction  Types of Function Types of Function  Library function Library function  User defined function User defined function 
Chapter 7 Functions.
Functions Lecture 4 – Section 2: 9/21/05 Section 4: 9/22/05.
1 Chapter 7 Functions. 2 Chapter 7 Topics l Writing a Program Using Functional Decomposition l Writing a Void Function for a Task l Using Function Arguments.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
1 Programming in C++ Dale/Weems/Headington Chapter 7 Functions.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
1 Functions every C++ program must have a function called main program execution always begins with function main any other functions are subprograms and.
Chapter 8 Functions. Chapter 8 Topics l Writing a Program Using Functional Decomposition l Writing a Void Function for a Task l Using Function Arguments.
1 ICS103 Programming in C Lecture 7: Introduction to Functions.
Functions Modules in C++ are called functions and classes Functions are block of code separated from main() which do a certain task every C++ program must.
1 Functions. 2 Chapter 7 Topics  Writing a Program Using Functional Decomposition  Writing a Void Function for a Task  Using Function Arguments and.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 7 Functions CS185/09 - Introduction to Programming Caldwell College.
Chapter 3 Arithmetic Expressions, Function Calls, and Output
1 Chapter 7 Functions Dale/Weems/Headington. 2 Chapter 7 Topics l Writing a Program Using Functional Decomposition l Writing a Void Function for a Task.
UniMAP SemI-09/10EKT120: Computer Programming1 Week 5 – Functions (1)
CECS 130 EXAM 1. To declare a constant (read only) value: const int x = 20; const float PI = 3.14; Can we do this? const int x;
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
CHAPTER 6 USER-DEFINED FUNCTIONS I. In this chapter, you will: Learn about standard (predefined) functions and discover how to use them in a program Learn.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Functions Programming Applications. Functions every C program must have a function called main program execution always begins with function main any.
Lecture 7: Modular Programming (functions) B Burlingame 05 October, 2016.
Chapter 8 Functions.
Chapter 9: Value-Returning Functions
EKT120: Computer Programming
Chapter 6 - Functions modular programming general function format
Chapter 6: User-Defined Functions I
Functions, Part 2 of 2 Topics Functions That Return a Value
Programming Fundamentals Lecture #7 Functions
Programmazione I a.a. 2017/2018.
Functions.
User-Defined Functions
2008/11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park
2011/11/20: Lecture 15 CMSC 104, Section 4 Richard Chang
Formatted and Unformatted Input/Output Functions
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
Functions Declarations CSCI 230
Functions I Creating a programming with small logical units of code.
Chapter 6 - Functions Outline 5.1 Introduction
Functions, Part 1 of 3 Topics Using Predefined Functions
Functions, Part 2 of 3 Topics Functions That Return a Value
A function with one argument
Chapter 6: User-Defined Functions I
Functions, Part 1 of 3 Topics Using Predefined Functions
In C Programming Language
Chapter 8 Functions.
Functions, Part 1 of 3 Topics Using Predefined Functions
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
Functions I Creating a programming with small logical units of code.
Functions, Part 2 of 3 Topics Functions That Return a Value
Functions, Part 2 of 3 Topics Functions That Return a Value
CPS125.
Presentation transcript:

Introduction to Problem Solving and Programming Lecture 4 – Functions

Outline What is a function Writing a Program Using Functional Decomposition Parameters Scope of variables Void functions vs. Value-Returning functions

Functions every C program must have a function called main program execution always begins with function main any other functions are subprograms and must be called

When a function is called, Then the flow of control passes to the first statement in the function’s body. The called function’s body statements are executed until one of these occurs: return statement (with or without a return value), or, closing brace of function body. Then control goes back to where the function was called.

Function Concepts Function Prototype Function Call Function Definition

What is in a prototype? int Cube( int ); /* prototype */ A prototype looks like a heading but must end with a semicolon; and its parameter list just needs to contain the type of each parameter. int Cube( int ); /* prototype */

Function Call a function call temporarily transfers control to the called function’s code when the function’s code has finished executing, control is transferred back to the calling block

Function Call Syntax FunctionName ( Argument List ) The argument list is a way for functions to communicate with each other by passing information. The argument list can contain 0, 1, or more arguments, separated by commas, depending on the function.

What is in a heading? int main ( ) float calc ( float x ) type of returned value name of function says no parameters int main ( ) float calc ( float x ) int countchar ( String s )

HEADER FILE FUNCTION EXAMPLE VALUE OF CALL <stdlib.h> abs(i) abs(-6) 6 <math.h> pow(x,y) pow(2.0,3.0) 8.0 fabs(x) fabs(-6.4) 6.4 <math.h> sqrt(x) sqrt(100.0) 10.0 sqrt(x) sqrt(2.0) 1.41421 <math.h> log(x) log(2.0) .693147 <stdio.h> printf( )

Program with Several Functions main function Square function Cube function

Program with Three Functions #include <stdio.h> int Square( int ); /* declares these functions */ int Cube( int ); Function Prototype: must be declared at top of program. int main( ) { int num = 3 ; int sq , cb ; sq = Square(num); printf(“ The square of 3 is %d \n “, sq ); cb = Cube(num); printf(“ The cube of 3 is %d \n “, cb ); return 0; } Function Call: invokes the function

Rest of Program int Square( int n ) { int s = n * n ; return s; } int Cube( int n ) int c = n * n * n ; return c; Function Definition: contains the actual function code.

A void function call stands alone #include <stdio.h> void DisplayMessage ( int n ) ; /*declares function */ int main( ) { DisplayMessage( 15 ) ; /* function call */ printf( “Good Bye \n “ ) ; return 0 ; }

A void function does NOT return a value void DisplayMessage ( int n ) { printf( “ I have liked math for %d years \n ” , n ); }

Two Kinds of Functions Value-Returning Void Always returns a single value to its caller and is called from within an expression. Never returns a value to its caller, and is called as a separate statement.

Example #include <stdio.h> int Cube ( int ) ; /* prototype */ void main ( ) { int yourNumber = 14; int myNumber = 9 ; printf( “My Number = %d \n “ , myNumber ) ; printf( “ its cube is %d \n “ , Cube (myNumber) ) ; printf( “Your Number = %d \n “ , yourNumber ) ; printf( “ its cube is %d \n “ , Cube (yourNumber) ) ; }

int Cube( int n ) { int c = n * n * n ; return c; }

Write a void function called DisplayMessage ( ) which you can call from main ( ) to describe the pollution index value it receives as a parameter. Your city describes a pollution Index less than 35 as “Pleasant”, 35 through 60 as “Unpleasant”, and above 60 as “Health Hazard.”

#include <stdio.h> void DisplayMessage (int); /* prototype */ int main ( ) argument { int pollutionIndex; printf(“ Enter pollution index : “); scanf( “ %d”, &pollutionIndex ); DisplayMessage(pollutionIndex); /* call */ return 0; }

parameter void DisplayMessage( int index ) { if ( index < 35 ) printf( “Pleasant” ) ; else if ( index <= 60 ) printf( “Unpleasant” ) ; else printf ( “Health Hazard” ); }

Parameter List The means used for a function to share information with the block containing the call

Classified by Location Arguments Parameters Always appear in a function call within the calling block. Always appear in the function heading, or function prototype.

Scope of Local and Global Variables

Example 1 Write a program that converts a temperature from Celsius to Fahrenheit and vice versa. Write the main function that accepts a conversion and temperature and calls the appropriate function. Celsius = (Fahrenheit-32)/1.8 Fahrenheit = 1.8*Celsius+32

Suggested Answer #include<stdio.h> float calc_celcius(float); float calc_fahrenheit(float); int main() { float temp, new_c, new_f; char conversion; printf(“Enter the temperature”); scanf(“%f”, &temp); printf(“Enter the conversion you wish to make ”); scanf(“%c”, &conversion); switch(conversion) case ‘C’: case ’c’: new_c=calc_celcius(temp); printf(“The temperature converted to celcius is %f”, new_c); break; }

Suggested Answer (contd.) case ‘F’: case ’f’: new_f=calc_fahrenheit(temp); printf(“The temperature converted to Fahren is %f”, new_f); break; default: printf(“Wrong input”); } return 0; float calc_celcius(float f) { float c; c = (f-32)/1.8; return c; float calc_fahrenheit (float c) float f; f = 1.8 *c+ 32; return f;

Example 2 Write a function that receives a positive integer & returns its factorial. Write a program that prints out the factorial of numbers 1 .. 20. (Use the function above )

Suggested Answer

Suggested Answer (contd.)

Example 3 Write a function that receives a positive integer & returns 1 if it is prime and 0 otherwise. Write a program that prints out all prime numbers 2-100 .

Suggested Answer

Suggested Answer (contd.)

Questions?