Programming · •Function declaration •Function Call •Function definition . Function...

35
Introduction to Functions Programming

Transcript of Programming · •Function declaration •Function Call •Function definition . Function...

Page 1: Programming · •Function declaration •Function Call •Function definition . Function declaration Syntax: function_type function_name(arguments list); Example: int add(int , int);

Introduction to Functions

Programming

Page 2: Programming · •Function declaration •Function Call •Function definition . Function declaration Syntax: function_type function_name(arguments list); Example: int add(int , int);

Introduction to Functions

• A complex problem is often easier to solve by dividing it into several smaller parts, each of which can be solved by itself.

• This is called structured programming.

• These parts are sometimes made into

functions in C++.

• main() then uses these functions to solve the original problem.

Page 3: Programming · •Function declaration •Function Call •Function definition . Function declaration Syntax: function_type function_name(arguments list); Example: int add(int , int);

Advantages of Functions

• Functions separate the concept (what is done) from the implementation (how it is done).

• Functions make programs easier to understand.

• Functions can be called several times in the same program, allowing the code to be reused.

Page 4: Programming · •Function declaration •Function Call •Function definition . Function declaration Syntax: function_type function_name(arguments list); Example: int add(int , int);

C++ Functions

• C++ allows

– Standard functions.

– user-defined function

• Standard functions (e.g., sqrt, etc.) are usually grouped into specialized libraries (e.g., math,

etc.)

Page 5: Programming · •Function declaration •Function Call •Function definition . Function declaration Syntax: function_type function_name(arguments list); Example: int add(int , int);

User-Defined Functions

• C programs usually have the following form:

// include statements

// function prototypes

// main() function

// function definitions

Page 6: Programming · •Function declaration •Function Call •Function definition . Function declaration Syntax: function_type function_name(arguments list); Example: int add(int , int);

Function Input and Output

Page 7: Programming · •Function declaration •Function Call •Function definition . Function declaration Syntax: function_type function_name(arguments list); Example: int add(int , int);

ELEMENTS OF USER-DEFINED FUNCTION

• In order to make use of user-defined functions, we need to establish three elements that are related to functions.

• Function declaration

• Function Call

• Function definition

Page 8: Programming · •Function declaration •Function Call •Function definition . Function declaration Syntax: function_type function_name(arguments list); Example: int add(int , int);

Function declaration

Syntax:

function_type function_name(arguments list);

Example:

int add(int , int);

Page 9: Programming · •Function declaration •Function Call •Function definition . Function declaration Syntax: function_type function_name(arguments list); Example: int add(int , int);

Function call

The program that calls the function is referred to as the calling program or calling functions

Syntax: function_name(actual parameters); Example: add(a,b);

Page 10: Programming · •Function declaration •Function Call •Function definition . Function declaration Syntax: function_type function_name(arguments list); Example: int add(int , int);

Function definition

The function definition is an independent program module that is specially written or implement the requirements of the function.

Page 11: Programming · •Function declaration •Function Call •Function definition . Function declaration Syntax: function_type function_name(arguments list); Example: int add(int , int);

Definitions

• Parameter:– a declaration of an identifier within the '()' of a function declaration

• Used within the body of the function as a variable of that function

• Initialized to the value of the corresponding argument.

• Argument:– an expression passed when a function is called; becomes the initial value of the corresponding parameter

Page 12: Programming · •Function declaration •Function Call •Function definition . Function declaration Syntax: function_type function_name(arguments list); Example: int add(int , int);

• Two ways for a subprogram to gain access to the data for it to process

– Direct access to non-local variables

– Parameter passing

• A formal parameter is a dummy variable listed in subprogram header and used in the subprogram

– Usually bound to storage when subprogram is called

• An actual parameter represents a value or address used in the subprogram call statement

Page 13: Programming · •Function declaration •Function Call •Function definition . Function declaration Syntax: function_type function_name(arguments list); Example: int add(int , int);

Types of functions 1) Function with no arguments & no return

value. – void sub( );

2) Function with arguments & no return value. – void sub(int x , int y);

3) Function with arguments & return one value. – int sub(int x , int y);

4) Function with no arguments & return one value. – int sub( );

Page 14: Programming · •Function declaration •Function Call •Function definition . Function declaration Syntax: function_type function_name(arguments list); Example: int add(int , int);

Function with no arguments & no return value.

void sub( );

Int main( )

{

sub();

return 0;

}

defination

void sub()

{

int a=50;

int b=40;

int c;

c=a-b;

cout<<“subtraction is ”<<c;

}

Page 15: Programming · •Function declaration •Function Call •Function definition . Function declaration Syntax: function_type function_name(arguments list); Example: int add(int , int);

A function with no arguments and no return value

Called function does not have any arguments Not able to get any value from the calling function Not returning any value There is no data transfer between the calling function and called function.

void printline(); Int main() { cout<<"Welcome to function in C“; printline(); cout<<"Function easy to learn."; printline(); return 0; } void printline() { int i; cout<<"\n"; for(i=0;i<30;i++) { cout<<"-"; } cout<<"\n“; }

Page 16: Programming · •Function declaration •Function Call •Function definition . Function declaration Syntax: function_type function_name(arguments list); Example: int add(int , int);

Function with no argument & one return value

int sub( );

int main( )

{

int r;

r=sub( );

Cout<<"Sub is “<<r;

return 0;

}

int sub( )

{

int x , y , s=0;

Cout<<"Enter x & y";

Cin>>x>>y;

s=x-y;

return s;

}

Page 17: Programming · •Function declaration •Function Call •Function definition . Function declaration Syntax: function_type function_name(arguments list); Example: int add(int , int);

A function with no arguments and a return value

Does not get any value from the calling function Can give a return value to calling program

int send(); int main() { int z; z=send(); cout<<"\nYou entered :“<<z; return 0; } int send() { int no1; cout<<"Enter a no: "; cin>>no1; return no1; }

Enter a no: 46 You entered : 46.

Page 18: Programming · •Function declaration •Function Call •Function definition . Function declaration Syntax: function_type function_name(arguments list); Example: int add(int , int);

Function with arguments & no return value

void sub(int a , int b); int main( ) { int x , y; Cout<<"Enter the value of x "; Cin>>x; Cout<<"Enter the value of y "; Cin>>y; sub(x,y); /* function call with actual

arguments */ Return 0; }

void sub(int a , int b)

{

int s;

s=a-b;

cout<<"Sub is <<s;

}

Page 19: Programming · •Function declaration •Function Call •Function definition . Function declaration Syntax: function_type function_name(arguments list); Example: int add(int , int);

Function with arguments & no return value

void add(int x, int y); int main() { add(30,15); add(63,49); add(952,321); return 0; } void add(int x, int y) { int result; result = x+y; cout<<"Sum of “<<x<< “and”<<y <<“is<<result<<“\n\n"); }

Page 20: Programming · •Function declaration •Function Call •Function definition . Function declaration Syntax: function_type function_name(arguments list); Example: int add(int , int);

Functions with arguments & return one value

int sub(int x , int y); int main( ) { int x , y , r; Cout<<"Enter x "); Cin>>x; Cout<<"Enter y "; Cin>>y; r=sub(x,y); Cout<<"Sum sub is“<<r; return 0; }

int sub(int x , int n)

{

int s;

s=x-n;

return s;

}

Page 21: Programming · •Function declaration •Function Call •Function definition . Function declaration Syntax: function_type function_name(arguments list); Example: int add(int , int);

Functions with arguments & return one value

Result 85. Result 1273.

int add(int x,int y); int main() { int z; z=add(952,321); cout<<"Result \n\n“<< add(30,55)); cout<<"Result \n\n“<<z; return 0; } int add(int x,int y) { int result; result = x + y; return result; }

Send 2 integer value x and y to add() Function add the two values and send back the result to the calling function int is the return type of function Return statement is a keyword and in bracket we can give values which we want to return.

Page 22: Programming · •Function declaration •Function Call •Function definition . Function declaration Syntax: function_type function_name(arguments list); Example: int add(int , int);

Global and local variable scope

Page 23: Programming · •Function declaration •Function Call •Function definition . Function declaration Syntax: function_type function_name(arguments list); Example: int add(int , int);

Global and local variable scope

Page 24: Programming · •Function declaration •Function Call •Function definition . Function declaration Syntax: function_type function_name(arguments list); Example: int add(int , int);

Local and global scope

A program can have same name for local and global variables but value of local variable inside a function will take preference output 10

Page 25: Programming · •Function declaration •Function Call •Function definition . Function declaration Syntax: function_type function_name(arguments list); Example: int add(int , int);

Example

#include<iostream> int a; void add(); int main() { a=100; add(); return 0; } void add() { int b=100; cout<<"sum of two variable is“<< a+b; }

Page 26: Programming · •Function declaration •Function Call •Function definition . Function declaration Syntax: function_type function_name(arguments list); Example: int add(int , int);

The Return Statement

• The return statement exits the called function and returns control back to the calling function. – Once a return statement is executed, no further instructions within the

function are executed.

• A single return value ( of the appropriate type ) may be returned. – Parentheses are allowed but not required around the return value. – A function with a void return type will not have a return value after the return

statement.

• More than one return statement may appear in a function, but only one will ever be executed by any given function call. – ( All returns other than the last need to be controlled by logic such as "if"

blocks. )

• If a function does not contain a return statement, most compilers will add one automatically at the end of the routine, and may generate a warning message. The return value, if any, is undefined in this case.

Page 27: Programming · •Function declaration •Function Call •Function definition . Function declaration Syntax: function_type function_name(arguments list); Example: int add(int , int);

Void Functions

• If a function does not return a value, then a special "TYPE" is used to tell the computer this. The return type is "void" (all lower case).

• Void functions are mostly used in two classes of functions. • The first is a function that prints information for the user to read.

For example (for our purposes), the cout function is treated as a void function. (In actuality, cout returns an integer which is the number of characters printed... but we almost always ignore this value.)

• The second use of void functions is with "reference" parameters (e.g., Arrays). A reference parameter is not a copy of the input data, as is so often the case. A reference parameter is an "alias" for the same bucket in memory as the input data. Thus any change made to a reference parameter is in fact made to the original variable!

Page 28: Programming · •Function declaration •Function Call •Function definition . Function declaration Syntax: function_type function_name(arguments list); Example: int add(int , int);

Nested Functions #include<iostream>

void display();

int greater(int x, int y);

int main()

{

display();

return 0;

}

void display() { int a,b, ans; cout<<"enter a:"; cin>>a; cout<<"enter b:"; cin>>b; ans=greater(a,b); cout<<“answer is:”<<ans; } int greater(int x, int y) { if(x>y) return x; else return y; }

Page 29: Programming · •Function declaration •Function Call •Function definition . Function declaration Syntax: function_type function_name(arguments list); Example: int add(int , int);

Passing values.

• Passing values is known as call by value. You actually pass a copy of the variable to the function. If the function modifies the copy, the original remains unaltered.

Page 30: Programming · •Function declaration •Function Call •Function definition . Function declaration Syntax: function_type function_name(arguments list); Example: int add(int , int);

Passing values.

void doit(int x) { x=5; } Int main() { int z=27; doit(z); cout<<"z is now”<<z; return 0; }

Page 31: Programming · •Function declaration •Function Call •Function definition . Function declaration Syntax: function_type function_name(arguments list); Example: int add(int , int);

Pass by reference

void foo(int &y); void foo(int &y) // y is now a reference { cout << "y = " << y << endl; y = 6; cout << "y = " << y << endl; } // y is destroyed here int main() { int x = 5; cout << "x = " << x << endl; foo(x); cout << "x = " << x << endl; return 0; }

Page 32: Programming · •Function declaration •Function Call •Function definition . Function declaration Syntax: function_type function_name(arguments list); Example: int add(int , int);

Example

Before: Value of a = 10 and value of b = 20 Value of a (p1) = 20 and value of b(p2) = 10 After: Value of a = 10 and value of b = 20

void swap(int p1, int p2);

int main()

{

int a=20;

int b=40;

cout<<“value of a \n”<<a<<“and value of b”<<b;

swap(a,b);

cout<<“value of a \n”<<a<<“and value of b”<<b;

return 0;

}

void swap(int p1, int p2)

{

int temp;

temp=p2;

p2=p1;

p1=temp;

cout<<“value of a \n”<<p1<<“and value of b”<<p2;

}

Page 33: Programming · •Function declaration •Function Call •Function definition . Function declaration Syntax: function_type function_name(arguments list); Example: int add(int , int);

Printing the Diamond Pattern as a Function void diamond(int size) //number of rows

{

int row, space, star;

for(row=1; row<=size; row++){ //top half

for(space=1; space<=size-row; space++)

cout<<" “;

for(star=1; star<=2*row-1; star++)

cout<<"*“;

cout<<“\n” ;

}

for(row=size-1; row>=1; row--){ //bottom half

for(space=1; space<=size-row; space++)

cout<<" “;

for(star=1; star<=2*row-1; star++)

cout<<"*“;

cout<<“\n” ;

}

Page 34: Programming · •Function declaration •Function Call •Function definition . Function declaration Syntax: function_type function_name(arguments list); Example: int add(int , int);

Calculating the Area of a Circle with a Function

Page 35: Programming · •Function declaration •Function Call •Function definition . Function declaration Syntax: function_type function_name(arguments list); Example: int add(int , int);

Write a C function which check that number is prime or not and call it into the main function.??

int main() { int n, result; cout<<"enter an interger:"; cin>>n; result=check_prime(n); if(result==1) cout<<"is prime:\n“<<n; else cout<<"not prime\n",n; return 0; }

int check_prime(int a) { int c; for(c=2; c<=a-1; c++) { if(a%c==0) return 0; } if(a%c!=0) return 1; }