A function with one argument

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Advertisements

BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection.
1 Introduction to Computers and Programming Class 3 Introduction to C Professor Avi Rosenfeld.
Functions Lecture 4 – Section 2: 9/21/05 Section 4: 9/22/05.
CECS 121 EXAM 1. /* C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }
Functions Top-down design Breaking a complex problem into smaller parts that we can understand is a common practice. The process of subdividing a problem.
NA2204.1jcmt CSE 1320 Intermediate Programming C Program Basics Structure of a program and a function type name (parameters) { /* declarations */ statement;
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Functions (1)
UniMAP SemI-09/10EKT120: Computer Programming1 Week 5 – Functions (1)
Computer programming Outline Functions [chap 8 – Kochan] –Defining a Function –Arguments and Local Variables Automatic Local.
USER DEFINED FUNCTIONS Computer Programming Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
Modularity using Functions Chapter 4. Modularity In programming blocks of code often can be "called up" and reused whenever necessary, for example code.
Functions Programming Applications. Functions every C program must have a function called main program execution always begins with function main any.
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.
More on Functions. Assignment 1 Let’s talk … -We always validate to ensure that our program accepts only valid input. - This is done as early in the program.
 Real numbers representation - Floating Point Notation  First C Program  Variables Declaration  Data Types in C ◦ char, short, int, long, float, double,
Week 3.  TO PRINT NUMBERS FROM 1 TO 20  TO PRINT EVEN NUMBERS FROM 1 TO 20 2.
UMBC CMSC 104 – Section 01, Fall 2016
Arithmetic Expressions
EKT120: Computer Programming
User-Written Functions
Chapter 6: User-Defined Functions I
Introduction to Programming
EKT120 COMPUTER PROGRAMMING
Lesson #6 Modular Programming and Functions.
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Functions Chapter 5 CS12 - Computer Programming 1 Chapter 5.
Lesson #6 Modular Programming and Functions.
Functions, Part 2 of 2 Topics Functions That Return a Value
Functions Dr. Sajib Datta
Functions Department of Computer Science-BGU יום רביעי 12 ספטמבר 2018.
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
Lesson #6 Modular Programming and Functions.
Looping.
Functions Declarations CSCI 230
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Functions I Creating a programming with small logical units of code.
CS1100 Computational Engineering
Functions.
Functions, Part 1 of 3 Topics Using Predefined Functions
Exam 1 Date: Feb. 2nd, 2015 during class time (50 minutes) Coverage
Functions, Part 2 of 3 Topics Functions That Return a Value
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Conversion Check your class notes and given examples at class.
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Lesson #6 Modular Programming and Functions.
Conversion Check your class notes and given examples at class.
Functions, Part 1 of 3 Topics Using Predefined Functions
In C Programming Language
Introduction to Problem Solving and Programming
Functions Extra Examples.
Functions Department of Computer Science-BGU יום שישי 26 אפריל 2019.
Functions Imran Rashid CTO at ManiWeber Technologies.
Functions, Part 1 of 3 Topics Using Predefined Functions
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Functions I Creating a programming with small logical units of code.
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
FUNCTION ||.
Functions, Part 2 of 42 Topics Functions That Return a Value
Functions, Part 2 of 3 Topics Functions That Return a Value
CPS125.
Presentation transcript:

A function with one argument The stars function only displayed the same number of stars every time. What if I wanted to display a different number of stars that could be linked to a numeric value that I send to the function? All I would need is to add an argument to the function to specify the number of stars. stars2, like stars has no result. When a function has no result, we declare its type as void.

A function with one argument /* a void function returns nothing */ void stars2 (int n) { int i; /* a loop displaying a star at each iteration */ for (i=1; i<=n; ++i) printf ("*"); } /* change line after each series */ printf ("\n");

/* A void function with one argument */ #include <stdio.h> /* a void function returns nothing */ void stars2 (int n) { int i; /* a loop displaying a star at each iteration */ for (i=1; i<=n; ++i) printf ("*"); } /* change line after each series */ printf ("\n"); int main (void) int a; a=10; /* the argument may be a constant, a variable or an expression */ stars2 (20); stars2 (a); stars2 (a+2); return (0);

/* A function with a prototype-h3 */ /* You can put only the prototype (header) on top *//* and define the function at the bottom */ #include <stdio.h> /* the function prototype */ void stars2 (int n); /* the main program */ int main (void) { int a; a=10; /* the argument may be a constant, a variable or an expression */ stars2 (20); stars2 (a); stars2 (a+2); return (0); } /* the function definition */ void stars2 (int n) { int i; /* a loop displaying a star at each iteration */ for (i=1; i<=n; ++i) printf ("*"); /* change line after each series */ printf ("\n"); }

Q? #include<stdio.h> void f1(void) main() { printf(“before calling f1 \n”); f1(); printf(“after calling f1 \n”) } printf(“this is f1 notification \n”);

A function with one result A function that has a result, will have a type (char, double or int) and will finish with a return statement. The result in the return statement must always match the type declared on top of the function. See an example of a function calculating the factorial of a number on the next slide.

Things to know A function usually does not display anything on the screen (unless the function contains a printf statement). Variables declared inside a function are unknown in the main program. Variables of the main program are unknown inside the function. The only way to send values from the main program to a function is through an argument.

/* A function with a prototype-h3 */ /* You can put only the prototype (header) on top *//* and define the function at the bottom */ #include <stdio.h> /* the function prototype */ void stars2 (int n); /* the main program */ int main (void) { int a; a=10; /* the argument may be a constant, a variable or an expression */ stars2 (20); stars2 (a); stars2 (a+2); return (0); } /* the function definition */ void stars2 (int n) { int i; /* a loop displaying a star at each iteration */ for (i=1; i<=n; ++i) printf ("*"); /* change line after each series */ printf ("\n"); }

Things to know A function usually does not display anything on the screen (unless the function contains a printf statement). Variables declared inside a function are unknown in the main program. Variables of the main program are unknown inside the function. The only way to send values from the main program to a function is through an argument.

A function with one result /* this function will return an integer */ int factorial (int n) { int i, product; product = 1; /* initialization */ /* computes n*n-1... */ for (i=n; i>1; i=i-1) product = product * i; } /* the value that goes out */ return (product);

#include <stdio.h> int cps125(void); /*prototype declaration*/ main() { int num1,num2,mark; num1=98; num2=78; mark=cps125(); printf("%d %d %d\n",num1,num2,mark); } int cps125(void) printf("My CPS125 is :\n"); return 100;

/* The factorial function: one argument, one result –h4*/ #include <stdio.h> /* this function will return an integer */ int factorial (int n) { int i, product; product = 1; /* initialization */ /* computes n*n-1... */ for (i=n; i>1; i=i-1) product = product * i; } /* the value that goes out */ return (product); int main (void) int a, result; printf ("Enter an integer number: "); scanf ("%d", &a); /* calling the function */ result = factorial (a); /* printing the report */ printf ("The factorial of %d is %d.\n", a, result); return (0); } /* NOTE: a large number will produce an arithmetic overflow */

Function with two arguments A function with two arguments will require two parameters in the function header. Parameters are presented in a list separated by commas. Ex: double bigger (double n1, double n2) { double larger; if (n1 > n2) larger = n1; else larger = n2; return (larger); }

Remember The number of actual arguments used in a function call must be the same as the number of parameters listed in the function definition. The order of arguments used in a function call must match the order of parameters in the function definition. The types of arguments used in a function call must match the types of the corresponding parameters in the function definition.

/* The bigger function: two arguments, one result-h5 */ #include <stdio.h> /* a function taking two double numbers and returning the larger number of the two */ /* this function will return a double */ double bigger (double n1, double n2) { double big; if (n1>n2) big = n1; else big = n2; return (big); } int main (void) double x, y, result; printf ("Enter a real number: "); scanf ("%lf", &x); scanf ("%lf", &y); /* calling the function (2 arguments) */ result = bigger (x,y); /* printing the report */ printf ("The bigger number is %f.\n", result); return (0);

Case study – Is a number prime? A prime number is a number only divisible by itself or 1. A function that determines if a number is prime or not will accept an integer number as an argument and return a truth value (1 if prime or 0 if not prime). The solution is to try to find a divisor for the number. If unsuccessful, the number is prime. To find a divisor, we try all possible numbers to see if it is divisible. http://c.ihypress.ca/05.php(program#6,7)

Results A function can have only one result. A function can execute only one return statement. A return statement ends the function execution and gives the control back to the calling function (usually main).

Program! /*PROGRAM TO ADD TWO INTEGER NUMBERS */ By using function.

/* PROGRAM # 37-B */ /*PROGRAM TO ADD TWO INTEGER NUMBERS */ #include <stdio.h> /* Declare function Add2int */ int Add2int(int num1, int num2); main( ){ int var1, var2, sum; /* Input numbers and print them */ printf(“Pls enter 2 integer numbers: “); scanf(“%d %d”, &var1, &var2); printf("The variables are %d %d.\n", var1, var2); /* Call to function Add2int, print the result */ sum=Add2int(var1, var2); printf(“The result of addition is %d.\n”, sum); } /* Function adds two integers and returns an integer as result */ int Add2int(int num1, int num2){ return(num1 + num2);

Program!!! /*TO ADD TWO INTEGER NUMBERS */ /* USING GLOBAL VARIABLES*/

/* PROGRAM # 38 */ /*TO ADD TWO INTEGER NUMBERS */ /* USING GLOBAL VARIABLES*/ #include <stdio.h> /* Declare function and variables var1 and var2 */ int Add2int( ); int var1, var2; main( ){ int sum; /* Input numbers and print them */ printf(“Pls enter 2 integer numbers: “); scanf(“%d %d”, &var1, &var2); printf("The variables are %d %d.\n", var1, var2); /* Call to function Add2int, print the result */ sum=Add2int( ); printf(“The result of addition is %d.\n”, sum); } /* Function adds two integer numbers and returns an integer as result */ int Add2int( ){ return(var1+var2);

Calling C Functions Functions in C are: Prototyped before main( ) Defined after main( ) Called within any function such as main( ) How to call a function: LHS = function_name ( values ); Values are passed to the function & the returned value is assigned to LHS.

/* PROGRAM #39 */ /* Write a program that asks for an integer number and then uses a function called FACT to calculate the factorial of the given number. The function accepts the number as the argument and returns the result to the main. Assume that user will inputs only positive integer numbers */

/* PROGRAM #39 */ #include<stdio.h> /* Declare function FACT */ int FACT(int num); main( ){ int anumber, factorial; /* Input an integer number */ printf(“Pls input an integer number: “); scanf(“%d”, &anumber); /* Calculate the factorial of the given number by calling function FACT, then print the result */ factorial=FACT(anumber); printf(“%d! is %d.\n”, anumber, factorial); } /* Function FACT, gets an integer number calculates its factorial , returns an integer as result */ int FACT(int num){ int i, fact=1; for(i=1; i<=num; i++) fact*=i; return(fact);

/* PROGRAM # 40 */ HOMEWOrk! Write a program that asks for 2 int numbers and then uses a function called CalSum to calculate the sum of the number bet. these 2 numbers (inclusive). The program will work even if the 1st number is greater than the 2nd number.

/* PROGRAM # 41 */ HOMEWORK!! Write a program that asks for three int numbers, A, B and C. The program then passes these 3 variables to a function called MyAverage that will ask the user to input A int numbers (eg, if A is 5 then user needs to input 5 integer numbers. If these int numbers fall between the interval defined by B and C, then their average is calculated and the result will be returned to the main.

/* PROGRAM # 37 */ Homework! /*PROGRAM TO ADD TWO INTEGER NUMBERS */ #include <stdio.h> /* Declare function Add2int */ int Add2int( ); main( ){ int var1, var2, sum; /* Input numbers and print them */ printf(“Pls enter 2 integer numbers: “); scanf(“%d %d”, &var1, &var2); printf("The variables are %d %d.\n", var1, var2); /* Call to function Add2int, print the result */ sum=Add2int( ); printf(“The result of addition is %d.\n”, sum); } /* Function adds two integers and returns an integer as result */ int Add2int( ){ return(var1+var2); Q) Does this program work properly?