1 / 7

CS 101

CS 101. Lecture 5. Functions. In math, we write a function as f(x) = x 2 . If we want to write this same function in C++, we use the following syntax:

hidi
Download Presentation

CS 101

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS 101 Lecture 5

  2. Functions In math, we write a function as f(x) = x2 . If we want to write this same function in C++, we use the following syntax: double f(double x){return x*x;}The first double says that f returns a double, which means that when the call the function, i.e., when the expression f(a) occurs, e.g., on the right side of an assignment statementdouble z=f(a); where a is a double variable. We can also call f by writing cout<<f(a); or cout<<y+f(a); We can even nest calls of f as double z=f(f(a)); The return value of f is the double value that f(a) represents. We cannot write f(a)= 3, any more than we could write x+y – 3; The second double, in double x, is a declaration of the variable x. x is called a local variable of f, and also occurs inside the body of f which is {return x*x;}. If a is a double variable which is declared in main, when we call f by writing cout<<f(a); control passes from main to the body of f and the value of a is copied to the local variable x, then x is squared in x*x, and finally f(a) holds a copy of x*x. The variable x ceases to exist when control returns from f to main again.F is said to have one argument.Let us look at a program which calls f.

  3. Using a function in a program #include<iostream.h>double f(double);//function declaration or prototypemain(){double a=3.4;cout<<f(a)<<endl;}//function call. Prints 11.56 to screendouble f(double x){return x*x;}//function definitionNotice that it is not necessary to write x inside the function declaration. It is not wrong to do so, but we may write double x or double y, it makes no difference. We have made a point of distinguishing a from x, since a is called an actual parameter and x is called a formal or local parameter. A function must be declared before it is called. Here are some other ways that f can be declared defined and called.

  4. Alternate possibilities for declaring, defining and calling f. #include<iostream.h>double f(double x){return x*x;}/*counts as declaration and definition*/main(){Double a=3.4;cout<<f(a)<<endl;}Also#include<iostream.h>main(){double a=3.4;double f(double);//declaration of f – must come before f(a)cout<<f(a)<<endl;}double f(double x){return x*x;}//definition of ff cannot be defined inside main or any other function, but it can be declared.

  5. Functions with no arguments The main difference between math functions and C++ functions is that functions in C++ can DO things like printing out to the screen or to a file and math functions can’t. Here is an example of a function f with no arguments and no return.void f(){cout<<“This function has no arguments and no return.\n”;}You may have noticed that main has no arguments either, since we write main(). The return type of main is int, though we don’t have to write that for the g++ compiler. For other compilers, such as Borland or Visual C++, we have to write int main(). One of the most common errors is calling f as f; rather than the correct way which is f(); Notice that we can’t write cout<<f(); since f doesn’t return anything, nor can we write int z=f();

  6. Another program with function calls #include<iostream.h>int sum(int x,int y){return x+y;}int prod(int x,int y){return x*y;}main(){cout<<“Please enter two integers.\n”;int a,b;cin>>a>>b;cout<<“The sum of “<<a<<“ and “<<b<<“ is “<<sum(a,b)<<“ and the product is “<<prod(a,b)<<“.\n”;}

  7. Quiz 4 and solution Write a program which will use a function named sqs which will take three int arguments, return an int which is the sum of the squares of its three arguments. The values of the int variables which are to be passed to sqs are to be entered from the keyboard. Make sure you define sqs appropriately. #include<iostream.h>int sqs(int x, int y, int z){return x*x+y*y+z*z;}main(){cout<<“Please enter three integers.\n”;int a,b,c;cin>>a>>b>>c;cout<<“The sum of the squares of the integers you just entered is “<<sqs(a,b,c)<<“.\n”;} One way for main to use sqs is to print out its return value to the screen.

More Related