Download presentation
Presentation is loading. Please wait.
Published byAdele Small Modified over 6 years ago
1
Functions Chapter 5 CS12 - Computer Programming 1 Chapter 5
2
Objectives Understand the concept of functions in C
Learn and include function prototype and function definition in C program Know what is global/external and local variables, when to include them in C program Appreciate the purpose of parameter passing and how it works in C program Learn and understand how to incorporate functions in program for validation check Chapter 5 CS12 - Computer Programming 1
3
Why functions are used in C?
to avoid writing the same instructions repeatedly make it easier to organize programs and keep track of what they were doing each subroutine (function) could be written and checked out more or less independently the variables in each subroutine were protected from inadvertent tampering by other subroutines Chapter 5 CS12 - Computer Programming 1
4
The Structure of Functions
There are three program elements involved in using a function: The function definition The function itself is referred to as function definition The call to the function The function is called by main() by using its name including the parenthesis that following the name The function prototype A function is declared in a similar way at the beginning of a program before it is called Chapter 5 CS12 - Computer Programming 1
5
Chapter 5 CS12 - Computer Programming 1
6
The Function Definition
A function definition has following syntax: <type> <function name>(<parameter list>){ <local declarations> <sequence of statements> return <expression> } A function returns a single result One of the statements in the function body should have the form: return <expression>; The value passed back by return should have the same type as the function. Chapter 5 CS12 - Computer Programming 1
7
How to use or call a function?
A function call has the following syntax: <function name>(<parameter list>) There is a one-to-one correspondence between the parameters in a function call and the parameters in the function definition. Chapter 5 CS12 - Computer Programming 1
8
Function Prototype The function prototype has the following syntax: <type> <function name>(<type list>); The function prototype declares the interface, or input and output parameters of the function, leaving the implementation for the function definition. Chapter 5 CS12 - Computer Programming 1
9
Local Variables Functions which are known only within that function and other functions are not allowed to use them Also known as automatic variables The length of time a variable lasts is called lifetime. Chapter 5 CS12 - Computer Programming 1
10
Functions that Return a Value
A function that uses no arguments but returns a value is a slightly more complicated kind of function: a function that returns a value. Chapter 5 CS12 - Computer Programming 1
11
Using Arguments to Pass Data to a Function
The mechanism used to convey information to a function is the argument In the function definition, a variable name could be placed in the parentheses Chapter 5 CS12 - Computer Programming 1
12
Passing Multiple Arguments
We can pass as many arguments as we like to function The value of the first actual argument in the calling program is assigned to the first formal argument in the function, and the value of the second actual argument is assigned to the second formal argument Chapter 5 CS12 - Computer Programming 1
13
‘Void’ type functions A function returns a single result, but a void type function does not return anything (but does something, a sub-program)! Example (void): Printing Cards #include <stdio.h> void printcard(int); int main(){ int c1, c2, c3, c4, c5; printcard(c1); printcard(c2); printcard(c3); printcard(c4); printcard(c5); } void printcard(int cardnum){ if(cardnum==1) Printf("A“); else if(cardnum>=2 && cardnum<=10) Printf(“%d”, cardnum); else if(cardnum==11) Printf("J“); else if(cardnum==12) Printf("Q“); else if(cardnum==13) Printf("K“); } Chapter 5 CS12 - Computer Programming 1
14
Passing parameters by Value (&)
This is by default and desirable behavior! An important fact to remember about parameter passing by value is that changes to the parameters inside the function body have no effect outside of the function. Example: int sum(int a, int b){ a = a + b; return a; } void main(){ int x, y, z; x = 3; y = 5; z = sum(x,y); } Chapter 5 CS12 - Computer Programming 1
15
Passing parameters by reference
The value of the parameters passed to a function will also changed in the calling function Example: #include <stdio.h> void Increment(int& Number){ Number = Number + 1; Printf("The parameter Number: %d“, Number); } void main(){ int I = 10; Increment(I); // parameter is a variable Printf("The variable I is: %d\n“, I ); Chapter 5 CS12 - Computer Programming 1
16
Passing parameters by referenc e
Example 2: int main ( ) { int a, b, x = 100; printf("Enter two numbers: "); scanf("%d%d", &a, &b); Add(a, b, x); printf("a, b, x contains %d, %d, %d\n", a, b, x); } void Add(int a, int b, int &x){ b = b - a; x = x + b; Output Enter two numbers: 6 8 a, b, x contains 6, 2, 102 a, b, x contains 6, 8, 102 CS12 - Computer Programming 1 Chapter 5
17
Programming Exercise Print the sum and average of two numbers using Pass by value Convert a value in inches to its equivalent value in Feet and inches using Pass by value Chapter 5 CS12 - Computer Programming 1
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.