Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC1201: Programming Language 2 1 Functions. 2 Function declaration: return_type FuncName( Type arg1, Type arg2,….. Type argN) { function body } A program.

Similar presentations


Presentation on theme: "CSC1201: Programming Language 2 1 Functions. 2 Function declaration: return_type FuncName( Type arg1, Type arg2,….. Type argN) { function body } A program."— Presentation transcript:

1 CSC1201: Programming Language 2 1 Functions

2 2 Function declaration: return_type FuncName( Type arg1, Type arg2,….. Type argN) { function body } A program can contain one or many functions Must always have a function called “main”. The main function is the starting point of all C++ programs The compiler will not compile the code unless it finds a function called “main” within the program. A program can contain one or many functions Must always have a function called “main”. The main function is the starting point of all C++ programs The compiler will not compile the code unless it finds a function called “main” within the program. A Function is a group of statements that together perform a task. It can be used anywhere in the program. Why we need function? – Organize code in program – Code are easier to maintain?

3 “Hello World” program 3 #include using namespace std; int main () ‏ { cout << “Hello World\n”; Return 0; }

4 Function 4 When we need function? – When you need to repeat the same process over and over in a program. – The function can be called many times but appears in the code once.

5 1- Predefined functions 5  Predefined functions are functions that are built into C++ Language to perform some standard operations.  The C++ standard library provides numerous built-in functions that your program can call. For example, function strcat() to concatenate two strings  the definitions have been written and it is ready to be used.  – User needs to include pre-defined header file (i.e. math.h, time.h)

6 2- User defined functions 6  Function that been created by the user.  – This functions need to be declared and defined by the user

7 functions 7  Value returning functions:  functions that have a return type.  These functions return a value of a specific data type using the return statement.  Void functions:  functions that do not have a return type.  These functions do not use a return statement to return a value.

8 Value returning functions 8 The syntax is: FuncType FuncName(formal parameter list )‏ { statements }

9 Void functions 9 The syntax is: Void FuncName ( formal parameter list )‏ { statements }

10 Examples: 10 1. Write a Function larger, which returns the larger of the two given integers. 2. Write a Function Square, which returns the square of the given integer. 3. Write a function number_type. The function should output the number and message saying whether the number is positive, negative, or zero.

11 Example: With return value 11 Double larger ( double x, double y )‏ { double max; if ( x >= y )‏ max = x; else max = y; return max; } Function Call …….. Cout << “The larger of 5 and 6 is “ << larger(5, 6) << endl; ……….

12 Example: With return value 12 #include using std::cin; using std::cout; using std::endl; int square (int x)‏ { return x*x; } int main ( )‏ { int number; cout<<"Enter any number to Calculate the square of this number "; cin>>number; cout<<endl; cout<<"the square of "<<number<<" is " <<square(number)<<endl; return 0; }

13 Example: Without return value 13 Void number_type ( int x) ‏ { if ( x > 0 ) ‏ cout << x << “ is positive.” << endl; else if ( x < 0 ) ‏ cout << x << “ is negative.” << endl; else cout<< x << “is a zero.”<<endl; } Function Call …….. Number_type( 5 ); ……….


Download ppt "CSC1201: Programming Language 2 1 Functions. 2 Function declaration: return_type FuncName( Type arg1, Type arg2,….. Type argN) { function body } A program."

Similar presentations


Ads by Google