Download presentation
Presentation is loading. Please wait.
Published byAmice Parker Modified over 8 years ago
1
Announcements
2
Practice questions, with and without solutions will be uploaded by Friday 5 th November, make sure to check them before the weekend \\netstorage\Subjects\ITCA-b\Exam Practice
3
Functions
4
4 Introducing Functions A C program is constructed from building blocks called functions main() is a function where program execution begins A function is a subroutine that contains one or more C statements and performs one or more tasks
5
5 Functions … contd. Two types of functions: those written by You and others provided to you in the compiler’s standard library – Library functions (printf, scanf) – User defined
6
6 Example … library function abs() #include int main() { int x = abs(-10); return 0; } Gives output x
7
7 Functions … contd. Every function must have a name (main() is reserved) One function cannot be embedded within another function (these are separate entities) One function may call another
8
8 Function Arguments A value passed to a function is called an argument It is passed by specifying it between the parenthesis that follow the function’s name int x = abs(-10);
9
9 C function general form Return-type function-name(parameter list) {..body of function. }
10
10 Function Parameters The variables in a function that receive arguments are called parameters of the function int square(int); int main() { int a = 5; int z = square(a); return 0; } int square(int x) { int z = x *x; return z; } Parameters are local variables of a function
11
11 Function Function Prototype Function Call Function Definition include int square(int); int main(){ int a = 5; int z = square(a); return 0; } int square(int x){ int z = x *x; return z; }
12
12 Function Definitions Function prototype – Tells compiler argument type and return type of function – int square( int ); Function takes an int and returns an int include int square(int); int main(){ int a = 5; int z = square(a); return 0; } int square(int x){ int z = x *x; return z; }
13
13 Function Definitions Calling/invoking a function – square(x); – Parentheses an operator used to call function Pass argument x Function gets its own copy of arguments – After finished, passes back result include int square(int); int main(){ int a = 5; int z = square(a); return 0; } int square(int x){ int z = x *x; return z; }
14
14 Function Prototypes Function prototype contains – Function name – Parameters (number and data type) – Return type ( void if returns nothing) – Only needed if function definition after function call include int square(int); int main(){ int a = 5; int z = square(a); return 0; } int square(int x){ int z = x *x; return z; }
15
15 Function Prototypes Prototype must match function definition – Function prototype double maximum( double, double, double ); – Function Definition double maximum( double x, double y, double z ) { … }
16
16 Function Prototypes If the function definition is after it is called then at least the function prototype should be available before it is called.
17
17 A function can be declared to return any valid C data type (except that a function cannot return an array) The following are valid: x=power(10,y); if (max(x,y)>100) Returning values from functions
18
Wrong
19
Returns one value at a time
20
Scope Rules for Functions #include void display(int); void main(void){ int i=20; display(i); getch(); } void display(int j){ int k=35; printf("\n%d",j); printf("\n%d",k); } The value of i is not automatically available to function display and has to be passed
21
Write a function which receives a float and an int from main( ), finds the product of these two and returns the product as a float which is printed through main( ).
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.