0% found this document useful (0 votes)
17 views

C Functions

This document provides an overview of functions in the C programming language as covered in the CSC 203 Computer Programming I course. The key topics covered include: 1. Understanding the basic structure and uses of functions in C, including defining functions with return types, parameters, and bodies. 2. Examples of how to declare, define, call and pass arguments to functions using both call by value and call by reference. 3. A discussion of common function aspects like formal and actual parameters, and how functions can be used to organize and reuse code in a program.

Uploaded by

bankolebu.20
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

C Functions

This document provides an overview of functions in the C programming language as covered in the CSC 203 Computer Programming I course. The key topics covered include: 1. Understanding the basic structure and uses of functions in C, including defining functions with return types, parameters, and bodies. 2. Examples of how to declare, define, call and pass arguments to functions using both call by value and call by reference. 3. A discussion of common function aspects like formal and actual parameters, and how functions can be used to organize and reuse code in a program.

Uploaded by

bankolebu.20
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 29

CSC 203

Computer Programming I
Course Outline

• Understand C Language Overview and Program


Structure, and Data types.
• Fundamentals of C Operators and Escape
Sequence
• Decision making in C
• Program Loops in C
• Uses of Functions in C
• Understand Storage Classes and Scope
• Understand Pointers and Use Pointers Effectively
• Create Structures, Unions and Data Storage
C-Functions
Functions in C
A function is a group of statements that together perform a
task.

A function is a collection of statement that will perform a


particular task.

A function can also be referred as a method or a sub-


routine or a procedure, etc.

Every C program has at least one function, which


is main().
Functions in C
Uses of C functions:
•C functions are used to avoid rewriting same logic/code
again and again in a program.
•There is no limit in calling C functions to make use of same
functionality wherever required.
•We can call functions any number of times in a program
and from any place in a program.
•A large C program can easily be tracked when it is divided
into functions.
•The core concept of C functions are, re-usability, dividing a
big task into small pieces to achieve the functionality and to
improve understandability of very large C programs.
Functions in C

A function definition in C programming consists of


a function header and a function body.

return_type function_name( parameter list )


{
body of the function
}
Functions in C

Return Type − is the data type of the value the function


returns.
A function may return a value.
Some functions perform the desired operations
without returning a value. In this case, the return_type
is the keyword void.

Function Name − This is the actual name of the function.


The function name and the parameter list together
constitute the function signature.
Functions in C

Parameters − A parameter is like a placeholder.

When a function is invoked, you pass a value to the parameter. This


value is referred to as actual parameter or argument.

The parameter list refers to the type, order, and number of the
parameters of a function.

Parameters are optional; that is, a function may contain no parameters.

Function Body − The function body contains a collection of statements


that define what the function does.
Functions in C
Example 1:
/* function returning the max between two numbers */
1. #include<stdio.h>
2. int max(int num1, int num2)
3. { /* local variable declaration */
4. int result;
5. if (num1 > num2)
6. result = num1;
7. Else
8. result = num2;
9. return result;
10. }
Functions in C

Example 1 shows the source code for a function


called max().

This function takes two parameters:


num1 and num2 and
returns the maximum value between the two .
A function declaration or prototype: tells the compiler
about a function's name, return type, and parameters.
or
A function declaration tells the compiler about a function
name and how to call the function. The actual body of the
function can be defined separately.
return_type function_name( parameter list );

From Example 1:
int max(int num1, int num2); or
int max(int, int);
Calling a Function

While creating a C function, you give a definition of


what the function has to do.
To use a function, you will have to call that function to
perform the defined task.
• A called function performs a defined task and when
its return statement is executed or when its function-
ending closing brace is reached, it returns the
program control back to the main program.
• To call a function, you simply need to pass the
required parameters along with the function name,
and if the function returns a value, then you can store
the returned value.
Example 2: Calling a Function in C
1. #include <stdio.h> /* function declaration */
2. int max(int num1, int num2);
3. int main () { /* local variable definition */
4. int a = 100;
5. int b = 200;
6. int ret; /* calling a function to get max value */
7. ret = max(a, b);
8. printf( "Max value is : %d\n", ret );
9. return 0;
10.} /* function returning the max between two numbers */
11.int max(int num1, int num2) { /* local variable declaration */
12. int result;
13. if (num1 > num2)
14. result = num1;
15. else
16. result = num2;
17. return result;
18.}
C function declaration, function call and
function definition:
C functions aspects syntax

Return_type function_name (arguments list)


function definition
{ Body of function; }

function call function_name (arguments list);

function declaration return_type function_name (argument list);


Example 3 program for C function
1. #include<stdio.h>
2. // function prototype, also called function declaration
3. float square ( float x );
4. // main function, program starts from here
5. int main( )
6. {
7. float m, n ;
8. printf ( "\nEnter some number for finding square \n");
9. scanf ( "%f", &m ) ;
10. // function call
11. n = square ( m ) ;
12. printf ( "\nSquare of the given number %f is %f",m,n );
13. }
14. float square ( float x ) // function definition Output:
15. { Enter some number for finding square
16. float p ; 2
17. p = x * x ; Square of the given number 2.000000 is 4.000000
18. return ( p ) ;
19. }
Explanation: Example 3 program
• In the example program, function “square” is called from
main function.
• The value of “m” is passed as argument to the function
“square”.
• This value is multiplied by itself in this function and
multiplied value “p” is returned to main function from
function “square”.
If a function is to use arguments, it must declare variables that accept the values of the
arguments.

These variables are called the formal parameters of the function.


• Formal parameters behave like other local variables inside the function and are
created upon entry into the function and destroyed upon exit.

While calling a function, there are two ways in which arguments can be passed to a
function :

• Calling by Value: This method copies the actual value of an argument into the formal
parameter of the function.
• Calling by Reference: This method copies the address of an argument into the formal
parameter.

By default, C uses call by value to pass arguments.


Function Argument: Call By Value

Call by value:
• In call by value method, the value of the variable is passed to the
function as parameter.
• The value of the actual parameter can not be modified by formal
parameter.
• Different Memory is allocated for both actual and formal
parameters. Because, value of actual parameter is copied to
formal parameter.
Note:
• Actual parameter – This is the argument which is used in function
call.
• Formal parameter – This is the argument which is used in function
definition
Example 4 program for C function (using call by value)
1. #include<stdio.h>
2. // function prototype, also called function declaration
3. void swap(int a, int b);
4. int main()
5. {
6. int m = 22, n = 44;
7. // calling swap function by value
8. printf(" values before swap m = %d \nand n = %d", m, n);
9. swap(m, n);
Output:
10. }
11.
12. void swap(int a, int b)
13. {
14. int tmp;
15. tmp = a;
16. a = b;
17. b = tmp;
18. printf(" \nvalues after swap m = %d\n and n = %d", a, b);
19. }
Explanation: Example 4 program

In this program, the values of the variables


“m” and “n” are passed to the function
“swap”.

These values are copied to formal


parameters “a” and “b” in swap function and
used.
Call by reference:
In call by reference method, the address of the variable is
passed to the function as parameter.
• The value of the actual parameter can be modified by
formal parameter.
• Same memory is used for both actual and formal
parameters since only address is used by both
parameters.
Example 5 program for C function (using call by reference)
1. #include<stdio.h>
2. // function prototype, also called function declaration
3. void swap(int *a, int *b);
4. int main()
5. {
6. int m = 22, n = 44;
7. // calling swap function by reference
8. printf("values before swap m = %d \n and n = %d",m,n);
9. swap(&m, &n);
10. } Output:
11. void swap(int *a, int *b)
12. {
13. int tmp;
14. tmp = *a;
15. *a = *b;
16. *b = tmp;
17. printf("\n values after swap a = %d \nand b = %d", *a, *b);
18. }
Explanation: Example 5 program

• In this program, the address of the variables “m” and “n”


are passed to the function “swap”.

• These values are not copied to formal parameters “a” and


“b” in swap function.

• Because, they are just holding the address of those


variables.

• This address is used to access and change the values of


the variables.
Function Library
• A function library is a collection functions that share a
common area of interest (e.g. Math, Time functions in
Arduino C).

• Many vendors have added new libraries to support


products and add-ons they sell for Arduino family.

• You can create your own functions library in C


language.
Function Signature and Function Prototype
Int LeapYear(int year) // Function Signature

Int LeapYear(int year);

// Function Prototype (note semicolon at the


end!)
Function Body

int VolumeOfCube(int width, int length, int height)


{
int volume;
volume = width*length*height;
return volume;
}

• NB: The function body begins with the opening brace


({) and follows the closing parenthesis of the argument
list and extends to the closing to the closing brace (}).
It starts where the function signature ends.
Overloaded Function
When a function shares a common name, but has
two or more different signatures, it is called an
overloaded function.
In most cases it is the argument list that differs
across signatures

This is a C++ concept. Technically C Programming


does not allow overloading. But since Arduino uses
C++ compiler, it allows it.
Revision Exercises

1.What is a function?
2. What are the two principal components of a
function?
3.What is a function signature?
4.What does function overloading mean?
5.What is a function type specifier?
6.What is the purpose of a return statement and can a
function return more than one value?
7.Name 3 things you should strive for when writing
your own functions?
Thank you all for
listening…

You might also like