Function: Declaration

Slides:



Advertisements
Similar presentations
1 C++Tutorial Rob Jagnow This tutorial will be best for students who have at least had some exposure to Java or another comparable programming language.
Advertisements

Question Bank. Explain the syntax of if else statement? Define Union Define global and local variables with example Concept of recursion with example.
SPLINT STATIC CHECKING TOOL Sripriya Subramanian 10/29/2002.
CSE 303 Lecture 16 Multi-file (larger) programs
Chapter 7: User-Defined Functions II
1 Lecture13: Other C Topics 12/17/2012. Topics Variable-length argument lists Pointers to functions Command-line arguments Suffixes for integer and floating-point.
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
C For Java Programmers Tom Roeder CS sp. Why C? The language of low-level systems programming  Commonly used (legacy code)  Trades off safety.
1 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
Review of Exam #2CS-2301, B-Term Review of Exam #2 CS-2301, System Programming for Non-Majors (Slides include materials from The C Programming Language,
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction 13.2The #include Preprocessor Directive 13.3The.
C FAQ’S Collected from the students who attended technical round in TCS recruitment.
Review of C++ Programming Part II Sheng-Fang Huang.
Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.
1 Further C  Multiple source code file projects  Structs  The preprocessor  Pointers.
18-2 Understand “Scope” of an Identifier Know the Storage Classes of variables and functions Related Chapter: ABC 5.10, 5.11.
The Structure of a C++ Program. Outline 1. Separate Compilation 2. The # Preprocessor 3. Declarations and Definitions 4. Organizing Decls & Defs into.
Chapter 0.2 – Pointers and Memory. Type Specifiers  const  may be initialised but not used in any subsequent assignment  common and useful  volatile.
1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.
Functions CIS Feb-06. Summary Slide Using Functions Mathematical Functions Misc. Functions Naming Conventions Writing Functions –Function Prototype.
Parameter Passing Mechanisms Reference Parameters § §
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 8: User-Defined Simple Data Types, Namespaces, and the string Type.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process.
(language, compilation and debugging) David 09/16/2011.
C++ / G4MICE Course Session 2 Basic C++ types. Control and Looping Functions in C Function/method signatures and scope.
CS415 C++ Programming Takamitsu Kawai x4212 G11 CERC building WV Virtual Environments Lab West Virginia University.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Preprocessor Midterm Review Lecture 7 Feb 17, 2004.
POINTERS Introduction to Systems Programming - COMP 1002, 1402.
EEL 3801 C++ as an Enhancement of C. EEL 3801 – Lotzi Bölöni Comments  Can be done with // at the start of the commented line.  The end-of-line terminates.
THE PREPROCESSOR
CSE 332: C++ pointers, arrays, and references Overview of Pointers and References Often need to refer to another object –Without making a copy of the object.
Revisiting building. Preprocessing + Compiling 2 Creates an object file for each code file (.c ->.o) Each.o file contains code of the functions and structs.
C++ Functions A bit of review (things we’ve covered so far)
1 Chapter 12 Classes and Abstraction. 2 Chapter 12 Topics Meaning of an Abstract Data Type Declaring and Using a class Data Type Using Separate Specification.
Lecture 3 Translation.
Chapter 12 Classes and Abstraction
User-Written Functions
Computer Science 210 Computer Organization
A bit of C programming Lecture 3 Uli Raich.
Functions, Part 2 of 2 Topics Functions That Return a Value
Chapter 13 - The Preprocessor
CSE 303 Concepts and Tools for Software Development
C++, OBJECT ORIENTED PROGRAMMING
Functions Separate Compilation
Computer Science 210 Computer Organization
Command-Line Arguments
C Basics.
Programmazione I a.a. 2017/2018.
Instructor: Ioannis A. Vetsikas
CS111 Computer Programming
Lecture 6 C++ Programming
Pointers and References
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Pointers, Dynamic Data, and Reference Types
Built-In (a.k.a. Native) Types in C++
Register Variables Declaring a variable as a "register" variable is an advisory to the compiler to keep the normal location of the variable in a register,
Computer Science 210 Computer Organization
Programming in C Miscellaneous Topics.
Programming in C Miscellaneous Topics.
C++ Tutorial Rob Jagnow
C Preprocessor Seema Chandak.
COP 3330 Object-oriented Programming in C++
Programming in C Pointers and Arrays.
Programming Languages and Paradigms
Functions Reasons Concepts Passing arguments to a function
Pointers, Dynamic Data, and Reference Types
Presentation transcript:

Function: Declaration <return type> <identifier>( <parameter>, ... );

Function: Declaration unsigned int square( int number ); Declaration creates prototype (older versions of C didn't require parameter list, no prototype created)

Function: Declaration unsigned int square( int );

Function: Declaration unsigned int square( int number );

Function: Definition <return type> <identifier>( <parameter>, ... ) { <statements> }

Function: Definition unsigned int square( int number ) { return number * number; } Definition “acts” as declaration if no declaration exists Definition must match prototype if declaration exists

Function: Calling unsigned int two_squared = square( 2 ); All arguments passed by value i.e. copy made Open source 07 (functions and overloading) Explain _Generic (compile time macro)

Exercises Open exercises/ex06/main.c Declare a new function with the following prototype: unsigned int gcd( unsigned int a, unsigned int b ) Define the function so that it implements Euclid's greatest common divisor algorithm as follows: if b equals 0, return a else return gcd( b, a modulo b ) ←- Recursive call to gcd() Build using Makefile Run & test. Output to console should be “GCD = 8”.

Enumeration enum [<identifier>] { <enumeration constant> [= <constant expression>], . <enumeration constant> [= <constant expression>] };

Enumeration enum MultipleChoice { OPTION_A, OPTION_B, OPTION_C, OPTION_D, ALL_OF_THE_ABOVE };

Enumeration enum MultipleChoice { OPTION_A, // 0 OPTION_B, // 1 OPTION_C, // 2 OPTION_D, // 3 ALL_OF_THE_ABOVE // 4 };

Enumeration enum MultipleChoice { OPTION_A = 10, OPTION_B, OPTION_C, OPTION_D, ALL_OF_THE_ABOVE };

Enumeration enum MultipleChoice { OPTION_A = 10, // 10 OPTION_B, // 11 OPTION_C, // 12 OPTION_D, // 13 ALL_OF_THE_ABOVE // 14 };

Enumeration enum MultipleChoice { OPTION_A, // 0 OPTION_B, // 1 OPTION_C = 12, // 12 OPTION_D, // 13 ALL_OF_THE_ABOVE // 14 };

Enumeration enum MultipleChoice question1 = OPTION_C;

Structures struct [<identifier>] { <struct declarator>; . };

Structures struct RGBA { float red; float green; float blue; float alpha; };

Structures struct RGBA polygon_colour; polygon_colour.red = 0.5f; polygon_colour.green = 0.5f; polygon_colour.blue = 0.5f; polygon_colour.alpha = 1.0f;

Typedef typedef struct RGBA RGBA;

Typedef typedef struct RGBA RGBA;

Typedef typedef struct RGBA RGBA;

Typedef typedef struct RGBA RGBA; Open source 08 Add typedef for enum and struct Briefly explain char * as string VLA char[] cannot be used, except at the end of the struct

Exercises Open exercises/ex07/main.c Define a new enum called Genre, with two or three constants for movie genres Define a new struct called BluRay, with the following attributes: char *title char *director unsigned short year enum Genre genre Define a new function that creates and returns a new BluRay, and another that prints a BluRay to console Create a BluRay in main(), and print to console, using the new functions Build using Makefile Run & test

Pointers char some_char = 'c'; char *char_ptr = &some_char;

Pointers char some_char = 'c'; char *char_ptr = &some_char;

char *char_ptr = &some_char; Pointers char some_char = 'c'; char *char_ptr = &some_char; base-type specifier

char *char_ptr = &some_char; Pointers char some_char = 'c'; char *char_ptr = &some_char; declarator

char *char_ptr = &some_char; Pointers char some_char = 'c'; char *char_ptr = &some_char; identifier

char *char_ptr = &some_char; Pointers char some_char = 'c'; char *char_ptr = &some_char; derived type

char *char_ptr = &some_char; Pointers char some_char = 'c'; char *char_ptr = &some_char; optional initialiser

char *char_ptr = &some_char; Pointers char some_char = 'c'; char *char_ptr = &some_char; address-of operator

Pointers char some_char = 'c'; char *char_ptr = 0xA1;

Pointers char some_char = 'c'; char *char_ptr = &some_char;

char *char_ptr = (char *)0xA1; Pointers char *char_ptr = (char *)0xA1; 'a' 12 ... 8 'c' 'Z' ... 0x00 0x01 0x02 0xA0 0xA1 0xA2

Pointers char some_char = 'c'; char *char_ptr = &some_char; printf( "I am pointing to %c", *char_ptr ); Open source 09 Arrays decay to pointer Pointer arithmetic – warn about pointing to other data Show loop using pointer arithmetic (assign back to pointer) Index operator dereference operator

Exercises Open exercises/ex08/main.c Complete the definition for swap(). The function must swap the values of its two arguments. Create two int variables in main() (initialise them to any values) Output the values of the variables before and after calling swap() Build using Makefile Run & test

Stack void another_func() { ... } void some_func() { another_func(); int main() { some_func(); return 0;

Stack void another_func() { ... } void some_func() { another_func(); int main() { some_func(); return 0; main()

Stack void another_func() { ... } void some_func() { another_func(); int main() { some_func(); return 0; main()

Stack void another_func() { ... } void some_func() { another_func(); int main() { some_func(); return 0; some_func() main()

Stack void another_func() { ... } void some_func() { another_func(); int main() { some_func(); return 0; some_func() main()

Stack void another_func() { ... } void some_func() { another_func(); int main() { some_func(); return 0; another_func() some_func() main()

Stack void another_func() { ... } void some_func() { another_func(); int main() { some_func(); return 0; another_func() some_func() main()

Stack void another_func() { ... } void some_func() { another_func(); int main() { some_func(); return 0; some_func() main()

Stack void another_func() { ... } void some_func() { another_func(); int main() { some_func(); return 0; main()

Stack void another_func() { ... } void some_func() { another_func(); int main() { some_func(); return 0; main()

Stack void another_func() { ... } void some_func() { another_func(); int main() { some_func(); return 0; main()

Stack void another_func() { ... } void some_func() { another_func(); int main() { some_func(); return 0; Open source 10

Exercises Open exercises/ex09/main.c Define the function zero_fill(). The function must fill mem with zeroes for the specified size. Define the function alloc_and_zero(). The function must allocate memory on the heap, of the specified size, fill it with zeroes, and return a pointer to the new heap memory Allocate a new instance of Date using alloc_and_zero(). Output the content of year, month, day to console. Build using Makefile Run & test. Ensure that output is zeroes. Open source 11 Explain function pointers before exercise on next slide

Exercises Open source 12 Open exercises/ex10/main.c Create a typedef alias for the function pointer type of the func parameter in for_each(). Rewrite the func parameter to use your new typedef. Define the function for_each(). The function must call func for each value in the array, passing in the value and its associated index. Define the function print_idx_val(). The function must print the value and index parameters to console e.g. "Value 0 is 100". In main(), call for_each() with the test values supplied and print_idx_val() Build using Makefile Run & test Open source 12

File Size fseek( file, 0, SEEK_END ); long file_size = ftell( file ); rewind( file );

Exercises Open source 12 Open exercises/ex11/main.c Define the function read_in_file(). The function must read in the contents of the file, at file_name, into memory and return it. If an error occurs, return NULL. HINT: retrieve the file size to determine the amount of memory to allocate. Define the function print_buffer(). The function must output the contents of buffer to the console. Read implementation of main(). Ensure you understand. Build using Makefile Run & test using haiku.txt Open source 12

Libraries & #include Directive main.c mylib.h #include “mylib.h” int main( int argc, char *argv[] ) { my_func( ... ); return 0; } #ifndef MYLIB_H #define MYLIB_H void my_func( ... ); #endif mylib.c #include “mylib.h” void my_func( ... ) { ... }

Libraries & #include Directive main.c mylib.h #include “mylib.h” int main( int argc, char *argv[] ) { my_func( ... ); return 0; } #ifndef MYLIB_H #define MYLIB_H void my_func( ... ); #endif mylib.c #include “mylib.h” void my_func( ... ) { ... } Library header file with single function declaration Explain inclusion guard

Libraries & #include Directive main.c mylib.h #include “mylib.h” int main( int argc, char *argv[] ) { my_func( ... ); return 0; } #pragma once void my_func( ... ); mylib.c #include “mylib.h” void my_func( ... ) { ... } #pragma once inclusion guard

Libraries & #include Directive main.c mylib.h #include “mylib.h” int main( int argc, char *argv[] ) { my_func( ... ); return 0; } #ifndef MYLIB_H #define MYLIB_H void my_func( ... ); #endif mylib.c #include “mylib.h” void my_func( ... ) { ... } Library header file with single function declaration Explain inclusion guard

Libraries & #include Directive main.c mylib.h #include “mylib.h” int main( int argc, char *argv[] ) { my_func( ... ); return 0; } #ifndef MYLIB_H #define MYLIB_H void my_func( ... ); #endif mylib.c #include “mylib.h” void my_func( ... ) { ... } Library source file #include explanation to follow Contains the definition for the function declared in header

Libraries & #include Directive main.c mylib.h #include “mylib.h” int main( int argc, char *argv[] ) { my_func( ... ); return 0; } #ifndef MYLIB_H #define MYLIB_H void my_func( ... ); #endif mylib.c #include “mylib.h” void my_func( ... ) { ... }

Libraries & #include Directive main.c mylib.h #include “mylib.h” int main( int argc, char *argv[] ) { my_func( ... ); return 0; } #ifndef MYLIB_H #define MYLIB_H void my_func( ... ); #endif mylib.c #include “mylib.h” void my_func( ... ) { ... }

Libraries & #include Directive main.c mylib.h #include “mylib.h” int main( int argc, char *argv[] ) { my_func( ... ); return 0; } #ifndef MYLIB_H #define MYLIB_H void my_func( ... ); #endif mylib.c #include “mylib.h” void my_func( ... ) { ... } Explain #include is preprocessor Point out quotes in #include

Libraries & #include Directive main.c mylib.h #include “mylib.h” int main( int argc, char *argv[] ) { my_func( ... ); return 0; } #ifndef MYLIB_H #define MYLIB_H void my_func( ... ); #endif mylib.c #include “mylib.h” void my_func( ... ) { ... } Explain inclusion guard

Libraries & #include Directive main.c mylib.h #ifndef MYLIB_H #define MYLIB_H void my_func( ... ); #endif int main( int argc, char *argv[] ) { my_func( ... ); return 0; } #ifndef MYLIB_H #define MYLIB_H void my_func( ... ); #endif mylib.c #include “mylib.h” void my_func( ... ) { ... }

Libraries & #include Directive main.c mylib.h #ifndef MYLIB_H #define MYLIB_H void my_func( ... ); #endif int main( int argc, char *argv[] ) { my_func( ... ); return 0; } #ifndef MYLIB_H #define MYLIB_H void my_func( ... ); #endif mylib.c #include “mylib.h” void my_func( ... ) { ... }

Libraries & #include Directive main.c mylib.h #ifndef MYLIB_H #define MYLIB_H void my_func( ... ); #endif int main( int argc, char *argv[] ) { my_func( ... ); return 0; } #ifndef MYLIB_H #define MYLIB_H void my_func( ... ); #endif mylib.c #ifndef MYLIB_H #define MYLIB_H void my_func( ... ); #endif void my_func( ... ) { ... }

Libraries & #include Directive main.c mylib.h #ifndef MYLIB_H #define MYLIB_H void my_func( ... ); #endif int main( int argc, char *argv[] ) { my_func( ... ); return 0; } #ifndef MYLIB_H #define MYLIB_H void my_func( ... ); #endif mylib.c #ifndef MYLIB_H #define MYLIB_H void my_func( ... ); #endif void my_func( ... ) { ... }

Libraries & #include Directive main.c mylib.h #ifndef MYLIB_H #define MYLIB_H void my_func( ... ); #endif int main( int argc, char *argv[] ) { my_func( ... ); return 0; } #ifndef MYLIB_H #define MYLIB_H void my_func( ... ); #endif mylib.c #ifndef MYLIB_H #define MYLIB_H void my_func( ... ); #endif void my_func( ... ) { ... } Compilation Unit

Libraries & Compilation main.c mylib.c cc cc main.o mylib.o ld binary executable

Libraries & Compilation main.c mylib.c cc cc main.o mylib.o ld binary executable

Libraries & Compilation main.c mylib.c cc cc main.o mylib.o ld binary executable

Libraries & Compilation main.c mylib.c cc cc main.o mylib.o ld binary executable

Libraries & Compilation main.c mylib.c cc cc main.o mylib.o ld binary executable

Libraries & Compilation main.c mylib.c cc cc main.o mylib.o ld Not really a distributable library See how to make a reusable static library now binary executable

Libraries & Compilation main.c cc main.o mylib.a ld binary executable

Libraries & Compilation main.c Static Library cc main.o mylib.a ld binary executable

Exercises Open source 12 Open exercises/ex12/src/mystring.c Define the function my_streq() that is declared in exercises/ex12/include/mystring.h. The function must compare the two argument strings to determine if they’re equal. Add a new test to exercises/ex12/tests/main.c to test your new function Build lib and tests using Makefile Run tests Open source 12

Exercises Create the header and implementation files for a new library called vector Define a new struct called Vector in your header file. The Vector struct should have the following attributes float x; float y; float z; Declare and define a function called vec_add(). The function must take two Vector arguments and return a Vector containing their sum. Declare and define a function called vec_dot(). The function must take two Vector arguments and return their dot product, calculated as: ( vec1.x * vec2.x ) + ( vec1.y * vec2.y ) + ( vec1.z * vec2.z ) Create your own tests for the functions Build using Makefile Run tests

Linked Lists head

Linked Lists: Append head

Linked Lists: Append head

Linked Lists: Append head

Linked Lists: Insert head

Linked Lists: Insert head

Linked Lists: Insert head

Linked Lists: Insert head

Linked Lists: Insert head

Linked Lists: Delete head

Linked Lists: Delete head

Linked Lists: Delete head

Linked Lists: Delete head

Linked Lists: Delete head

Linked Lists: Delete head

Linked Lists: Search Search for 8 head 21 60 8 17 31

Linked Lists: Search Search for 8 head 21 60 8 17 31

Linked Lists: Search Search for 8 head 21 60 8 17 31

Linked Lists: Search Search for 8 head 21 60 8 17 31

Linked Lists: Search Search for 8 head 21 60 8 17 31

Linked Lists: Search Search for 8 head 21 60 8 17 31

Linked Lists: Search Search for 8 head 21 60 8 17 31

Linked Lists: Search What would happen if 8 didn’t exist? Search for 8 head 21 60 8 17 31 What would happen if 8 didn’t exist?